What is The ls Command in Linux? – A Complete Guide

ls-command

The ls command in Linux allows a user to list the contents of a directory to view the available files and sub-directories. As a UNIX/Linux user, the ls command is one of the many commands you will run daily, and mastering the different ways of utilizing it is handy.

Luckily, the ls command is beginner-friendly, and with a few practical examples, you will quickly grasp the hold of it and enhance your performance when performing different tasks. This post discusses more on the Linux ls command while giving examples.

ls Command Syntax

Below is the basic syntax to follow when running the ls Linux command in Linux.

While it’s possible to run the command without any options, for someone looking to get more specific results, there are numerous options that you can utilize. More on them in the next section.

ls Command Examples

The ls command has numerous options that you can use to get desired results. This section presents practical examples of using the ls command with options such as ls -l. Take a look!

Example 1: Basic Usage

Opening the terminal will take you to the current working directory.

Run the ls command with no options to show all the files and directories contained.

For our case, we list the contents of our home directory.

Example 2: ls Command Long Listing

You can display all the information about the files and directories, which is the long listing method. For that, use the ls -l option.

The results will contain details such as file permissions, dates, size, owner, etc.

Example 3: Use Human Readable Format

The results in the previous example could be challenging to read. Adding the -h option will show the same results as the ls command but in a human-readable format.

Example 4: Use Reverse Order

The default behavior of the Linux ls command is that it will print the output based on the date, showing the newest first. However, you can reverse the order by adding the -r option.

The below example starts by running the ls command and then adds the -r to show the difference in the order.

Example 5: ls Command Include Hidden Files

Running the ls Linux command won’t include the hidden files in the output. To list all the hidden files, add the -a option.

Notice how running the same command we did earlier but now with the -a option shows hidden files with a period (.) preceding them.

Example 6: List Files Recursively

When running the ls Linux command, you can show the directory tree of all the files and directories. This option requires you to add the -R flag and will display more results, capturing the tree hierarchy.

Here’s an example.

Example 7: Display the Last Edited Directory Contents

Instead of listing all the files and folders in your working directory, you can specify only to show the last edited file or folder. The -t option will filter the files and folders to arrange them based on the edit time.

Moreover, adding the Linux head command allows you to choose how many results you want. For instance, if we wanted to show the last edited file or the two last edited files or folders, we would run our command as follows.

Example 8: Display Output Per Line

Sometimes, you may wish all the results of running the command appear per line for better analysis. The -1 option will help you achieve that.

You can combine this option with other options we’ve discussed for more specific results.

Example 9: Show inode Number

With the -i option, your results will contain an inode number for every file or directory.

Example 10: List in Other Directories

You are not restricted to only listing the files and folders of your current directory. You can specify the target directory by specifying its path.

For instance, to run a basic ls command in the Desktop/ directory, we would add its path as shown below.

You can also use various options for better results. For instance, we could long list the Desktop/ as shown below.

Another approach is combining the ls command with the cd command. The example below shows how to run the cd command that switches the current directory to the mentioned path and then runs the ls command upon successful execution.

Example 11: ls Command Help Page

Like all Linux commands, the ls command also comes with a help/manual page containing instructions on how to use it.

To access this page, run the below command.

You will get an output like the one below.

Conclusion

The Linux ls command helps show the directory contents of any specified file or folder. We’ve discussed the command in detail, including its syntax and different options that you can use. Thanks to the examples, you can now easily understand how to work with the ls command in Linux.

Found this helpful? - Share it!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top