Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

add comment

3 Answers

up vote 14 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
5  
Pay very close attention when using rsync!!! The trailing slash really matters. –  Josh Oct 10 '12 at 19:48
7  
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
add comment

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
add comment

One good example is moving a file into a directory:

mv some_file foo

vs.

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
1  
@Josh: Thanks for the edit! –  Keith Thompson Oct 11 '12 at 18:42
add comment

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