Ls: how to tell, by looking at its output, which is a directory link and which is a file link?

In this scenario, directory links and file links seem indistinguishable to me:

# touch 1
# mkdir 2
# mkdir 3
# mkdir 4
# cd 2
# ln -s ../3 3l
# ln -s ../4 4l
# ln -s ../1 1l
# ls -l
total 12
lrwxrwxrwx. 1 root root 4 Oct  3 09:22 1l -> ../1
lrwxrwxrwx. 1 root root 4 Oct  3 09:22 3l -> ../3
lrwxrwxrwx. 1 root root 4 Oct  3 09:22 4l -> ../4

Is there a way to tell which is which: that 1l is a file link and 2l and 3l are directory links?

I found this question: ls - How to determine if a soft link's target is a directory or a file? - Unix & Linux Stack Exchange to which the answer is to use -F, but from the man ls it is absolutely not clear what ‘classify’ means or what other affects it may cause, and also: cannot files have trailing slashes in their names?

ls -F is what I would use if I needed to do it using only ls.

That being said, is there a reason you need to do it with a single command?

The character / is reserved in almost all filesystems you are likely to come across on Linux.

3 Likes

I recall reading something about a command line option for trimming trailing slashes from names. For some reason, it registered with me as a sign that file names could have trailing slashes. I also vaguely recall weird names that could be created by Windows on Linux or the other way around. One cannot be too careful, in this world.

I use colored output while using ls. This distinguishes between files and folders.

About special characters, avoiding them is best practice. If you still need to manipulate them see the article below. Sometimes you need to manipulate file or folder-names created in Windows.

No!
The 'nix file systems use the / as a reserved character designating the directory structure. As such / can never be a part of a file or directory name but only a path separator.

If you are familiar with windows then you likely know that the \ is reserved in the same way by windows systems.

ls -lp

from man ls

-p, --indicator-style=slash
append / indicator to directories

2 Likes

Or ls -l without the -p.
This will show a drwxr-xr-x. or similar at the beginning of the line while ls -lp will add the / at the end of the line along with what is already shown.
ls -p will give the directory names with the trailing /.
ls on most systems also shows file and directory names in different colors so a quick glance will tell what it is. (directories blue, images pink, executables green, standard files white, compressed files red) with a black background on my system.

That doesn’t work in this case because the OP is trying to differentiate between symlinks to files and symlinks to directories. ls -l just shows them both as links. ls -lp or ls -F adds the trailing / as you noted.

Thanks.