Working with Directories


This section covers deleting directories, copying and moving files between directories, and moving directories. It also describes how to use pathnames to make your work with Linux easier.

rmdir: Deletes a Directory

The rmdir (remove directory) utility deletes a directory. You cannot delete the working directory or a directory that contains files other than the . and .. entries. If you need to delete a directory that has files in it, first use rm to delete the files and then delete the directory. You do not have to (nor can you) delete the . and .. entries; rmdir removes them automatically. The following command deletes the promo directory:

$ rmdir /home/alex/literature/promo


The rm utility has a r option (rm r filename) that recursively deletes files, including directories, within a directory and also deletes the directory itself.

Caution: Use rm r carefully, if at all

Although rm r is a handy command, you must use it carefully. Do not use it with an ambiguous file reference such as *. It is frighteningly easy to wipe out your entire home directory with a single short command.


Using Pathnames

touch


Use a text editor to create a file named letter if you want to experiment with the examples that follow. Alternatively you can use touch to create an empty file:

$ cd $ pwd /home/alex $ touch letter


With /home/alex as the working directory, the following example uses cp with a relative pathname to copy the file letter to the /home/alex/literature/promo directory (you will need to create promo again if you deleted it earlier). The copy of the file has the simple filename letter.0610:

$ cp letter literature/promo/letter.0610


If Alex does not change to another directory, he can use vim to edit the copy of the file he just made:

$ vim literature/promo/letter.0610


If Alex does not want to use a long pathname to specify the file, he can use cd to make promo the working directory before using vim:

$ cd literature/promo $ pwd /home/alex/literature/promo $ vim letter.0610


To make the parent of the working directory (named /home/alex/literature) the new working directory, Alex can give the following command, which takes advantage of the .. directory entry:

$ cd .. $ pwd /home/alex/literature


mv, cp: Move or Copy Files

Chapter 5 discussed the use of mv to rename files. However, mv works even more generally: You can use this utility to move files from one directory to another (change the pathname of a file) as well as to change a simple filename. When used to move one or more files to a new directory, the mv command has this syntax:


mv existing-file-list directory

If the working directory is /home/alex, Alex can use the following command to move the files names and temp from the working directory to the literature directory:

$ mv names temp literature


This command changes the absolute pathnames of the names and temp files from /home/alex/names and /home/alex/temp to /home/alex/literature/names and /home/alex/literature/temp, respectively (Figure 6-11). Like most Linux commands, mv accepts either absolute or relative pathnames.

Figure 6-11. Using mv to move names and temp


As you work with Linux and create more files, you will need to create new directories using mkdir to keep the files organized. The mv utility is a useful tool for moving files from one directory to another as you extend your directory hierarchy.

The cp utility works in the same way as mv does, except that it makes copies of the existing-file-list in the specified directory.

mv: Moves a Directory

Just as it moves ordinary files from one directory to another, so mv can move directories. The syntax is similar except that you specify one or more directories, not ordinary files, to move:


mv existing-directory-list new-directory

If new-directory does not exist, the existing-directory-list must contain just one directory name, which mv changes to new-directory (mv renames the directory). Although you can rename directories using mv, you cannot copy their contents with cp unless you use the r option. Refer to the tar and cpio man pages for other ways to copy and move directories.




A Practical Guide to Red Hat Linux
A Practical Guide to Red HatВ® LinuxВ®: Fedoraв„ў Core and Red Hat Enterprise Linux (3rd Edition)
ISBN: 0132280272
EAN: 2147483647
Year: 2006
Pages: 383

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net