Previous | Table of Contents | Next |
Creating Parent Directories
Sometimes when you want to create a directory, its parent directory or directories might not exist. In this case, mkdir issues an error message. Here is an illustration of this:
$ mkdir /tmp/ch04/test1 mkdir: Failed to make directory "/tmp/ch04/test1"; No such file or ⇒directory
In such cases, you can specify the -p (p as in parent) option to the mkdir command. It creates all the necessary directories for you. For example
$ mkdir -p /tmp/ch04/test1
creates all the required parent directories.
The mkdir command uses the following procedure to create the requested directory:
Common Errors
The most common error in using mkdir is trying to make a directory that already exists. If the directory /tmp/ch04 already exists, the command
$ mkdir /tmp/ch04
generates an error message similar to the following:
mkdir: cannot make directory '/tmp/ch04': File exists
An error also occurs if you try to create a directory with the same name as a file. For example, the following commands
$ ls -F docs/names.txt names $ mkdir docs/names
result in the error message
mkdir: cannot make directory 'docs/names': File exists
If you specify more than one argument to mkdir, it creates as many of these directories as it can. Any directory that could not be created generates an error message.
In Chapter 3, you looked at using the cp command to copy files. Now look at using it to copy directories.
To copy a directory, you specify the -r option to cp. The syntax is as follows:
cp -r source destination
Here, source is the pathname of the directory you want to copy, and destination is where you want to place the copy. For example
$ cp -r docs/book /mnt/zip
copies the directory book located in the docs directory to the directory /mnt/zip. It creates a new directory called book under /mnt/zip.
Copying Multiple Directories
In the same way that you can copy multiple files with cp, you can also copy multiple directories. If cp encounters more than one source, all the source directories are copied to the destination. The destination is assumed to be the last argument.
For example, the command
$ cp -r docs/book docs/school work/src /mnt/zip
copies the directories school and book, located in the directory docs, to /mnt/zip. It also copies the directory src, located in the directory work, to /mnt/zip. After the copies finish, /mnt/zip looks like the following:
$ ls -aF /mnt/zip ./ ../ book/ school/ src/
You can also mix files and directories in the argument list. For example
$ cp -r .profile docs/book .kshrc doc/names work/src /mnt/jaz
copies all the requested files and directories to the directory /mnt/jaz.
If your argument list consists only of files, the -r option has no effect.
Common Errors
The most common error in copying files and directories is in the requested destination. The most common problems in copying directories involve using a destination that is not a directory.
An example of this is
$ cp -r docs /mnt/zip/backup cp: cannot create directory '/mnt/zip/backup': File exists $ ls -F /mnt/zip/backup /mnt/zip/backup
As you can see, the cp operation fails because a file called /mnt/zip/backup already exists.
You have looked at the mv command to rename files, but its real purpose is to move files and directories between different locations in the directory tree. The basic syntax is this:
mv source destination
Here source is the name of the file or directory you want to move, and destination is the directory where you want the file or directory to end up. For example
$ mv /home/ranga/names /tmp
moves the file names located in the directory /home/ranga to the directory /tmp.
Moving a directory is exactly the same:
$ mv docs/ work/
moves the directory docs into the directory work. To move the directory docs back to the current directory you can use the command:
$ mv work/docs .
One nice feature of mv is that you can move and rename a file or directory all in one command. For example
$ mv docs/names /tmp/names.txt
moves the file names in the directory docs to the directory /tmp and renames it names.txt.
Moving Multiple Items
As you can with cp, you can specify more than one file or directory as the source. For example
$ mv work/ docs/ .profile pub/
moves the directories work and docs along with the file .profile into the directory pub.
When you are moving multiple items, you cannot rename them. If you want to rename an item and move it, you must use a separate mv command for each item.
Previous | Table of Contents | Next |