ls Linux Command for File and Directory List

Main picture of the post: background with a ls command executed and tittle ls linux command for file and directory list.

The ls command is one of the most basic in Linux and serves for listing files and directories in the OS. In this post it will describe the command options and how to use it with some use cases.

The ls syntax command

The syntax of the command is the following:

ls [OPTION]… [FILE]…

Some of the most typical options used in the ls command are:

-l : Use a long listing format

r : Reverse order while sorting

t : Sort by modification time, newest first

-a : Do not ignore entries starting with “.”

-h : With -l, print sizes in human readable format

-R : list subdirectories recursively

The options mentioned above and many others that ls has, can be combined in the same command execution like:

$ ls -la

And the option order doesn’t matter so the next command will trough the same output as the above:

$ ls -al

Running ls without options or parameters

When running without as is without any options it will return a list of files and directories without newlines ordered alphabetically:

$ ls
a  test0  test1  test2  test3

This could be useful to get quickly the list from the current folder, or use it as an input of other command or script without the need to format the output too much.

Get the list one file per line

To get a one file per line formatted list, the “-l” option is used:

$ ls -l
total 20
-rw-rw-r--. 1 fse fse 2 Apr 11 22:25 a
-rw-rw-r--. 1 fse fse 5 Apr 11 22:24 test0
-rw-r--r--. 1 fse fse 5 Apr 11 22:01 test1
-rw-r--r--. 1 fse fse 5 Apr 11 22:01 test2
-rw-rw-r--. 1 fse fse 6 Apr 11 22:25 test3

Note that the “total 8” from the output means the number blocks taken by the files listed. The other details of the output will be explained at the end of the post.

Get the list ordered by modification timestamp from newer to older

If you want to get a list ordered by file or folder modification timestamp, being the newest the first item, then use the “-t”:

$ ls -t
a  test3  test0  test1  test2

Combined with the “-l” option, you’ll get the list of files one per file ordered by modification date:

$ ls -tl
total 20
-rw-rw-r--. 1 fse fse 2 Apr 11 22:25 a
-rw-rw-r--. 1 fse fse 6 Apr 11 22:25 test3
-rw-rw-r--. 1 fse fse 5 Apr 11 22:24 test0
-rw-r--r--. 1 fse fse 5 Apr 11 22:01 test1
-rw-r--r--. 1 fse fse 5 Apr 11 22:01 test2

Get an ordered list by modification timestamp from older to newer with reverse option

The “-r” reverts the sorting option used by ls so, if running with only the “-r” it will output a list alphabetically in reverse order:

$ ls -r
test3  test2  test1  test0  a

This is applicable with the “-tl” described above so, combining both, it will output the list ordered by modification date from older to newer, one file per line:

$ ls -tlr
total 20
-rw-r--r--. 1 fse fse 5 Apr 11 22:01 test2
-rw-r--r--. 1 fse fse 5 Apr 11 22:01 test1
-rw-rw-r--. 1 fse fse 5 Apr 11 22:24 test0
-rw-rw-r--. 1 fse fse 6 Apr 11 22:25 test3
-rw-rw-r--. 1 fse fse 2 Apr 11 22:25 a

Run ls to get the hidden files and folders

The ls command output by default will not show any hidden files (Those whose name starts with “.”). In order to list them, it needs the option “-a”:

$ ls -a
.  ..  a  .hidden  test0  test1  test2  test3

Using previous “-l” option, a more detailed output can be obtained:

$ ls -la
total 24
drwxrwxr-x. 2 fse fse 82 Apr 11 22:44 .
drwx------. 3 fse fse 94 Apr 11 22:40 ..
-rw-rw-r--. 1 fse fse  1 Apr 11 22:43 a
-rw-rw-r--. 1 fse fse  5 Apr 11 22:29 .hidden
-rw-rw-r--. 1 fse fse  1 Apr 11 22:42 test0
-rw-rw-r--. 1 fse fse  1 Apr 11 22:41 test1
-rw-rw-r--. 1 fse fse  1 Apr 11 22:44 test2
-rw-rw-r--. 1 fse fse  1 Apr 11 22:43 test3

Note that in these executions, there are 3 additional items showing: “.”, “..” and “.hidden”. For more clarification, the “.” means the current directory where the ls command is executed, the “..” is the parent directory containing the current directory and the “.hidden” it’s a file that does not appear with normal ls execution.

Run ls recursively from the current path

The “-R” option, will help to obtain all the files from the path where the ls is running in a single execution. This means that all the folders and subfolders from the current path will be listed in one ls command execution, and could be useful to search a particular file or directory without the need to change into each directory and running on them the command each time:

$ ls -R
.:
com

./com:
a  test0  test1  test2  test3

Again, this can be combined with previous commented options:

