Previous Section
 < Day Day Up > 
Next Section


File and Directory Operations: find, cp, mv, rm, and ln

As you create more and more files, you may want to back them up, change their names, erase some of them, or even give them added names. Linux provides you with several file commands that enable you to search for files, copy files, rename files, or remove files (see Table 10-5 later in this chapter). If you have a large number of files, you can also search them to locate a specific one. The commands are shortened forms of full words, consisting of only two characters. The cp command stands for "copy" and copies a file, mv stands for "move" and renames or moves a file, rm stands for "remove" and erases a file, and ln stands for "link" and adds another name for a file, often used as a shortcut to the original. One exception to the two-character rule is the find command, which performs searches of your filenames to find a file.

Searching Directories: find

Once you have a large number of files in many different directories, you may need to search them to locate a specific file, or files, of a certain type. The find command enables you to perform such a search. The find command takes as its arguments directory names followed by several possible options that specify the type of search and the criteria for the search. find then searches within the directories listed and their subdirectories for files that meet these criteria. The find command can search for a file by name, type, owner, and even the time of the last update.

$ find directory-list -option criteria

The -name option has as its criteria a pattern and instructs find to search for the filename that matches that pattern. To search for a file by name, you use the find command with the directory name followed by the -name option and the name of the file.

$ find directory-list -name filename

The find command also has options that merely perform actions, such as outputting the results of a search. If you want find to display the filenames it has found, you simply include the -print option on the command line along with any other options. The -print option instructs find to output to the standard output the names of all the files it locates (you can also use the -ls option to list files in the long format). In the next example, the user searches for all the files in the reports directory with the name monday. Once located, the file, with its relative pathname, is printed.

$ find reports -name monday -print
reports/monday

The find command prints out the filenames using the directory name specified in the directory list. If you specify an absolute pathname, the absolute path of the found directories will be output. If you specify a relative pathname, only the relative pathname is output. In the preceding example, the user specified a relative pathname, reports, in the directory list. Located filenames were output beginning with this relative pathname. In the next example, the user specifies an absolute pathname in the directory list. Located filenames are then output using this absolute pathname.

$ find /home/chris -name monday -print
/home/chris/reports/monday

Searching the Working Directory

If you want to search your working directory, you can use the dot in the directory pathname to represent your working directory. The double dots would represent the parent directory. The next example searches all files and subdirectories in the working directory, using the dot to represent the working directory. If you are located in your home directory, this is a convenient way to search through all your own directories. Notice the located filenames are output beginning with a dot.

$ find . -name weather -print
./weather

You can use shell wildcard characters as part of the pattern criteria for searching files. The special character must be quoted, however, to avoid evaluation by the shell. In the next example, all files with the .c extension in the programs directory are searched for:

$ find programs -name '*.c' -print

Locating Directories

You can also use the find command to locate other directories. In Linux, a directory is officially classified as a special type of file. Although all files have a byte-stream format, some files, such as directories, are used in special ways. In this sense, a file can be said to have a file type. The find command has an option called -type that searches for a file of a given type. The -type option takes a one-character modifier that represents the file type. The modifier that represents a directory is a d. In the next example, both the directory name and the directory file type are used to search for the directory called thankyou:

$ find /home/chris -name thankyou -type d -print
/home/chris/letters/thankyou
$

File types are not so much different types of files as they are the file format applied to other components of the operating system, such as devices. In this sense, a device is treated as a type of file, and you can use find to search for devices and directories, as well as ordinary files. Table 10-4 lists the different types available for the find command's -type option.

Table 10-4: The find Command

Command or Option

Execution

find

Searches directories for files according to search criteria. This command has several options that specify the type of criteria and actions to be taken.

-name pattern

Searches for files with the pattern in the name.

-group name

Searches for files belonging to this group name.

-size numc

Searches for files with the size num in blocks. If c is added after num, the size in bytes (characters) is searched for.

-mtime num

Searches for files last modified num days ago.

-newer pattern

Searches for files modified after the one matched by pattern.

-print

Outputs the result of the search to the standard output. The result is usually a list of filenames, including their full pathnames.

-type filetype

Searches for files with the specified file type. File type can be b for block device, c for character device, d for directory, f for file, or l for symbolic link.

Copying Files

To make a copy of a file, you simply give cp two filenames as its arguments (see Table 10-5). The first filename is the name of the file to be copied—the one that already exists. This is often referred to as the source file. The second filename is the name you want for the copy. This will be a new file containing a copy of all the data in the source file. This second argument is often referred to as the destination file. The syntax for the cp command follows:

$ cp source-file destination-file

In the next example, the user copies a file called proposal to a new file called oldprop:

$ cp proposal oldprop

