4.3. Managing Files
A large part of system administration involves dealing with files and directories: creating directories, copying files, moving files and directories around, and deleting them. Fedora provides a powerful set of tools for managing files from the shell prompt as well as graphically.
4.3.1. How Do I Do That?
Linux, like most modern operating systems, uses a tree-like hierarchy to store and organize files. To manage files effectively, extend the hierarchy to organize your data.
22.214.171.124. Understanding Linux directory names
Fedora's master directory (or folder, as it would be referred to by other operating systems) is called the root directory; it may contain files and directories. Each of those directories may in turn contain other files and directories.
For each user, one directory is designated as the home directory, and that is where that user stores her personal files. Additionally, each process (a running copy of a program) has a current working directory on the system, which is the directory that it accesses by default unless another directory is explicitly specified.
The root directory is always the same system-wide; the home directory is consistent for a particular user, but varies from user to user; and the current working directory is unique to each process and can be changed anytime.
A pathname specifies how to find a file in the file hierarchy. There are three different pathname schemes that can be used, based on the three different starting points (root, home, and current working directory); each scheme specifies the path from the selected starting point to the desired file, separating directory names with the forward slash character (/). These three schemes are summarized in Table 4-4.
The special symbols . (same directory) and .. (parent directory) are useful in pathnames. For example, if your current directory is /home/chris/book, then ../invitation refers to /home/chris/invitation.
126.96.36.199. Key directories
Fedora uses a standard set of directories derived from historical conventions, the Linux Standard Base (LSB) project, and the kernel. Table 4-5 outlines the key directories and their purpose.
Local files refers to filesbinaries, scripts, and datafilesthat you have developed and that are not part of Fedora. Separating these files from the rest of the operating system makes it easier to move them to a new system in the future.
188.8.131.52. Ambiguous filenames
The wildcard characters ? and * can be used for pattern matching, which is useful for dealing with several files at a time without individually specifying each filename. ? will match any one character in a filename, and * will match any number of any characters (including none).
Square brackets  can be used to contain a list of characters , a range of characters [aj], or a combined list and range [123aj]; this pattern will match any one character from the list or range. Using an exclamation mark or carat symbol as the first character inside the square brackets will invert the meaning, causing a match with any one character which is not in the list or range.
Table 4-6 lists some examples of ambiguous filenames.
184.108.40.206. Choosing easy-to-use filenames
Linux filenames can be up to 254 characters long and contain letters, spaces, digits, and most punctuation marks. However, names that contain certain punctuation marks or spaces cannot be used as command arguments unless you place quote marks around the name (and even then there may be problems). Linux filenames are also case-sensitive, so it's productive to adopt a consistent naming convention and stick to it.
Here are my recommendations for Linux filenames:
220.127.116.11. Listing the contents of directories
$ ls 4Suite crontab hosts libuser.conf nxserver a2ps.cfg cron.weekly hosts.allow lisarc oaf ...(Lines snipped)...
You can specify an alternate directory or file pattern as an argument:
$ ls / bin etc lost+found mnt proc sbin sys usr boot home media net ptal selinux tftpboot var dev lib misc opt root srv tmp $ ls -d a* a2ps.cfg alsa ant.conf audit.rules a2ps-site.cfg alternatives ant.d auto.master acpi amanda asound.state auto.misc adjtime amandates atalk auto.net alchemist amd.conf at.deny auto.smb aliases amd.net atmsigd.conf aliases.db anacrontab auditd.conf
By default, filenames starting with a dot (.) are not shown. This provides a convenient way to store information such as a program configuration in a file without constantly seeing the filename in directory listings; you'll encounter many dot files and directories in your home directory. If you wish to see these "hidden" files, add the -a (all) option:
$ ls -a
ls can display more than just the name of each file. The -l (long) option will change the output to include the security permissions, number of names, user and group name, file size in bytes, and the date and time of last modification:
$ ls -l -rw------- 1 chris chris 3962 Aug 29 02:57 a2script -rwx------ 1 chris chris 17001 Aug 29 02:57 ab1 -rw------- 1 chris chris 2094 Aug 29 02:57 ab1.c -rwx------ 1 chris chris 884 Aug 29 02:57 perl1 -rw------- 1 chris chris 884 Aug 29 02:57 perl1.bck -rwx------ 1 chris chris 55 Aug 29 02:57 perl2 -rw------- 1 chris chris 55 Aug 29 02:57 perl2.bck -rwx------ 1 chris chris 11704 Aug 29 02:57 pointer1 -rw------- 1 chris chris 228 Aug 29 02:57 pointer1.c -rwx------ 1 chris chris 12974 Aug 29 02:57 pp1 -rw------- 1 chris chris 2294 Aug 29 02:57 pp1.c
$ ls -S -l -rwx------ 1 chris chris 17001 Aug 29 02:57 ab1 -rwx------ 1 chris chris 12974 Aug 29 02:57 pp1 -rwx------ 1 chris chris 11704 Aug 29 02:57 pointer1 -rw------- 1 chris chris 3962 Aug 29 02:57 a2script -rw------- 1 chris chris 2294 Aug 29 02:57 pp1.c -rw------- 1 chris chris 2094 Aug 29 02:57 ab1.c -rwx------ 1 chris chris 884 Aug 29 02:57 perl1 -rw------- 1 chris chris 884 Aug 29 02:57 perl1.bck -rw------- 1 chris chris 228 Aug 29 02:57 pointer1.c -rwx------ 1 chris chris 55 Aug 29 02:57 perl2 -rw------- 1 chris chris 55 Aug 29 02:57 perl2.bck
There are dozens of options to the ls command; see its manpage for details.
18.104.22.168. Displaying and changing the current working directory
$ pwd /home/chris
To change to the /tmp directory:
$ cd /tmp
To change to the foo directory within the current directory:
$ cd foo
To change back to the directory you were in before the last cd command:
$ cd -
To change to the book directory within your home directory, regardless of the current working directory:
$ cd ~/book
To change to jason's home directory:
$ cd ~jason/
22.214.171.124. Creating and removing directories from the command line
$ mkdir newdirectory
To create a chain of directories, or a directory when one or more of the parent directories might not exist, use the -p (path) option:
$ mkdir -p foo/bar/baz/qux
This has the side effect of turning off any warning messages if the directory already exists.
$ rmdir newdirectory
$ rm -r newdirectory
126.96.36.199. Copying files
To copy a file, use the cp command with the source and destination filenames as positional arguments:
$ cp /etc/passwd /tmp/passwd-copy
This will make a copy of /etc/passwd named /tmp/passwd-copy. You can copy multiple files with a single cp command as long as the destination is a directory; for example, to copy /etc/passwd to /tmp/passwd and /etc/hosts to /tmp/hosts:
$ cp /etc/passwd /etc/hosts /tmp
188.8.131.52. Renaming and moving files
In Linux, renaming and moving files are considered the same operation and are performed with the mv command. In either cases, you're changing the pathname under which the file is stored without changing the contents of the file.
To change a file named yellow to be named purple in the current directory:
$ mv yellow purple
$ mv ~jason/orange ~
184.108.40.206. Removing files
$ rm badfile
You will not be prompted for confirmation as long as you are the owner of the file. To disable confirmation in all cases, use -f (force):
$ rm -f badfile
Or to enable confirmation in all cases, use -i (interactive):
$ rm -i badfile rm: remove regular empty file \Q badfile ' ? y
220.127.116.11. Creating multiple names by linking files
$ ls -i /etc/hosts 3410634 /etc/hosts
This is useful in several situations. For example, the links can appear in different directories, giving convenient access to one file from two parts of the filesystem, or a file can be given a long and detailed name as well as a short name to reduce typing.
Links are created using the ln command. The first argument is an existing filename (source), and the last argument is the filename to be created (destination), just like the cp and mv commands. If multiple source filenames are given, the destination must be a directory.
For example, to create a link to /etc/passwd named ~/passwords, type:
$ ln /etc/passwd ~/passwords
The second column in the output from ls -l displays the number of links on a file:
$ ls -l electric.mp3 -rw-rw-r-- 1 chris chris 23871 Oct 13 01:00 electric.mp3 $ rm zap.mp3 $ ln electric.mp3 zap.mp3 $ ls -l electric.mp3 -rw-rw-r-- 2 chris chris 23871 Oct 13 01:00 electric.mp3
The alternative to a hard link is a symbolic link, which links one filename to another filename instead of linking a filename to an inode number. This provides a work-around for all three of the limitations of hard links.
$ ls -l ants.avi -rw-rw-r-- 1 chris chris 1539071 Oct 13 01:06 ants.avi $ ln -s ants.avi ants_in_ant_farm.avi $ ls -l *ants* -rw-rw-r-- 1 chris chris 1539071 Oct 13 01:06 ants.avi lrwxrwxrwx 1 chris chris 8 Oct 13 01:06 ants_in_ant_farm.avi -> ants.avi
18.104.22.168. Determining the contents of files
$ file * fable: ASCII text newicon.png: empty passwd: ASCII text README: ASCII English text xpdf.png: PNG image data, 48 x 48, 8-bit/color RGBA, non-interlaced
22.214.171.124. Viewing the contents of text files
$ cat README Dia is a program for drawing structured diagrams. ...(more)...
$ less README
126.96.36.199. Managing files graphically using GNOME
When you are logged in to GNOME, Nautilus is already running as part of the desktop environment. To open a Nautilus window, double-click on the Home icon on your desktop or select a folder from the Places menu. A window will appear, such as the one shown in Figure 4-2, showing each file as an icon. Emblems overlaid on the icons are used to indicate the file status, such as read-only.
Figure 4-2. Nautilus file management window
By default, Nautilus uses a spatial mode, which means that each directory will open in a separate window, and those windows will retain their position when closed, re-opening at the same location when you access them later.
You can open child directories by double-clicking on them, or you can open a parent directory using the pull-down menu in the bottom-lefthand corner of the window. To deal with more than one directory (for example, for a copy or move operation), open windows for each of the directories and arrange them on the screen so that they are not overlapping.
To manage files, start by selecting one or more files:
188.8.131.52. Managing files graphically with KDE
KDE's Konqueror is both a file manager and a web browser. Figure 4-3 shows the file manager view. Although at first glance this looks similar to Nautilus, Konqueror offers a larger set of features, most of which are accessed through the toolbar and menus.
Figure 4-3. Konqueror in file management mode
To start Konqueror, select Home from the K menu. Unlike Nautilus, Konqueror does not use spatial windows; as you move around the file hierarchy, the same window is reused. To create a second window for drag-and-drop, press Ctrl-N (or select the menu option LocationNew Window). Alternately, you can split a window horizontally or vertically using the Window menu, and then drag and drop between the two panes. To view more information about the files, select the menu option ViewView ModeDetailed List View, which shows information similar to that displayed by ls -l. There are other options on the View Mode menu that are useful in different situations, such as the Photobook view for directories of photographs.
You can change to child directories by double-clicking on them, or you can change to parent directories by using the up-arrow icon on the toolbar. You can also select a directory from the Navigation Panel, shown on the left in Figure 4-3 (the Navigation Panel can be toggled on and off using the F9 key).
To manage files, start by selecting one or more files:
Once you have selected a file (or files):
As with Nautilus, you can also use traditional cut, copy, and paste operations on the files:
4.3.2. How Does It Work?
184.108.40.206. Matching filenames
$ ls /etc/*release*
When the user presses Enter, the shell converts /etc/*release* into a list of matching filenames before it executes the command. The command effectively becomes:
$ ls /etc/fedora-release /etc/lsb-release /etc/redhat-release
This is different from some other platforms, where the application itself is responsible for filename expansion. The use of shell globbing simplifies the design of software, but it can cause unexpected side effects when an argument is not intended to be a filename. For example, the echo command is used to display messages:
$ echo This is a test. This is a test.
$ echo *** This is a test. *** bin boot dev etc home lib lost+found media misc mnt net opt proc ptal root sbin selinux srv sys tftpboot tmp usr var This is a test. bin boot dev etc home lib lost+found media misc mnt net opt proc ptal root sbin selinux srv sys tftpboot tmp usr var
$ echo "*** This is a test. ***" *** This is a test. ***
220.127.116.11. The merged file hierarchy
Microsoft Windows uses drive designators at the start of pathnames, such as the C: in C:\Windows\System32\foo.dll, to indicate which disk drive a particular file is on. Linux instead merges all active filesystems into a single file hierarchy; different drives and partitions are grafted onto the tree in a process called mounting.
$ mount /dev/mapper/main-root on / type ext3 (rw) /dev/proc on /proc type proc (rw) /dev/sys on /sys type sysfs (rw) /dev/devpts on /dev/pts type devpts (rw,gid=5,mode=620) /dev/md0 on /boot type ext3 (rw) /dev/shm on /dev/shm type tmpfs (rw) /dev/mapper/main-home on /home type ext3 (rw) /dev/mapper/main-var on /var type ext3 (rw) /dev/sdc1 on /media/usbdisk type vfat (rw,nosuid,nodev,_netdev,fscontext=system_u:object_r:removable_t,user=chris)
Or you can view the same information in a slightly more readable form, along with free-space statistics, by running the df command; here I've used the -h option so that free space is displayed in human-friendly units (gigabytes, megabytes) rather than disk blocks:
$ df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/main-root 30G 12G 17G 42% / /dev/md0 251M 29M 210M 13% /boot /dev/shm 506M 0 506M 0% /dev/shm /dev/mapper/main-home 48G 6.6G 39G 15% /home /dev/mapper/main-var 30G 13G 16G 45% /var /dev/sdc1 63M 21M 42M 34% /media/usbdisk
4.3.3. What About...
18.104.22.168. ...finding out which files are going to match an ambiguous filename before executing a command?
$ ls a* (press Tab, Tab) a2.html all-20090412 a3f1.html $ ls a*
$ ls a* (press Esc-*) $ ls a2.html all-20050412 a3f1.html
22.214.171.124. ...entering a filename quickly at the shell prompt?
Type the first few characters of the filename, then press Tab. bash will fill in the rest of the name (or as much as is unique). For example, if there is only one filename in the current directory that starts with all:
$ ls all (press Tab) $ ls all-20090412
126.96.36.199. ...using a filename in one command, and then reusing that filename in the next command?
$ mkdir backup-directory-august $ cd (press Esc, _) $ cd backup-directory-august
4.3.4. Where Can I Learn More?