$ ls -Ral
.:
total 16
drwx------. 3 fse  fse   94 Apr 11 22:40 .
drwxr-xr-x. 4 root root  32 Apr 11 22:01 ..
-rw-------. 1 fse  fse   10 Apr 11 22:05 .bash_history
-rw-r--r--. 1 fse  fse   18 Nov 24 17:33 .bash_logout
-rw-r--r--. 1 fse  fse  193 Nov 24 17:33 .bash_profile
-rw-r--r--. 1 fse  fse  231 Nov 24 17:33 .bashrc
drwxrwxr-x. 2 fse  fse   82 Apr 11 22:44 com

./com:
total 24
drwxrwxr-x. 2 fse fse 82 Apr 11 22:44 .
drwx------. 3 fse fse 94 Apr 11 22:40 ..
-rw-rw-r--. 1 fse fse  1 Apr 11 22:43 a
-rw-rw-r--. 1 fse fse  5 Apr 11 22:29 .hidden
-rw-rw-r--. 1 fse fse  1 Apr 11 22:42 test0
-rw-rw-r--. 1 fse fse  1 Apr 11 22:41 test1
-rw-rw-r--. 1 fse fse  1 Apr 11 22:44 test2
-rw-rw-r--. 1 fse fse  1 Apr 11 22:43 test3

List a target path

Until now, all the examples was performed in the current directory. So, in order to list other path than the current, you will add an absolute path or relative path where you want to run the ls command at the end:

Absolute path:

$ ls /home/fse/com
a  test0  test1  test2  test3  text

Relative path:

$ ls com
a  test0  test1  test2  test3  text

Combined with other options:

$ ls -lRa /home/fse
/home/fse:
total 20
drwx------. 3 fse  fse  110 Apr 12 00:18 .
drwxr-xr-x. 4 root root  32 Apr 11 22:01 ..
-rw-------. 1 fse  fse   10 Apr 11 22:05 .bash_history
-rw-r--r--. 1 fse  fse   18 Nov 24 17:33 .bash_logout
-rw-r--r--. 1 fse  fse  193 Nov 24 17:33 .bash_profile
-rw-r--r--. 1 fse  fse  231 Nov 24 17:33 .bashrc
drwxrwxr-x. 2 fse  fse   94 Apr 12 00:22 com
-rw-------. 1 fse  fse  626 Apr 12 00:18 .viminfo

/home/fse/com:
total 28
drwxrwxr-x. 2 fse fse   94 Apr 12 00:22 .
drwx------. 3 fse fse  110 Apr 12 00:18 ..
-rw-rw-r--. 1 fse fse    6 Apr 12 00:21 a
-rw-rw-r--. 1 fse fse    5 Apr 11 22:29 .hidden
-rw-rw-r--. 1 fse fse    6 Apr 12 00:21 test0
-rw-rw-r--. 1 fse fse    6 Apr 12 00:21 test1
-rw-rw-r--. 1 fse fse    6 Apr 12 00:21 test2
-rw-rw-r--. 1 fse fse    6 Apr 12 00:21 test3
-rw-rw-r--. 1 fse fse 1697 Apr 12 00:19 text

Obtaining the file size in a human readable format

The option “-h” can provide these details but it has to be combined with the “-l” option to obtain the file size in a human readable format:

$ ls -lh
total 24K
-rw-rw-r--. 1 fse fse    6 Apr 12 00:21 a
-rw-rw-r--. 1 fse fse    6 Apr 12 00:21 test0
-rw-rw-r--. 1 fse fse    6 Apr 12 00:21 test1
-rw-rw-r--. 1 fse fse    6 Apr 12 00:21 test2
-rw-rw-r--. 1 fse fse    6 Apr 12 00:21 test3
-rw-rw-r--. 1 fse fse 1.7K Apr 12 00:19 text

In the previous output, the rest of the files consumes 6 bytes of size while the “text” file consumes 1.7 K. The file size number could end in “M” or “G”.

The ls columns output meaning

When you run the ls command with the “-l” option, it will show additional details of the file. Here you can find out the explanation of the details:

$ ls -l
total 4
drwxrwxr-x. 2 fse fse 94 Apr 12 00:22 com
-rw-rw-r--. 1 fse fse  5 Apr 12 00:43 text

In the other hand, the output shows the following:

  • The first “-” means that it’s a file. In the other hand, a “d” would mean that it is a directory.
  • Next, the “rw-rw-r–.” means the permissions set of the file.
  • The number indicates how many links refers to the file. In the case of directories, the number of links and files.
  • The two “fse” corresponds to user and group that owns the file.
  • The following number tells about the size of the file.
  • The date and hour corresponds to the date of the file last modification
  • Finally, the name of the file.

List files that matches a pattern on their names

By using the wildcard “*”, you can list the files that matches a pattern:

$ ls
appended  file1  file2  text1  text2  text3
$ ls fil*
file1  file2

The absolute or full path of ls command

The command is normally located in the path /bin/ls

$ which ls
alias ls='ls --color=auto'
        /bin/ls
$ ls -ltr /bin/ls
-rwxr-xr-x. 1 root root 117608 Nov 16  2020 /bin/ls