start page | rating of books | rating of authors | reviews | copyrights

UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 21.11 Safer Removing, Moving, and Copying Chapter 21
More About Managing Files
Next: 21.13 Read an Inode with stat
 

21.12 Copying Files to a Directory

Several commands like cp and mv will let you copy a file to a directory. That is, given the command:

% cp file1 somewhere

if somewhere is a directory, cp copies file1 into the directory, leaving its name unchanged. You get a new file whose relative pathname (1.21) is somewhere/file1. A few commands, for example, mv and ln, have the same behavior.

Of course, this version of the command looks the same as a "regular" cp command. This leads to a common frustration: what if the directory somewhere doesn't exist? Maybe you forgot to create it; maybe you misspelled the name. cp doesn't know that you really meant a directory, so it just copies file1 into a new file, somewhere, in the current directory. There are plenty of situations in which this can be plenty confusing, and even (if you're unlucky) lead to errors.

There's an easy safeguard, though. If you're copying files into a directory, add a slash and dot (/.) after the directory's pathname:

% cp file1 path-to-directory/.

This makes a pathname to the special entry named . (dot) (1.21, 18.2) in the directory somewhere-which is a link to the directory itself. If the directory named somewhere doesn't exist, you'll get an error message:

% cp file1 somewhere/.
cp: somewhere/.: No such file or directory

- ML


Previous: 21.11 Safer Removing, Moving, and Copying UNIX Power ToolsNext: 21.13 Read an Inode with stat
21.11 Safer Removing, Moving, and Copying Book Index21.13 Read an Inode with stat

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System