|< Day Day Up >|
Use a text editor to create a file named letter if you want to experiment with the examples that follow. Or you can use touch to create an empty file:
$ 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. The copy of the file has the simple filename letter.0610:
$ pwd /home/alex $ 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 calling vim:
$ cd literature/promo $ pwd /home/alex/literature/promo $ vim letter.0610
To make the parent of the working directory (named /home/alex/literature) become the new working directory, Alex can give the following command, which takes advantage of the . . directory entry:
$ cd .. $ pwd /home/alex/literature
~ (Tilde) in pathnames
The shell expands the characters ~/ (a tilde followed by a slash) at the start of a pathname into the pathname of your home directory. Using this shortcut, you can display your .bashrc startup file (page 258) with the following command, no matter which directory is your working directory:
$ less ~/.bashrc
A tilde quickly references paths that start with your or someone else's home directory. The shell expands a tilde followed by a login name at the beginning of a pathname into the pathname of that user's home directory. Assuming he has permission to do so, Alex can examine Scott's ~/.bashrc file with the following command:
$ less ~scott/.bashrc
Refer to "Tilde expansion" on page 326 for more information.
mv, cp: Moves or Copies a File
Chapter 3 discussed the use of mv to rename files. However, mv is more general than that: 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 4-11). Like most Linux commands, mv accepts either absolute or relative pathnames.
Figure 4-11. Using mv to move names and temp
As you work with Linux and create more and more files, you will need to create 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 develop your file tree. The cp utility works in the same way that mv does, but 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 directories can be renamed using mv, their contents cannot be copied with cp unless you use the r option. Refer to the explanations of tar (page 786) and cpio (page 619) for other ways to copy and/or move directories.
|< Day Day Up >|