Linux ls Command - List Files

Amir
Amir
Linux ls Command - List Files

The ls command lists the contents of a specified directory. In this post we explain all the options available for the ls command.

ls Commad Options

Options Description
-a List all entries including ones that start with a dot
-A List all entries excluding . and ..
-c Sort files by change time
-d List directory entries
-h Show sizes in human readable format (i.e. K, M)
-H Same as above only with powers of 1000 instead of 1024
-l Show contents in long-listing format
-o Long -listing format without group info
-r Show contents in reverse order
-s Print size of each file in blocks
-S Sort by file size
–sort Sort contents by a word. (i.e size, version, status)
-t Sort by modification time
-u Sort by last access time
-v Sort by version
-1 List one file per line


List Files

The ls command lists the contents of a specified directory, excluding dotfiles. If no directory is specified then, by default, the contents of the current directory are listed.

Listed files are sorted alphabetically, by default, and aligned in columns if they don’t fit on one line.

Example:

1
2
3
$ ls
apt     configs     Documents       Music       workspace
bin     Desktop     git             Pictures    Public      Videos

List Files in a Long Listing Format

The ls command’s -l option prints a specified directory’s contents in a long listing format. If no directory is specified then, by default, the contents of the current directory are listed.

1
ls -l /etc

Example Output:

1
2
3
4
5
total 1204
drwxr-xr-x  3 root root 4096 Apr 21 03:44 acpi
-rw-r--r--  1 root root 3028 Apr 21 03:38 adduser.conf
drwxr-xr-x  2 root root 4096 Jun 11 20:42 alternatives
...

List the Ten Most Recently Modified Files

The following will list up to ten of the most recently modified files in the current directory, using a long listing format (-l) and sorted by time (-t).

1
ls -lt | head

List All Files Including Dotfiles

A dotfile is a file whose names begin with a .. These are normally hidden by ls and not listed unless requested. For example the following output of ls won’t list the dot files:

1
2
$ ls
bin pki

The -a or --all option will list all files, including dotfiles.

Example:

1
2
3
$ ls -a
.   .ansible        .bash_logout    .bashrc
..  .bash_history   .bash_profile   bin     pki

List Files in a Tree-Like Format

The tree command lists the contents of a specified directory in a tree-like format. If no directory is specified then, by default, the contents of the current directory are listed.

Example Output:

1
2
3
4
5
6
$ tree /tmp
/tmp
├── 5037
├── adb.log
└── evince-20965
    └── image.FPWTJY.png

Use the tree command’s -L option to limit the display depth and the -d option to only list directories.

Example Output:

1
2
$ tree -L 1 -d /tmp /tmp
└── evince-20965

List Files Sorted by Size

The ls command’s -S option sorts the files in descending order of file size.

1
2
3
4
5
$ ls -l -S ./Fruits
total 8
-rw-rw-rw- 1 root root 166703 Jan 28 00:09 apples.jpg 
-rw-rw-rw- 1 root root 134270 Jan 28 00:09 kiwis.jpg 
-rw-rw-rw- 1 root root 30134 Jan 28 00:09 bananas.jpg

When used with the -r option the sort order is reversed.

1
2
3
4
5
$ ls -l -S -r ./Fruits
total 8
-rw-rw-rw- 1 root root 30134 Jan 28 00:09 bananas.jpg 
-rw-rw-rw- 1 root root 134270 Jan 28 00:09 kiwis.jpg 
-rw-rw-rw- 1 root root 166703 Jan 28 00:09 apples.jpg

Conclusion

In this post we learned all the different options to list files using the ls command.

See also