1

Is there any difference between a directory name such as

mydirectory

and

mydirectory/

I noticed this happens when I execute ls in some directories - some of the directory names have a slash and some don't. This is problematic because if I want to access a file contained in a directory, I may need to add a slash at the end:

vi $mydirectory"/"$myfile

or simply do

vi $mydirectory$myfile

marked as duplicate by terdon Jan 20 '16 at 14:25

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.

  • 5
    This is a backslash: \ . This is a forward slash: / (or just slash). – cjm Jul 15 '15 at 20:13
  • 2
    Files can not have a slash / ever: there are only 2 banned characters in Unix file-name (/ and null). However sometimes a command like ls -F may add a / to the end of directory names. Sometimes you may choose to add them to the end of directory names to avoid ambiguity. E.g to dereference a symlink to directory. – ctrl-alt-delor Jul 15 '15 at 21:47
7

Without / it might also be a file.

In some situations it can be deadly. For example when using mv:

mv file1 mydirectory
mv file2 mydirectory
mv file3 mydirectory

All right? But if mydirectory did not exist or wasn't a directory, the final result is that file1 and file2 are gone and file3 is now named mydirectory.

mv file1 mydirectory/
mv file2 mydirectory/
mv file3 mydirectory/

If mydirectory did not exist, all you get is three error messages and file1, file2 and file3 are still there.

So the / removes some ambiguity.

Apart from that there aren't really any rules. Some programs may behave differently depending whether you supplied the / at the end or not. It's up to them what to make of it.

In some cases you also get problems if you use too many /. For example find keeps surplus / in its output, which might trip you up if you try to find file/pathnames using simple string comparisons instead of, say, realpath or something.

2

That would likely be a "display" thing with ls, i.e., if you do ls -p or ls -F, it should consistently add the slash after directories. It's essentially adding visual cues as to what it thinks the file is. (Do note that -F will also, e.g., append @ to symlinks, * to files with executable permissions, | to named pipes, = to sockets and so on). You can also reproduce with ls -d directory/ vs ls -d directory.

/ and \0 I believe is the only two characters not valid in a file/directory name.

Always adding the slash should be safe, if you've assigned mydirectory and myfile correctly, doing vim "$mydirectory/$myfile" should do what you expect. (With the caveat of $mydirectory and $myfile having been assigned correctly). foo/bar and foo//bar should be equivalent.

Generally; I really dislike assigning the output of ls to variables and dealing with it, primarily because I've previously been surprised about characters ending up into filenames combined with lacking quoting, ending up with destructive results. (I.e. prefix=/home user=" oops" rm -rf $prefix$user, which would end up recursively deleting /home, and then trying to recursively delete ./oops).

Instead of using ls, I try when I can to rely on glob expansion by bash, or find -print0 | xargs -0. If you do use ls, ensure you're NOT using an existing alias for ls, and you use the display options that fits your particular task.

0

If you think about directories or folders in the context of a "fancy filename" the folder is still a file but folder is rather a pointer to a container like a file folder in your filing cabinet, so if you denote or use a slash "/" or slash dot "/." it is helpful for a script to know an be sure you don't mean a file rather than a folder.

Hope this makes sense.

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