Should I put trailing slash after source and destination when copy folders Should I put trailing slash after source and destination when copy folders unix unix

Should I put trailing slash after source and destination when copy folders


No trailing slash on source directory

You should not put a trailing slash on the source directory:

The point is relevant to cp - but also to mv, where it is much more important.

I'll cite the warning from the manual - note that it's not found in the man page, but in the info page info coreutils 'mv invocation':

Warning: Avoid specifying a source name with a trailing slash,when it might be a symlink to a directory. Otherwise, 'mv' may dosomething very surprising, since its behavior depends on the underlyingrename system call. On a system with a modern Linux-based kernel, itfails with 'errno=ENOTDIR'. However, on other systems (at leastFreeBSD 6.1 and Solaris 10) it silently renames not the symlink butrather the directory referenced by the symlink.

Use trailing slashes on destination directory

If you want to move files and directories into a directory, you should add a trailing slash to the destination directory.
It does not change anything as long as all goes well.
But it can save you from a common error that causes loosing or changing data:

If you move a file into a directory, there are two things that can go wrong:
You may have misspelled the last component of the destination directory name, like one wrong character.Two of the things that can happen are these two cases:

  • If the misspelled name does not exist:

    • the file will be renamed to the misspelled name

    • it gets moved into the parent directory of the intended destination

    • So it ends up neither were it was, not where you expect it to go.

  • If the misspelled name does exist, and is a file:

    • the file will overwrite the file with the misspelled name, in the parent directory of the destination.

    • Again, you will not find it were it was, or where it should be: it just vanished.

If you use a trailing slash on the destination directory, both cases are prevented:

It makes mv not use it as a file name when it normally would do. Instead, mv just gives an error:

mv: failed to access ‘foo/’: Not a directory

Automatically remove slashes

There is a special option for cp and mv to mitigate the risk by allways stripping the slash from src dirs:

Some GNU programs (at least `cp' and `mv') allow you to remove anytrailing slashes from each SOURCE argument before operating on it.  The`--strip-trailing-slashes' option enables this behavior.   This is useful when a SOURCE argument may have a trailing slash andspecify a symbolic link to a directory.  This scenario is in fact rathercommon because some shells can automatically append a trailing slashwhen performing file name completion on such symbolic links.  Withoutthis option, `mv', for example, (via the system's rename function) mustinterpret a trailing slash as a request to dereference the symbolic linkand so must rename the indirectly referenced _directory_ and not thesymbolic link.  Although it may seem surprising that such behavior bethe default, it is required by POSIX and is consistent with other partsof that standard.


I try to put a trailing / on the target.

If I make a mistake and the source and target are both files rather than directories, adding the / means that I'll get an error; without the / it will clobber the target file.

(Tab completion will usually add the trailing / anyway, so it's usually easier to add it than not.)

See Volker Siegel's answer for more details.