The ls command is one of the most frequently used commands in Linux. In fact, you will use this command almost every time you get into the Linux terminal.
In this article, I will explain how to use the ls command and some of the most important flags in it that you’ll need day-to-day.
How to use the ls command in Linux
The general syntax for the ls command is as follows:
ls [flags] [directory]
The ls command requires one directory as an argument to show the content of the directory and some flags to control the output. When used with no flag and argument, the ls command displays the list of the name of all the files and subdirectories of the current directory.
ls
The files and directories are listed in alphabetical order and fitted column-wise.
Desktop Downloads Music Public Templates
Documents local-pass.txt Pictures snap Videos
You can also view the content of any directory without actually moving to that directory. The below example will list the content of the ‘var’ directory.
ls /var/
There are some shortcuts that help you to view the contents of some special directories without typing the actual directory path –
ls /
– shows the content of the root directory.ls ~
– shows the content of the users’ home directory.ls ..
– list the contents of one directory above.ls ../..
– list the cotents of two directoy levels above.
If you want to see only the subdirectories inside a directory then use the flag -d
followed by */
ls -d */
Generally, the ls
command lists only files and subdirectories but doesn’t list the content inside the subdirectories. To list the contents of a directory with the contents of its subdirectories use the ls
followed by *
.
ls *
The ls
command doesn’t include the hidden file in the results. In the Linux system, any file that starts with a .
is a hidden file (e.g .temp). To list files and directories including hidden files and directories use the flag -a
ls -a
View Files and Directories in Long Listing Format
The default output of the ls
command only shows the name of the files and directories. Sometimes that is not very useful.
You can see more information about the files and directories using the flag -l
(lowercase L). It will print the output in a long listing format that shows – the file type, file permissions, number of hard links to the file, file owner, file group, file size, date & time, and file name.
ls -l /Downloads/
Type the above command and you will gate an output like below.
root@ubuntu:~$ ls -l
total 40
drwxr-xr-x 3 root root 4096 Mar 11 09:14 Desktop
drwxr-xr-x 5 root root 4096 Mar 12 05:43 Documents
drwxr-xr-x 3 root root 4096 Mar 9 21:43 Downloads
-rw-rw-r-- 1 root root 125 Jul 10 2021 local-pass.txt
drwxr-xr-x 2 root root 4096 Jul 9 2021 Music
drwxr-xr-x 2 root root 4096 Jul 9 2021 Pictures
drwxr-xr-x 2 root root 4096 Jul 9 2021 Public
drwx------ 3 root root 4096 Jul 10 2021 snap
drwxr-xr-x 2 root root 4096 Jul 9 2021 Templates
drwxr-xr-x 2 root root 4096 Jul 9 2021 Videos
Look at a single line. For example, take the second line –
drwxr-xr-x 5 root root 4096 Mar 12 05:43 Documents
The first character shows the file type. Here the first character is d
, which indicates that it is a directory. List of different characters to indicate the file type is given below:
-
– Regular file.b
– Block special file.c
– Character special file.d
– Directory.l
– Symbolic link.n
– Network file.p
– FIFO.s
– Socket.
After that, you have nine characters like – rwxr-xr-x
.It shows the file permission for the Owner, Group, and Public (Others). The first three characters are for the file owner, the next three characters are for the group and the last three are for the public. The permission is defined using four characters – r, w, x, and – (a single dash).
- r – read permission for the file.
- w – write permission for the file.
- x – execute permission for the file.
In our example, rwxr-xr-x
means the owner has the read, write, and execute permission. Group and Public have the write and execute permission.
After that, you have the number 5 which shows the number of hard links to the file or directory. The next two fields root root
shows the Owner and Group of the directory. Then you have the directory size in bytes followed by the last file modification date and time and the directory name.
You can combine the flag -a
and -l
to list files in a long format including hidden files. you can write in any order like below and it will give the same result.
ls -l -a
or ls -a -l
or ls -la
or ls -al
The below command will list the files and directories of the ‘Downloads’ folder in a long listing format including the hidden files and directories (if any).
ls -al /Downloads/
Shorting Outputs
By default the ls command shorts the output by alphabetical order and fitted column-wise. you can reverse the order using the flag -r
ls -r /Downloads/
You can short the file by different aspects like – extension, size, time, and version.
You can use the flag --sort=PARAMETER
or you can use different short flags respective to each parameter to shorten the files and folders.
ls --sort=extension
(orls -X
) – sort by extension.ls --sort=size
(orls -S
) – sort by file size.ls --sort=time
( orls -t
) – sort by modification time.ls --sort=version
(orls -v
) – natural sort of version numbers.
You can use the flag -r
with these flags to reverse the file order. For example, you want to sort by file size in reverse order use the below command.
ls -rS /Downloads/
Store the Output in a Text File
The ls
command also has an option to store the output in a text file. To store the output in a text file type the ls
command as you like and then use >
followed by a name.
The below example will save the list of the files and drectories in the ‘Downloads’ folder in files-list.txt
fle.
ls /Downloads/ > files-list.txt