What Is Linux ls Command?

The ls command is a commonly used Linux command line. It can list all the files and directories in your specified directory. This command line can use the current directory to list files and directories in alphabetical order by name. It also supports flags that can change its behavior.

The ls command was initially used in the AT&T UNIX version 1 and then developed later. Now, it is available on all Linux distributions, BSD distributions, and Unix-like operating systems such as macOS (Darwin) and the Windows Subsystem for Linux (WSL).

However, the ls command supported options may differ due to operating systems and distributions.

How can you use the ls command in Linux? In this post, NewsPivot will introduce some commonly used scenarios.

Get Started with the ls Command in Linux

The output formats of the ls command vary based on the selected option flags and the commands it works with. This section will showcase some typical scenarios and use cases.

Open the Terminal in Linux

Before you use the ls command along with other options, commands, and flags, you first need to open the terminal in Linux. Here is the most-used method to launch the terminal in Linux.

You can click the Show Apps icon from the bottom-left corner of the screen and select Terminal from the app list to open the terminal in Linux.

open the Terminal in Linux from the App list

Use the ls Command to List Files and Directories

Step 1. Use the cd command to navigate to the target path or directory. For example, I type cd /snap and press Enter to go to the snap folder.

Step 2. Type ls and press Enter. Then, you can see the files and folders in the Snap folder.

run the ls command to list all files and folders in the Snap folder in Linux

Step 3. You can also use the ls command to see the files and folders of the subfolder of the Snap folder. For example, you can type ls /snap/bare to view the files and folder in that path.

Step 4. If you want to go to another folder, you can just run the cd command. For example, I run the cd /bin command to switch to that directory.

run the ls command to list files and folders and run the cd command to go to another path

Use the ls Command with the -R Option to List Files Recursively

The ls command can work with the -R option to list subdirectories. For example,

Step 1. Run cd /home to navigate to the home path.

Step 2. Run ls -R /home to list the subdirectories.

run the ls -R command to list subdirectories

The output of the -R option may be very long. However, you can use the search function in the terminal to quickly locate the target option.

search for the needed option in the terminal

Use the ls Command with the less and more Options to List Files Recursively and Scroll Interactively

In Linux, the more and less options allow you to scroll the content after running the ls command to list all files and folders. It is useful when the output is so long.

The difference between these two options is that the less option has both upward and backward scrolling features. The final “:” means that less is awaiting input.

the less option in Linux

After using the more option (for example, I run ls -R /usr | more in the terminal), there will be a –More– option. You need to press Enter to view more content and then you can scroll up to view the above content and then scroll down to view the down content.

the more option in Linux

Use the ls Command with the grep Option to List Files Recursively and Filter the Output

If you only want to view the special files and directories, you can use the grep option with the ls command. For example, I run ls -R /usr | grep unshare to only display unshare files.

run ls -R /usr | grep unshare to only display unshare files

Use the ls Command with the -l Option to List Files with Full Details

You can use the -l command with the ls command to display the details of files and directories, which contain the permissions, owners, groups, file sizes, and modified dates.

For example, I run ls -l /usr/include in the terminal.

run ls -l /usr/include in the terminal

Use the ls Command with the -F Option to List Files and Directories with Type

If you just want to know whether a file is a normal file, or a directory, a symbolic link, an executable, or a network file rather than the permissions or owners, you can use the ls command with the -F option.

For example, I run ls -F /etc in the terminal to check the files.

run ls -F /etc

Here, / stands for directories, * for executables, => for network files, and @ for symbolic links.

Use the ls Command with the -a Flag to Show Hidden Files

In Linux, the period . is used as a prefix for hidden files. These files are also called dot files. If you want to show hidden files in Linux, you can run the ls command with the -a flag.

For example, I run ls -a in the terminal.

run ls -a in the terminal too show hidden files

Use the ls Command with the -A Flag to Show Hidden Files Without . and ..

The files with . and .. are implicit directories. If you want to see hidden files with the suppressed, you need to use ls with the -A flag.

For example, I run ls -A in the terminal.

run ls -A in the terminal to show hidden files without implied . and ..

Use the ls Command with the -S or -t Options to List Files by Size or Time

If you use ls with -S, you can view files by size. While, if you use ls with -t, you can view files by time.

For example, I run ls -Sl /usr/include in the terminal to list files by size.

run ls -Sl /usr/include in the terminal to view files by size

I also run ls -tl /usr/include in the terminal to list files by time. The latest file is the first.

run ls -Sl /usr/include in the terminal to view files by time

Conclusion

This is the introduction to the ls command and how to use ls to work for you. You can see that the ls command is an important tool in Linux, allowing you to quickly list and organize files and directories.

The ls command can work with a wide range of options and flags for various needs, such as displaying hidden files, sorting by time or size, and filtering specific file types. Understanding the usage of the ls command can significantly enhance productivity when using Linux.

  • linkedin
  • reddit