Previous | Table of Contents | Next |
Common Errors
You can encounter three common errors with mv:
The first and second cases produce the same error message, so look at one example that illustrates what happens:
$ mv .profile docs pub /mnt/jaz/backup mv: when moving multiple files, last argument must be a directory $ ls -aF /mnt/jaz ./ ../ archive/ lost+found/ old/
As you can see, no directory named backup exists in the /mnt/jaz directory, so mv reports an error. The same error is reported if backup was a file in the /mnt/jaz directory.
The third case occurs when you try to move a directory from one file system to another. For the purposes of this book, you can think of a file system as either a hard drive or a hard drive partition.
assume that /home and /tmp are on separate partitions. In this case, the command
$ mv /tmp/ch01 /home/ranga/docs
returns error output
mv: cannot move '/tmp/ch01' across filesystems: Not a regular file
The most common workaround to this is to use the cp -r to copy the directory and then remove the original with rm:
$ cp -r /tmp/ch01 /home/ranga $ rm -r /tmp/ch01
I cover the -r option of rm later in this chapter.
Sometimes, you might use the tar (as in tape archive) command instead of cp:
$ ( cd /tmp ; tar -cvpf - ch01 | ( cd /home/ranga ; tar -xvpf -) ) $ rm -r /tmp/ch01
I explain the tar version of moving directories in Chapter 22, Problem Solving with Shell Scripts.
You can use two commands to remove directories:
Use the first command to remove empty directories. It is considered safe because in the worst case, you can accidentally lose an empty directory, which you can quickly re-create with mkdir.
The second command removes directories along with their contents. It is considered unsafe because in the worst case of rm -r, you could lose your entire system.
Caution:
When using rm to remove either files or directories, make sure that you remove only those files that you dont want.There is no way to restore files deleted with rm, so mistakes can be very hard to recover from.
rmdir
To remove an empty directory, you can use the rmdir command. Its syntax is
rmdir directories
Here, directories includes the names of the directories you want removed. For example, the command
$ rmdir ch01 ch02 ch03
removes the directories ch01, ch02, and ch03 if they are empty. The rmdir command produces no output if it is successful.
Common Errors
You might encounter two common error messages from rmdir. These occur when you
For the first case, you need to know how to determine whether a directory is empty. You can do this by using the -A option of the ls command. An empty directory produces no output. If there is some output, the directory you specified is not empty.
For example, if the directory bar is empty, the following command
$ ls -A bar
returns nothing.
Now say that the directory docs is not empty. The following command
$ rmdir docs
produces an error message
rmdir: docs: Directory not empty
To illustrate the second error, assume that names is a file. The following command
$ rmdir names
produces an error message
rmdir: names: Not a directory
rm -r
You can specify the -r option to rm to remove a directory and its contents. The syntax is as follows:
rm -r directories
Here directories includes the names of the directories you want removed.
For example, the command
$ rm -r ch01/
removes the directory ch01 and its contents. This command produces no output.
You can specify a combination of files and directories as follows:
$ rm -r ch01/ test1.txt ch01-old.txt ch02/
In order to make rm safer, you can combine the -r and -i options.
Common Errors
Usually the only error reported by rm is that a requested file or directory cannot be removed. If the file or directory midterm_answers does not exist, rm reports an error:
$ rm -r midterm_answers rm: midterm_answers: No such file or directory
In this chapter, you have looked at working with directories. Specifically, you covered the following topics:
You reviewed each of these topics because it is important to know how to perform these functions when writing shell scripts. As you progress further into this book, you see how common directory manipulations occur in shell scripts.
$ cd /usr/local $ cd bin $ cd ../../tmp $ cd
$ ls -a backup ./ ../ sysbak-980322 sysbak-980112
Previous | Table of Contents | Next |