Skip to Content

6 ways to use ls command in Linux

The ls command is one of the most basic commands in Linux. It prints the contents of a directory to the screen. By adding different options, you can modify the output of the ls command to your needs.

By default, it prints the names of all of the files and directories in the directory, but you can use various flags to display more information.

ls command in Linux: list the contents of the directory

In Linux, the “ls” command can be used to view the contents of a directory in a variety of ways, including by file type, by modification time, and by file size.

The “ls” command has a number of options that allow you to customize the output.

Boost Your Website Speed!

If you want your website to run as fast as ours, consider trying Cloudways. Their powerful cloud infrastructure and optimized stack deliver exceptional performance. Free migration!

For example,

  • the “-l” option prints the permissions, owner, group, size, and modification date for each file.
  • The “-a” option prints all files, including hidden files.
  • The “-t” option sorts the output by modification time.
  • The “-S” option sorts the output by file size. Largest first.
  • The “ls” command with -R option can also be used to recursively list the contents of all subdirectories.
  • You can use the ls command to list the contents of multiple directories. For example, if you want to list the contents of your home directory and the /tmp directory, you would type: ls ~ /tmp

 

You can use multiple flags with the ls command. For example, if you want to list all of the files in the current directory, sorted by file size in ls command, with file sizes in human-readable format, you can type:

ls -lhS

My favorite ls command are ls -lrt and ls -lrSh.

ls -lrt: This command is useful for displaying files and directories in a long listing format, sorted in ascending order of modification time (oldest modification first).

Example:

-rw-r--r-- 1 user group 512K Jul 16 14:15 file1.txt
drwxr-xr-x 2 user group 4.0K Jul 16 18:20 folder1
-rw-r--r-- 1 user group 1.2M Jul 17 09:45 file2.txt
drwxr-xr-x 2 user group 4.0K Jul 17 10:30 folder2

ls -lrSh: This command is useful for displaying files and directories in a long listing format, sorted in reverse order of file size, with file sizes displayed in human-readable format.

Example:

-rw-r--r-- 1 user group 10K Jul 17 09:45 file4.txt
-rw-r--r-- 1 user group 100K Jul 16 14:15 file2.txt
-rwxr-xr-x 2 user group 1M Jul 17 10:30 file3.txt
-rwxr-xr-x 2 user group 10M Jul 16 18:20 file1.txt

 

show more information about each file with ls -l in Linux

You can use the -l flag with the ls command to show more information about each file, such as the permissions, owner, group, size, and modification time.

The “-l” option stands for “long listing”.

For example, if you want to list all of the files and directories in the current directory with more information, you would type: ls -l

You can also use the “ls” command to show only certain types of files. For example, if you want to see only the directories in your home directory, you can use the following command:

ls -l | grep "^d"

This will show you a list of all the directories in your home directory.

  • The “grep” command is used to filter the output of the “ls” command.
  • The “^d” option tells grep to show only lines that start with the letter “d”.

 

recursively list the contents of all subdirectories with ls -R in Linux

The “ls” command has an “-R” option that allows you to recursively list the contents of all subdirectories.

This means that the “ls” command will list the contents of all subdirectories, as well as the contents of those subdirectories, and so on.

The “-R” option stands for “recursive”.

For example, if you want to list all of the files in the current directory and all subdirectories recursively, you would type:

ls -R

show all files including hidden files with ls -a in Linux

You can use the -a flag with the ls command to show all files, including hidden files. This means that the “ls -a” command will list all of the files in the directory, including the files that are normally hidden from view.

Hidden files are typically denoted by a . (dot) at the beginning of the filename.

For example, if you want to list all of the files in the current directory, including hidden files, you would type:

ls -a

sort the output by file modification time with ls -t in Linux

The “ls” command has a “-t” option that allows you to sort the output by file modification time.

This means that the “ls -lt” command will list the files in the directory in order of oldest to newest.

sort the output by file size with ls -S in Linux

You can use the -S flag with the ls command to sort the output by file size. For example, if you want to list all of the files in the current directory, sorted by file size, you can type:

ls -lS

You can use multiple flags with the ls command. For example, if you want to list all of the files in the current directory, including hidden files, sorted by modification time, you would type:

ls -lat

print file sizes in human-readable format with ls -h in Linux

The “ls” command has a “-h” option that allows you to show the file sizes in human-readable format.

This means that the “ls” command will list the file sizes in bytes, followed by the word “bytes or M or G”.

For example, if you want to list all of the files in the current directory, sorted by file size in ls command, with file sizes in human-readable format, you would type: ls -lhS

reverse the order of the output with ls -r in Linux

You can use the -r flag with the ls command to reverse the order of the output.

For example, if you want to list all of the files in the current directory, sorted by modification time in ls command, in reverse order, you would type: ls -ltr

Advanced ls command options

  • You can use the –color flag with the ls command to colorize the output. For example, if you want to list all of the files in the current directory, with colors indicating the file type, you can type: ls –color
  • You can use the -i flag with the ls command to show the inode number of each file. The inode number is a unique identifier for each file. For example, if you want to list all of the files in the current directory, with their inode numbers, you can type: ls -li
  •  You can use the -F flag with the ls command to show a trailing / for directories, a trailing * for executable files, and a trailing @ for symbolic links. For example, if you want to list all of the files in the current directory, with indicators for each file type, you can type: ls -F
  • You can use the –time-style flag with the ls command to specify how the modification time is displayed. For example, if you want to list all of the files in the current directory, with the modification time displayed in ISO 8601 format, you can type: ls –time-style=iso
  • You can use the -d flag with the ls command to list only the directories, not the files. For example, if you want to list all of the directories in the current directory, you can type: ls -d */
  • You can use the –group-directories-first flag with the ls command to list directories before files. For example, if you want to list all of the files and directories in the current directory, with the directories listed first, you can type: ls –group-directories-first

 

FAQ about ls command in Linux

Does the ls command show hidden files on my computer in Linux?

Yes, the ls command will show hidden files on your computer. By default, Linux doesn’t show hidden files, but you can use the “-a” flag to show all files, including hidden ones.

How can I see more information about a file using the ls command in Linux?

If you want to see more information about a file, you can use the -l flag with the ls command. This will show you the file’s permissions, owner, group, size, and last modified date.

How do I show only directories when using the ls command in Linux?

If you want to see only directories when using the ls command, you can use the “-d” flag. For example, the following command will show only directories: ls -d

 How do I sort the output of the ls command by file size?

The ls command in Linux is used to list files and directories. The output of the ls command is sorted alphabetically by default, but it can be sorted by file size using the -l or -S option.

The -l option will show you the long listing of files, while the -S option will show you only the file sizes. To sort the output of the ls command by file size, use the -l -s option followed by the -h option, which sorts files by human-readable file sizes (e.g. KB or MB).