How to sort by size, type, time, name using linux ls command

Sort by size, type, time, and name using the linux ls command is a function that we use more often.

This article describes how to use the linux ls command to sort files or directories.

For example, use the ls command to sort by size and time.

Here are some common ls sorting methods.

First introduce the linux ls command options that will be used in the sorting method.

  • -l List in long format.
  • -r Reverse the order of the sort to get reverse lexicographical order or the oldest entries first (or largest files last, if combined with sort by size
  • -t Sort by time modified (most recently modified first) before sorting the operands by lexicographical order.
  • -c Use time when file status was last changed for sorting (-t) or long printing (-l).
  • -S Sort files by size
  • -u Use time of last access, instead of last modification of the file for sorting (-t) or long printing (-l).

The following will be sorted using the linux ls command options above.

ls sort by time / ls sort by date

1. ls sort by last modification (metadata)

In the following example, we use the ls -lcp option to sort the files (directories) in descending order according to the last modification time of the file (directory) metadata.

➜ ls -lct

In the following example, we use the ls -lcpr option to sort the files (directories) in ascending order according to the last modification time of the file (directory) metadata.

➜ ls -lctr

2. ls sort by modify time

  • File modification time descending sorting
➜ ls -lt

  • File modification time ascending sorting
➜ ls -ltr

3. ls sort by last access time

In the following example, we use the ls -lut option to sort the files (directories) in descending order according to the last access time of the files (directories).

➜ ls -lut

In the following example, we use the ls -lrut option to sort the files (directories) in ascending order according to the last access time of the files (directories).

➜ ls -lurt

ls sort by size

1. sorted by increasing size

In the following example, we use the ls -lSr option to sort files (directories) in ascending order according to size.

➜  ls -lSr

2. sorted by decreasing size

➜ ls -lS

ls sort by name

➜ ls -l
Default collation: ., .., numbers, uppercase letters, lowercase letters. If there are special characters, after the file or directory at the beginning of the number

ls sort by type

➜ ls -l | sort

Add a Comment

Your email address will not be published.