Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

unix

cp

I want to copy:

  • This folder: ajax (/home/thej/public_html/JC/ajax).
  • Into this folder: /home/thej/public_html/demo/conf/. The final result will be /home/thej/public_html/demo/conf/ajax.

I know the cp command should be something like:

cp -r /home/thej/public_html/JC/ajax /home/thej/public_html/demo/conf 

My question is:

  1. should I put / after ajax, ajax/?

  2. should I put / after conf, conf/?

I googled online, some put '/', some not, so really confused with it.

like image 942
user2507818 Avatar asked Aug 10 '13 02:08

user2507818


People also ask

Should I use trailing slash?

If your site has a directory structure, it's more conventional to use a trailing slash with your directory URLs (for example, example.com/directory/ rather than example.com/directory ), but you can choose whichever you like. Be consistent with the preferred version. Use it in your internal links.

Why would you use a slash at the end of the source path when using rsync?

A trailing slash on a source path means "copy the contents of this directory". Without a trailing slash it means "copy the directory".

What is trailing forward slash?

A trailing slash is a forward slash (“/”) placed at the end of a URL such as domain.com/ or domain.com/page/. The trailing slash is generally used to distinguish a directory which has the trailing slash from a file that does not have the trailing slash.

What is the difference between cp and mv command in Unix?

mv command in Unix: mv is used to move or rename the files but it will delete the original file while moving. cp command in Unix: cp is used to copy the files but like mv it's not delete the original file means original file remain as it is.


1 Answers

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 do something very surprising, since its behavior depends on the underlying rename system call. On a system with a modern Linux-based kernel, it fails with 'errno=ENOTDIR'. However, on other systems (at least FreeBSD 6.1 and Solaris 10) it silently renames not the symlink but rather 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 any trailing 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 and specify a symbolic link to a directory.  This scenario is in fact rather common because some shells can automatically append a trailing slash when performing file name completion on such symbolic links.  Without this option, `mv', for example, (via the system's rename function) must interpret a trailing slash as a request to dereference the symbolic link and so must rename the indirectly referenced _directory_ and not the symbolic link.  Although it may seem surprising that such behavior be the default, it is required by POSIX and is consistent with other parts of that standard. 
like image 84
Volker Siegel Avatar answered Sep 19 '22 15:09

Volker Siegel