You could unintentionally destroy another file with the cp command. The cp command generates a copy by first creating a file and then copying data into it. If another file has the same name as the destination file, that file is destroyed and a new file with that name is created. By default, Red Hat configures your system to check for an existing copy by the same name (cp aliases with the -i option, see Chapter 9). To copy a file from your working directory to another directory, you only need to use that directory name as the second argument in the cp command. In the next example, the proposal file is overwritten by the newprop file. The proposal file already exists.

$ cp newprop proposal

You can use any of the wildcard characters to generate a list of filenames to use with cp or mv. For example, suppose you need to copy all your C source code files to a given directory. Instead of listing each one individually on the command line, you could use an * character with the .c extension to match on and generate a list of C source code files (all files with a .c extension). In the next example, the user copies all source code files in the current directory to the sourcebks directory:

$ cp *.c sourcebks

If you want to copy all the files in a given directory to another directory, you could use * to match on and generate a list of all those files in a cp command. In the next example, the user copies all the files in the props directory to the oldprop directory. Notice the use of a props pathname preceding the * special characters. In this context, props is a pathname that will be appended before each file in the list that * generates.

$ cp props/* oldprop

You can, of course, use any of the other special characters, such as ., ?, or []. In the next example, the user copies both source code and object code files (.c and .o) to the projbk directory:

$ cp *.[oc] projbk

When you copy a file, you may want to give the copy a different name than the original. To do so, place the new filename after the directory name, separated by a slash.

$ cp filename directory-name/new-filename

Moving Files

You can use the mv command either to rename a file or to move a file from one directory to another. When using mv to rename a file, you simply use the new filename as the second argument. The first argument is the current name of the file you are renaming. If you want to rename a file when you move it, you can specify the new name of the file after the directory name. In the next example, the proposal file is renamed with the name version1:

$ mv proposal version1

As with cp, it is easy for mv to erase a file accidentally. When renaming a file, you might accidentally choose a filename already used by another file. In this case, that other file will be erased. The mv command also has an -i option that checks first to see if a file by that name already exists.

You can also use any of the special characters described in Chapter 8 to generate a list of filenames to use with mv. In the next example, the user moves all source code files in the current directory to the newproj directory:

$ mv *.c newproj

If you want to move all the files in a given directory to another directory, you can use * to match on and generate a list of all those files. In the next example, the user moves all the files in the reports directory to the repbks directory:

$ mv reports/* repbks
Note 

To copy files to a CD-R/RW or DVD-R/RW disk, you can use any number of CD/DVD burning tools. Many of these are interfaces to the cdrecord and dvdrecord utilities, along with mkisofs and cdda2wav. On KDE, you can use KOnCD, CD-Rchive, and KreateCD, and on GNOME, GNOME Toaster, GNOME CD Master, and CD-rec. With cdrecord and dvdrecord, you can copy files to a CD-R/RW directly. mkisofs is used to create CD images, and cdda2wav records tracks from music CDs. For DVD+RW/+R drives, you use the dvd+rw tools such as growisofs and dvd+rw-format (see Chapter 30).

Copying and Moving Directories

You can also copy or move whole directories at once. Both cp and mv can take as their first argument a directory name, enabling you to copy or move subdirectories from one directory into another (see Table 10-5). The first argument is the name of the directory to be moved or copied, while the second argument is the name of the directory within which it is to be placed. The same pathname structure used for files applies to moving or copying directories.

You can just as easily copy subdirectories from one directory to another. To copy a directory, the cp command requires you to use the -r option. The -r option stands for "recursive." It directs the cp command to copy a directory, as well as any subdirectories it may contain. In other words, the entire directory subtree, from that directory on, will be copied. In the next example, the thankyou directory is copied to the oldletters directory. Now two thankyou subdirectories exist, one in letters and one in oldletters.

$ cp -r letters/thankyou oldletters
$ ls -F letters
/thankyou
$ ls -F oldletters
/thankyou

Erasing Files and Directories: the rm Command

As you use Linux, you will find the number of files you use increases rapidly. Generating files in Linux is easy. Applications such as editors, and commands such as cp, easily create files. Eventually, many of these files may become outdated and useless. You can then remove them with the rm command. The rm command can take any number of arguments, enabling you to list several filenames and erase them all at the same time. In the next example, the user erases the file oldprop:

$ rm oldprop

Be careful when using the rm command, because it is irrevocable. Once a file is removed, it cannot be restored (there is no undo). With the -i option, you are prompted separately for each file and asked whether to remove it. If you enter y, the file will be removed. If you enter anything else, the file is not removed. In the next example, the rm command is instructed to erase the files proposal and oldprop. The rm command then asks for confirmation for each file. The user decides to remove oldprop, but not proposal.

$ rm -i proposal oldprop
Remove proposal? n
Remove oldprop? y
$
Table 10-5: File Operations

Command

Execution

cp filename filename

Copies a file. cp takes two arguments: the original file and the name of the new copy. You can use pathnames for the files to copy across directories:
$ cp today reports/monday

cp -r dirname dirname

Copies a subdirectory from one directory to another. The copied directory includes all its own subdirectories:
$ cp -r letters/thankyou oldletters

mv filename filename

Moves (renames) a file. mv takes two arguments: the first is the file to be moved. The second argument can be the new filename or the pathname of a directory. If it is the name of a directory, then the file is literally moved to that directory, changing the file's pathname:
$ mv today /home/chris/reports

mv dirname dirname

Moves directories. In this case, the first and last arguments are directories:
$ mv letters/thankyou oldletters

ln filename filename

Creates added names for files referred to as links. A link can be created in one directory that references a file in another directory:
$ ln today reports/monday

rm filenames

Removes (erases) a file. Can take any number of filenames as its arguments. Literally removes links to a file. If a file has more than one link, you need to remove all of them to erase a file:
$rm today weather weekend

Links: the ln Command

You can give a file more than one name using the ln command. You might want to reference a file using different filenames to access it from different directories. The added names are often referred to as links. Linux supports two different types of links, hard and symbolic. Hard links are literally another name for the same file, whereas symbolic links function like shortcuts referencing another file. Symbolic links are much more flexible and can work over many different file systems, whereas hard links are limited to your local file system. Furthermore, hard links introduce security concerns, as they allow direct access from a link that may have public access to an original file that you may want protected. Links are usually implemented as symbolic links.

Symbolic Links

To set up a symbolic link, you use the ln command with the -s option and two arguments: the name of the original file and the new, added filename. The ls operation lists both filenames, but only one physical file will exist.

$ ln -s original-file-name added-file-name

In the next example, the today file is given the additional name weather. It is just another name for the today file.

$ ls
today
$ ln -s today weather
$ ls
today weather

You can give the same file several names by using the ln command on the same file many times. In the next example, the file today is given both the names weather and weekend:

$ ln -s today weather
$ ln -s today weekend
$ ls
today weather weekend

If you list the full information about a symbolic link and its file, you will find the information displayed is different. In the next example, the user lists the full information for both lunch and /home/george/veglist using the ls command with the -l option. The first character in the line specifies the file type. Symbolic links have their own file type represented by an l. The file type for lunch is l, indicating it is a symbolic link, not an ordinary file. The number after the term "group" is the size of the file. Notice the sizes differ. The size of the lunch file is only four bytes. This is because lunch is only a symbolic link—a file that holds the pathname of another file—and a pathname takes up only a few bytes. It is not a direct hard link to the veglist file.

$ ls lunch /home/george/veglist
lrw-rw-r-- 1 chris group 4 Feb 14 10:30 lunch
-rw-rw-r-- 1 george group 793 Feb 14 10:30 veglist

To erase a file, you need to remove only its original name (and any hard links to it). If any symbolic links are left over, they will be unable to access the file. In this case, a symbolic link would hold the pathname of a file that no longer exists.

Hard Links

You can give the same file several names by using the ln command on the same file many times. To set up a hard link, you use the ln command with no -s option and two arguments: the name of the original file and the new, added filename. The ls operation lists both filenames, but only one physical file will exist.

$ ln original-file-name added-file-name

In the next example, the monday file is given the additional name storm. It is just another name for the monday file.

$ ls
today
$ ln monday storm
$ ls
monday storm

To erase a file that has hard links, you need to remove all its hard links. The name of a file is actually considered a link to that file—hence the command rm that removes the link to the file. If you have several links to the file and remove only one of them, the others stay in place and you can reference the file through them. The same is true even if you remove the original link—the original name of the file. Any added links will work just as well. In the next example, the today file is removed with the rm command. However, a link to that same file exists, called weather. The file can then be referenced under the name weather.

$ ln today weather
$ rm today
$ cat weather
The storm broke today
and the sun came out.
$
Note 

Each file and directory in Linux contains a set of permissions that determine who can access them and how. You set these permissions to limit access in one of three ways: You can restrict access to yourself alone, you can allow users in a predesignated group to have access, or you can permit anyone on your system to have access. You can also control how a given file or directory is accessed. A file and directory may have read, write, and execute permissions. When a file is created, it is automatically given read and write permissions for the owner, enabling you to display and modify the file. You may change these permissions to any combination you want. See Chapter 28 for more details).



Previous Section
 < Day Day Up > 
Next Section
This HTML Help has been published using the chm2web software.