ls program lists information about files. On GNU/Linux everything is treated as a file. Even a directory is a file. While man page of
ls, which you can see by invoking
man ls on your terminal, summarizes the basic documentation the real documentation is in
coreutils info package which you can read by invoking
info coreutilson your terminal. Every command on GNU/Linux many take some options or arguments(also known as switches), sometimes mandatory and sometimes optional and at times there are sub-commands associated with each command.
ls has no mandatory arguments. When invoked without an argument,
ls prints files in the current directory. When you launch your terminal, it starts in your home directory. Full documentation of
ls can be read at https://www.gnu.org/software/coreutils/manual/coreutils.html#ls-invocation. Quick help on any command is typically accessible using either
--help. Here, I am presenting most common options with examples. Say you are in home directory and you type
ls then your output may be somewhat like below:
ls also accepts path as final argument. Consider we have an empty folder names
1 in our home directory. If we invoke
ls 1 from our home directory then it will have no output.
Hidden files and folders with ls
On GNU/Linux hidden files and folders start with a .(dot or period). To show these files you should use -a or –all arguments. Here, -a is short form of –all. If we perform ls -a on the empty folder 1 then it will show us two directories . and … Directory . always refers to current directory while directory
.. refers to parent directory with one exception. In the root directory i.e.
.. refers to same directory as
/. There is another switch for
ls which can be used to see hidden files and folders. It is
--almost-all. The only difference is that the latter switch does not show
On GNU/Linux backup files have
~ appended to filename as last character.
--ignore-backups will not list these backup files.
Types of files
--format=verbose lists name of each file, print the file type, file mode bits, number of hard links, owner name, group name, size, and timestamp, normally the modification timestamp. It prints question marks for information that cannot be determined.
Consider our folder
1 has a file called
ls -l if executed inside this directory will have following output:
Here, first character in the listing is
-, which specifies file type.
rw-rw-r-- permission bits,
1 is number of hard links, user
shiv owns this file, owned by group
shiv, sie is 0 bytes followed by modification timestamp and filename. We will learn about permission bits later let us first learn about various file types.
- refers to regular or ordinary file.
b refers to block special file typically used by storage devices.
c refers to character special file, for example sound card device.
C refers to high-performance or contiguous data file.
d refers to directory.
D door (Solaris 2.5 and up)
l refers to symbolic link. Think of these like short-cut files of Windows.
M off-line (“migrated”) file (Cray DMF)
n network special file (HP-UX)
p FIFO (named pipe)
P port (Solaris 10 and up)
? some other file type
The file mode bits listed are similar to symbolic mode specifications. But
ls combines multiple bits into the third character of each set of permissions as follows:
s If the set-user-ID or set-group-ID bit and the corresponding executable bit are both set.
S If the set-user-ID or set-group-ID bit is set but the corresponding executable bit is not set.
t If the restricted deletion flag or sticky bit, and the other-executable bit, are both set. The restricted deletion flag is another name for the sticky bit.
T If the restricted deletion flag or sticky bit is set but the other-executable bit is not set.
x If the executable bit is set and none of the above apply.
Sorting listing of files
Most useful sorts are on the basis of time or size. The commands are
ls -lt or
ls -lS will sort according to time or size. If you combine like
ls -lrt or
ls -lrS then the sort will be reversed. You can also sort on the basis of ctime or mtime using
On modern operating systems usually this is enabled. However, if your
ls does not give colored output then you can enable color with
Classification of files
--format=single-column list one file per line. This is the default for
ls when standard output is not a terminal.
--indicator-style=classifyappend a character to each file name indicating the file type. Also, for regular files that are executable, append
*. The file type indicators are
/ for directories,
@ for symbolic links,
| for FIFOs,
= for sockets,
> for doors, and nothing for regular files. You can try
ls -F /dev to see this in action.
Recursively listing a directory
You can use
-R switch to recursively list contents of a directory. Beware though if your directory has large no. of files and folders inside it then it may take a long time to print all that on terminal.
With this we have discussed most important uses of
ls command. For reference you can see the link given above of coreutils.