howtouselinux

6 ways to use ls command in Linux

Table of Contents

The ls command is one of the most basic commands in Linux. It prints the contents of a directory to the screen. 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

The “ls” command is a very useful command for viewing the contents of a 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. 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, and the “-S” option sorts the output by file size.

The “ls” command can also be used 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.

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

 

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.

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

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 would 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 would 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 would 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 would 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 would 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 would 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 would 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).

 

David Cao
David Cao

Hey there! I am David, a Cloud & DevOps Enthusiast and 18 years of experience as a Linux engineer. I work with AWS, Git & GitHub, Linux, Python, Ansible, and Bash. I am a technical blogger and a Software Engineer, enjoy sharing my learning and contributing to open-source.