Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Possible Duplicate:
How linux handles multiple path separators (/home////username///file)

Most commands I use in linux behave exactly the same whether I include the trailing slash / character on the end of a directory name or not.

For example:

ls /home/cklein
ls /home/cklein/

cp foo bar
cp foo/ bar/

When does this trailing slash matter? What is the semantic meaning of the trailing slash?

share|improve this question

marked as duplicate by Gilles, Renan, jasonwryan, warl0ck, jw013 Oct 11 '12 at 14:00

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

This is not a duplicate of unix.stackexchange.com/questions/1910/… – djsmith Jul 25 '14 at 17:24
up vote 26 down vote accepted

It is completely dependent on the tool. rm won't let you remove a symlink to a directory if there's a slash at the end, and rsync does different things if the remote file specification has a slash at the end.

share|improve this answer
Pay very close attention when using rsync!!! The trailing slash really matters. – Josh Oct 10 '12 at 19:48
For the most part, a trailing / means “treat this as a directory, dereference the symlink if it is one, complain if it isn't a directory”. This behavior is mandated for utilities specified by POSIX. Rsync's source argument is a notable exception. – Gilles Oct 10 '12 at 23:42
Note that by default, chown -R /dir1/symlink1 will not perform recursion when the target is a symbolic link; however, chown -R /dir1/symlink1/ will do what you would expect. – chrisfargen Nov 17 '13 at 8:08
It does depend on the tool, but the rm case is not a good example for this: The difference here is not related to rm. In rm ./symlinkToDir, symlinkToDir is a symlink. But in rm ./symlinkToDir/, symlinkToDir/ is the same as symlinkToDir/. by definition, and that is the directory the symlink points to. The chown example @chrisfargen is very similar. – Volker Siegel Jul 24 '14 at 4:29
Even though the OP asked specific for Linux, it may be worth noting that the OS or flavor of tools used may make a difference. One notable example is cp which has a different behavior when using the -r option on OS X (BSD cp) and Linux (GNU cp). cp -r src/ dest will only copy the contents of src into dest when using BSD cp but will copy the direcory src itself into dest when using GNU cp. (see jondavidjohn.com/linux-vs-osx-the-cp-command) – matlehmann Sep 17 '15 at 15:27

One good example is moving a file into a directory:

mv some_file foo


mv some_file foo/

If foo doesn't exist, the first will rename some_file to foo, rather than the intended foo/some_file; the second will complain, which is what you want.

If foo does exists but isn't a directory, the first can clobber the foo file; again, the second will complain.

cp presents similar issues.

Working on some old versions of SunOS, I developed the habit of appending /., because the system actually ignored a trailing / on a file name; for example, /etc/motd/ would refer to the file rather than being an error. Later versions of SunOS / Solaris don't seem to have that problem.

share|improve this answer
@Josh: Thanks for the edit! – Keith Thompson Oct 11 '12 at 18:42

foo/ is like foo/., so if foo is a symlink to a directory, foo/ is a directory (not a symlink), and if foo is not a directory or a symlink to a directory, then you get a ENOTDIR error for anything trying to access foo/. That's the behavior on Linux.

Behavior may differ on other systems.

See here and here and here to see what POSIX/SUS have to say about it.

share|improve this answer

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