Now that we have covered some of these commands in an "isolated" fashion, let's put some of the commands together.
Let's start by viewing the hierarchy of a directory under denise's home directory, called krsort.dir.old, as shown in Figure 21-7:
$ cd /home/denise/krsort.dir.old $ pwd /home/denise/krsort.dir.old $ ls -l total 168 -rwxr-xr-x 1 denise users 34592 Oct 27 18:20 krsort -rwxr-xr-x 1 denise users 3234 Oct 27 17:30 krsort.c -rwxr-xr-x 1 denise users 32756 Oct 27 17:30 krsort.dos -rw-r--r-- 1 denise users 9922 Oct 27 17:30 krsort.q -rwxr-xr-x 1 denise users 3085 Oct 27 17:30 krsortorig.c $
We can then make a new directory called krsort.dir.new and copy a file to it as shown in Figure 21-8:
$ mkdir ../krsort.dir.new $ cp krsort ../krsort.dir.new $ ls -l ../krsort.dir.new total 68 -rwxr-xr-x 1 denise users 34592 Oct 27 18:27 krsort $
Now let's try the -i option to cp. If we attempt to copy a file to an existing file name, we'll be asked if we wish to overwrite the destination file. We are alerted to the fact that the destination file already exists and we can then select a new name for the file we wish to copy, as shown in Figure 21-9:
$ pwd /users/denise/krsort.dir.old $ cp -i krsort ../krsort.dir.new overwrite ../krsort.dir.new/krsort? (y/n) n $ cp krsort ../krsort.dir.new/krsort.new.name $ ls -l ../krsort.dir.new total 136 -rwxr-xr-x 1 denise users 34592 Oct 27 18:27 krsort -rwxr-xr-x 1 denise users 34592 Oct 27 18:29 krsort.new.name $
We can also use a wild card with cp to copy all files in krsort.dir.old to krsort.dir.new as shown in Figure 21-10:
$ cp * ../krsort.dir.new $ ls -l ../krsort.dir.new total 236 -rwxr-xr-x 1 denise users 34592 Oct 27 18:30 krsort -rwxr-xr-x 1 denise users 3234 Oct 27 18:30 krsort.c -rwxr-xr-x 1 denise users 32756 Oct 27 18:30 krsort.dos -rwxr-xr-x 1 denise users 34592 Oct 27 18:29 krsort.new.name -rw-r--r-- 1 denise users 9922 Oct 27 18:30 krsort.q -rwxr-xr-x 1 denise users 3085 Oct 27 18:30 krsortorig.c $
Let's start over at the point where the krsort.dir.new directory is empty, as shown in Figure 21-11:
We can now move the file krsort to the krsort.dir.new directory as shown in Figure 21-12:
$ mv krsort ../krsort.dir.new $ ls -l ../krsort.dir.new total 68 -rwxr-xr-x 1 denise users 34592 Oct 27 18:20 krsort $
If we now attempt to move krsort to the krsort.dir.new directory with the -i option and write over the file krsort, we get the following:
Because we used the -i option to mv, we are asked whether we wish to allow a file to be overwritten with the move. Because we responded n to the question, the file is not overwritten.
We can also use a wild card with the mv command to copy all files from the krsort.dir.old directory to the krsort.dir.new directory. Without the -i option, any files in the krsort.dir.new directory are overwritten by files that have the same name, as shown in Figure 21-13:
$ mv * ../krsort.dir.new $ ls -l ../krsort.dir.new total 168 -rwxr-xr-x 1 denise users 34592 Oct 27 18:44 krsort -rwxr-xr-x 1 denise users 3234 Oct 27 18:46 krsort.c -rwxr-xr-x 1 denise users 32756 Oct 27 18:46 krsort.dos -rw-r--r-- 1 denise users 9922 Oct 27 18:46 krsort.q -rwxr-xr-x 1 denise users 3085 Oct 27 18:46 krsortorig.c $
The most feared command in the UNIX world, with good reason I might add, is the rm command. rm removes whatever you want whenever you want, with no questions asked unless you use the -i option.
Want to blow away your system instantly? rm would be more than happy to help you. As an average user, and not the system administrator, you probably do not have the permissions to do so. It is, however, unnerving to know that this is a possibility. In addition, it is likely that you have permissions remove all of your own files and directories. All of this can be easily avoided by simply using the -i option to rm.
Let's assume that krsort.dir.new and krsort.dir.old are identical directories as shown in Figure 21-14:
To interactively remove files from krsort.dir.new, you do the following:
$ rm -i ../krsort.dir.new/* ../krsort.dir.new/krsort: ? (y/n) n ../krsort.dir.new/krsort.c: ? (y/n) n ../krsort.dir.new/krsort.dos: ? (y/n) n ../krsort.dir.new/krsort.q: ? (y/n) n ../krsort.dir.new/krsortorig.c: ? (y/n) n $ ls -l ../krsort.dir.new total 168 -rwxr-xr-x 1 denise users 34592 Oct 27 18:44 krsort -rwxr-xr-x 1 denise users 3234 Oct 27 18:46 krsort.c -rwxr-xr-x 1 denise users 32756 Oct 27 18:46 krsort.dos -rw-r--r-- 1 denise users 9922 Oct 27 18:46 krsort.q -rwxr-xr-x 1 denise users 3085 Oct 27 18:46 krsortorig.c $
This obviously resulted in nothing being removed from krsort.dir.new because we responded n when asked whether we wanted to delete files.
Let's now go ahead and add a file beginning with a "." (period) to krsort.dir.new. The touch command does just that, it touches a file to create it with no contents, as shown in Figure 21-15 (touch can also be used to update the time stamp of an existing file):
$ touch ../krsort.dir.new/.dotfile $ ls -al ../krsort.dir.new total 172 drwxr-xr-x 2 denise users 1024 Oct 27 18:54 . drwxrwxr-x 4 denise users 1024 Oct 27 18:40 .. -rw-r--r-- 1 denise users 0 Oct 27 18:56 .dotfile -rwxr-xr-x 1 denise users 34592 Oct 27 18:44 krsort -rwxr-xr-x 1 denise users 3234 Oct 27 18:46 krsort.c -rwxr-xr-x 1 denise users 32756 Oct 27 18:46 krsort.dos -rw-r--r-- 1 denise users 9922 Oct 27 18:46 krsort.q -rwxr-xr-x 1 denise users 3085 Oct 27 18:46 krsortorig.c $
If we now attempt to remove files using the same rm command earlier issued, we'll see the following, as shown in Figure 21-16:
$ rm -i ../krsort.dir.new/* ../krsort.dir.new/krsort: ? (y/n) y ../krsort.dir.new/krsort.c: ? (y/n) y ../krsort.dir.new/krsort.dos: ? (y/n) y ../krsort.dir.new/krsort.q: ? (y/n) y ../krsort.dir.new/krsortorig.c: ? (y/n) y $ ls -al ../krsort.dir.new total 4 drwxr-xr-x 2 denise users 1024 Oct 27 18:57 . drwxrwxr-x 4 denise users 1024 Oct 27 18:40 .. -rw-r--r-- 1 denise users 0 Oct 27 18:56 .dotfile
The "*" used as a wild card with the rm command does not remove the file .dotfile. The file .dotfile in this directory prevents the rmdir command from removing krsort.dir.new. This file must first be removed before the rmdir command can successfully delete krsort.dir.new.
$ rmdir -i ../krsort.dir.new ../krsort.dir.new: ? (y/n) y rmdir: ../krsort.dir.new: Directory not empty $ rm ../krsort.dir.new/.dotfile $ rmdir -i ../krsort.dir.new ../krsort.dir.new: ? (y/n) y $
rmdir has now successfully removed krsort.dir.new because .dotfile is gone, as shown in Figure 21-17:
18.219.213.196