Sort ls command by last modified date and time

The ls command is an essential tool in Unix-like operating systems, widely used to display the contents of a directory in a terminal on Linux and Mac devices. It provides a quick overview of files and directories, making it easier to navigate the file system.

To sort the output of the ls command by date and time, you can use the -t option in conjunction with the ls -l command. This combination not only lists the files but also provides detailed information such as permissions, number of links, owner, group, size, and the last modified date and time.

% ls -lt

total 60
drwx------   1 root root 4096 Aug  7 17:28 root
drwxrwxrwt   1 root root 4096 Aug  7 17:20 tmp
drwxr-xr-x   5 root root  360 Aug  7 16:52 dev
dr-xr-xr-x  13 root root    0 Aug  7 16:52 sys
dr-xr-xr-x 180 root root    0 Aug  7 16:52 proc
drwxr-xr-x   1 root root 4096 Jul 31 17:20 etc
drwxr-xr-x   1 root root 4096 Jul 31 16:57 run
drwxr-xr-x   1 root root 4096 Jul 31 16:56 home
drwxr-xr-x   1 root root 4096 Apr  5 05:17 var
drwxr-xr-x   2 root root 4096 Apr  5 05:01 media
drwxr-xr-x   2 root root 4096 Apr  5 05:01 mnt
drwxr-xr-x   2 root root 4096 Apr  5 05:01 opt
drwxr-xr-x   2 root root 4096 Apr  5 05:01 srv
drwxr-xr-x   1 root root 4096 Apr  5 05:01 usr
lrwxrwxrwx   1 root root    7 Apr  5 05:01 bin -> usr/bin
lrwxrwxrwx   1 root root    7 Apr  5 05:01 lib -> usr/lib
lrwxrwxrwx   1 root root    8 Apr  5 05:01 sbin -> usr/sbin
drwxr-xr-x   2 root root 4096 Apr 15  2020 boot

To display the latest files and directories first, you can also use the -r option along with -lt. This will reverse the order of the output, showing the most recently modified items at the top of the list.

% ls -ltr

total 60
drwxr-xr-x   2 root root 4096 Apr 15  2020 boot
lrwxrwxrwx   1 root root    8 Apr  5 05:01 sbin -> usr/sbin
lrwxrwxrwx   1 root root    7 Apr  5 05:01 lib -> usr/lib
lrwxrwxrwx   1 root root    7 Apr  5 05:01 bin -> usr/bin
drwxr-xr-x   1 root root 4096 Apr  5 05:01 usr
drwxr-xr-x   2 root root 4096 Apr  5 05:01 srv
drwxr-xr-x   2 root root 4096 Apr  5 05:01 opt
drwxr-xr-x   2 root root 4096 Apr  5 05:01 mnt
drwxr-xr-x   2 root root 4096 Apr  5 05:01 media
drwxr-xr-x   1 root root 4096 Apr  5 05:17 var
drwxr-xr-x   1 root root 4096 Jul 31 16:56 home
drwxr-xr-x   1 root root 4096 Jul 31 16:57 run
drwxr-xr-x   1 root root 4096 Jul 31 17:20 etc
dr-xr-xr-x 181 root root    0 Aug  7 16:52 proc
dr-xr-xr-x  13 root root    0 Aug  7 16:52 sys
drwxr-xr-x   5 root root  360 Aug  7 16:52 dev
drwxrwxrwt   1 root root 4096 Aug  7 17:20 tmp
drwx------   1 root root 4096 Aug  7 17:32 root

Conclusion:

  • ls -l: The l option displays the output in a detailed list format, showing file permissions, number of links, owner, group, size, and last modified date.
  • ls -lt: The t option sorts the list by modification time, displaying the oldest files first.
  • ls -ltr: The tr option reverses the order, showing the most recently modified files first.
Sort ls command date and time

Frequently Asked Questions (FAQ)

  • Q: How can I customize the output of the ls command?

    A: You can customize the output of the ls command using various options such as -a to include hidden files, -h for human-readable file sizes, and -S to sort files by size.

  • Q: What does the ls -l command output mean?

    A: The output of ls -l includes several columns: file type and permissions, number of links, owner name, group name, file size, last modified date, and file name.

  • Q: Can I use wildcards with the ls command?

    A: Yes, you can use wildcards such as * and ? to match multiple files or directories. For example, ls *.txt will list all text files in the current directory.

  • Q: How do I get help with the ls command?

    A: You can get help with the ls command by typing man ls in the terminal, which will display the manual page with detailed information about its usage and options.

  • Q: Is there a way to save the output of the ls command to a file?

    A: Yes, you can redirect the output to a file using the > operator. For example, ls -l > output.txt will save the output to a file named output.txt.

Comments & Discussion

Facing issues? Have questions? Post them here! We're happy to help!