Understanding ls -ltr command in Linux

The ls command is one of the most frequently used Linux commands. It allows users to list information about files and directories within a file system. The ls -lrt command is an extension of this, providing more control over the type and order of output displayed.

Also read: How to Use ls Command in Linux/Unix

What is the ls -ltr command

The ‘ls -lrt’ command stands for “list in long format, reverse sorted by modification time”. This means that it will provide detailed information about each file or directory, as well as sort them in reverse order based on their last modified date/time stamp.

Image 12
Output of ls -lrt command

This particular variant of the ‘ls’ command provides a useful way to quickly view recently created or modified files within a directory structure without having to manually sort through everything else present. It can also be used to identify which files are taking up too much storage space when disk quotas are enabled on certain systems.

Breakdown of the Command

Here’s what each argument (flag) included with this particular variant of the ‘ls’ command does:

  • -l: Long list format – Displays additional details such as file size, modified date/time stamp, etc., for each item listed
  • -r: Reverse order while sorting – Sorts items in reverse alphabetical/numerical order
  • -t: Sort by modification time – Orders items based on their last-modified timestamp

Examples of Usage

Here are some examples of how the ‘ls -lrt’ command can be used on Linux systems:

1. To list all files and subdirectories within the current working directory, sorted by modification time in reverse order:

ls -lrt
Image 14
listing all the files and directories in the current working directories using ls -ltr command

2. To list all files and subdirectories in a specific directory, sorted by modification time in reverse order:

ls -lrt /path/to/directory
Image 13
using the ls -ltr command to list all the files and directories, sorted by modification time in reverse

3. To list all regular files (not directories) within the current working directory, sorted by modified date/time stamp in reverse order:

ls -ltr | grep ^-
Image 15
Using ls -ltr | grep ^- command to list only files within the current working directory, sorted by modified date/time stamp in reverse order

Various Arguments of ls Command Explained

  • ls path – This command will list information about the specified path.
Image 17
Example of ls command
  • ls / – This command will list all items within the root directory (/).
Image 18
Example of ls/ command
  • ls .. – This command will list information about the parent directory relative to your current working location.
Image 19
Example of ls .. command
  • ls ~ – This command will list all items present in your home directory (~).
Image 20
Example of ls ~ command
  • ls -R – This command will recursively display any subdirectories within the specified path in addition to their respective contents.
Image 21
Example of ls -R command
  • ls -A – This command will display hidden files and directories (those whose names begin with a dot ‘ . ’) along with other visible ones.
Image 22
Example of ls -A command
  • ls -C – This command will sort output by filename instead of modification date/time stamp, making it easier to locate specific entries if you know their name but not when they were created or modified lastly.
Image 23
Example of ls -C command
  • ls-d – This command will only display directories instead of both directories and regular files present at a given location (useful for quickly navigating around a file system). But using it alone would yeild not so useful outputs but it is quite useful when used with */ operator to list only directories.
Image 24
Example of using ls -d */ to display only directories in the present working directory
  • ls -F –This command adds an extra character at the end of each entry to indicate its type; for example ‘ / ’ is added after any directory name while ‘ * ’ is added after executables etc., making it easier to differentiate between different types of entries at a glance without having to open them or use other tools such as ‘file’ .
Image 25
Example of ls -F command
  • ls -lrta – This command will display all entries in long format, sorted by modification date/time stamp in reverse order while also including hidden files and directories.
Image 26
Example of ls -ltra command
  • ls -lrtS – This command is similar to the above but sorts output based on file size instead of modification date/time.
Image 27
Example of ls -ltrS command
  • ls -rltuR – This command will recursively list all subdirectories within a given path and sort output by modification time in reverse order.
Image 28
Example of ls -rltuR command
  • ls -F – This command is a combination of the ‘ls -F’ and ‘ls -l’ commands; it displays detailed information about each entry along with an extra character at the end indicating its type.
Image 29
Example of ls -F command

Summary

The ‘ls -lrt’ command is an extension of the ‘ls’ command which provides users with more control over the type and order of output displayed. By including additional arguments such as ‘-l’, ‘-r’, and ‘-t’, users can quickly view recently created or modified files within a directory structure without having to manually sort through everything else present, making this particular variant of the ‘ls’ command very useful for tasks such as identifying large files or quickly navigating around a file system.