# Sorting the linux ls command output

The ls command sort options are:

Option Sort
none by name, alphabetical order (1 < 11 < 2)
-r reverse order
-U no order
-v by name, naturel order (1 < 2 < 11)
-S by size, largest first
-X by extension, alphabetically
-t by last modification date (mtime), newer first
-c by last change date (ctime), newer first
-u by last access date (atime), newer first

# Alphabetical order (défaut)

The ls command sort file by alphabetical order by default:

> ls -l
total 28
-rw-rw-r--. 1 poc poc 20 May 19 13:56 file-111.txt
-rw-rw-r--. 1 poc poc  5 May 19 11:09 file-11.txt
-rw-rw-r--. 1 poc poc 20 May 19 13:55 file-1.txt
-rw-rw-r--. 1 poc poc 20 May 19 13:57 file-213.md
-rw-rw-r--. 1 poc poc 25 May 19 13:56 file-213.txt
-rw-rw-r--. 1 poc poc  2 May 19 11:09 file-21.txt
-rw-rw-r--. 1 poc poc 25 May 19 13:56 file-2.txt
1
2
3
4
5
6
7
8
9

TIP

The alphabetical order may not seem natural, especially when the filename contains numbers. See Natural order section.

WARNING

The -l option is here solely to show the file informations on which files are ordered. This option isn't required to actually sort the output.

# No order (faster)

Sometimes, their is no need to have the ls output sorted. If you have a huge number of files in a directory and don't want them sorted, it's way faster to ask for an unsorted list with the -U option. ls will use the directory order (which depends on your filesystem).

> ls -lU
total 28
-rw-rw-r--. 1 poc poc 25 May 19 13:56 file-213.txt
-rw-rw-r--. 1 poc poc 20 May 19 13:56 file-111.txt
-rw-rw-r--. 1 poc poc  5 May 19 11:09 file-11.txt
-rw-rw-r--. 1 poc poc 20 May 19 13:55 file-1.txt
-rw-rw-r--. 1 poc poc 25 May 19 13:56 file-2.txt
-rw-rw-r--. 1 poc poc 20 May 19 13:57 file-213.md
-rw-rw-r--. 1 poc poc  2 May 19 11:09 file-21.txt
1
2
3
4
5
6
7
8
9

# Reverse order

All order can be reversed with the -r option:

> ls -lr
total 28
-rw-rw-r--. 1 poc poc 25 May 19 13:56 file-2.txt
-rw-rw-r--. 1 poc poc  2 May 19 11:09 file-21.txt
-rw-rw-r--. 1 poc poc 25 May 19 13:56 file-213.txt
-rw-rw-r--. 1 poc poc 20 May 19 13:57 file-213.md
-rw-rw-r--. 1 poc poc 20 May 19 13:55 file-1.txt
-rw-rw-r--. 1 poc poc  5 May 19 11:09 file-11.txt
-rw-rw-r--. 1 poc poc 20 May 19 13:56 file-111.txt
1
2
3
4
5
6
7
8
9

# Natural order (version)

When filenames contain numbers, like in this page, the alphabetical order seems wrong (but is not 😛). The ls command provides the -v (version) option to change the order from alphabetical to natural.

> ls -lv
total 28
-rw-rw-r--. 1 poc poc 20 May 19 13:55 file-1.txt
-rw-rw-r--. 1 poc poc 25 May 19 13:56 file-2.txt
-rw-rw-r--. 1 poc poc  5 May 19 11:09 file-11.txt
-rw-rw-r--. 1 poc poc  2 May 19 11:09 file-21.txt
-rw-rw-r--. 1 poc poc 20 May 19 13:56 file-111.txt
-rw-rw-r--. 1 poc poc 20 May 19 13:57 file-213.md
-rw-rw-r--. 1 poc poc 25 May 19 13:56 file-213.txt
1
2
3
4
5
6
7
8
9

# Size order

To order the output by file size, simply use the -S (Size) option (larger files come first)

> ls -lS
total 28
-rw-rw-r--. 1 poc poc 25 May 19 13:56 file-213.txt
-rw-rw-r--. 1 poc poc 25 May 19 13:56 file-2.txt
-rw-rw-r--. 1 poc poc 20 May 19 13:56 file-111.txt
-rw-rw-r--. 1 poc poc 20 May 19 13:55 file-1.txt
-rw-rw-r--. 1 poc poc 20 May 19 14:13 file-213.md
-rw-rw-r--. 1 poc poc  5 May 19 11:09 file-11.txt
-rw-rw-r--. 1 poc poc  2 May 19 11:09 file-21.txt
1
2
3
4
5
6
7
8
9

# File extension order

ls also provides a way to order it's output depending on the file extension with the -X (eXtension) option.

> ls -lX
total 28
-rw-rw-r--. 1 poc poc 20 May 19 13:57 file-213.md
-rw-rw-r--. 1 poc poc 20 May 19 13:56 file-111.txt
-rw-rw-r--. 1 poc poc  5 May 19 11:09 file-11.txt
-rw-rw-r--. 1 poc poc 20 May 19 13:55 file-1.txt
-rw-rw-r--. 1 poc poc 25 May 19 13:56 file-213.txt
-rw-rw-r--. 1 poc poc  2 May 19 11:09 file-21.txt
-rw-rw-r--. 1 poc poc 25 May 19 13:56 file-2.txt
1
2
3
4
5
6
7
8
9

# Last modification time order (mtime)

Last sort options are about file dates. The -t option allows to order the ls output on the file modification time (mtime, which is the last time the file content has changed).

> ls -lt
total 28
-rw-rw-r--. 1 poc poc 25 May 19 13:57 file-213.md
-rw-rw-r--. 1 poc poc 25 May 19 13:56 file-213.txt
-rw-rw-r--. 1 poc poc 20 May 19 13:56 file-111.txt
-rw-rw-r--. 1 poc poc 25 May 19 13:56 file-2.txt
-rw-rw-r--. 1 poc poc 20 May 19 13:55 file-1.txt
-rw-rw-r--. 1 poc poc  2 May 19 11:09 file-21.txt
-rw-rw-r--. 1 poc poc  5 May 19 11:09 file-11.txt
1
2
3
4
5
6
7
8
9

Their is two other dates tracked by the filesystem that can be used by ls to order its output: the last access time (atime) and the last change time (ctime, which take into account modifications on file content and metadata, like file owner and file permission).

# Last access time order (atime)

> ls -ltu
> ls -u 
1
2

# Last change time order (ctime)

> ls -ltc
> ls -c 
1
2

TIP

When used with the -l option, the -u and -c options only sort the output is the -t option is added. Otherwise (-l option is set but not the -t option), they only change the time shown by the -l option but not the order used.

> ls -ltu # show the last access time and order on it
> ls -lu  # show the last access time but order alphabetically
1
2

WARNING

Their is no way to sort on the file creation time with the ls command simply because filesystems are not required to store it (some does, however, like ext4).

Last updated: 6/11/2019, 11:01:06 PM