2

In the man page for cp, what is the difference between copying to DEST and DIRECTORY?

cp copies a SOURCE to a DEST and SOURCES to a DIRECTORY.

   SYNOPSIS

   cp [OPTION]... [-T] SOURCE DEST
   cp [OPTION]... SOURCE... DIRECTORY
5

With DEST it is assuming you are naming the file name and path (and therefore directory) of the destination file. With DIRECTORY you are just listing which directory the destination file will be in, while preserving the SOURCE name.

2

If SOURCE is just one file, the difference depends on what the command finds: If there is a directory DEST, SOURCE is copied into it, preserving the name. If it does not exist or is a file, DEST is interpreted as new name to use, eventually overriding DEST if it is a file.

If you have multiple SOURCES, DEST can only be a directory, since copying multiple files into one dest file would only preserve the last copied file and be rather useless for almost all cases.

A copy command:

cp s1 s2 s3 d1 d2 d3 

would look as if it would make some sense, since an even number of names could match s1 -> d1, s2 -> d2 ... , but think of shell expansion:

cp * 

could result in different interpretation, depending on the number of files matched by *. So if you use multiple files, the last is always assumed to be a directory. If it doesn't exist, you get an error message.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.