Whenever it’s required to take a look at the contents of a directory on the command line in Linux, ls is the command that’s used. It lists the contents sorted in alphabetical order.

<img alt="Linux LS Command" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/07/echo/ls-basic-example.png62cbf2acdd2e2.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="78" loading="lazy" src="data:image/svg xml,” width=”300″>

While that’s the tool’s basic usage, it offers a plethora of features (in the form of command-line options) that help you in several circumstances. In this tutorial, we will discuss the usage of some of these command-line options through easy to understand examples.

The ls command doesn’t display hidden files/directories by default. However, you can force the tool to do that using the -a command-line option. So, to list all files/directories (including hidden ones – those with names beginning with a dot ‘.’) in the current directory, run the following command:

ls -a 

<img alt="List hidden directories" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/07/echo/ls-a-hidden.png62cbf2ad0ebc6.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="125" loading="lazy" src="data:image/svg xml,” width=”413″>

2. How to skip current (.) and previous directory (..) entries in the output

When using the -a command line options, the entries for the current and previous directory (. and .. respectively) also show up. If you want ls to not display these in output, use the -A command-line option instead. 

<img alt="Skip directories with LS" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/07/echo/ls-A-almost-all.png62cbf2ad3e357.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="106" loading="lazy" src="data:image/svg xml,” width=”405″>

3. How to display files/directories in reverse order

By default, the ls command displays the files alphabetically in the output. But if you want, you can also make the tool display files in reverse order. For this, you’ll have to use the tool’s -r command-line option.

ls -r

Here is an example:

<img alt="display files/directories in reverse order" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/07/echo/ls-reverse-sorting.png62cbf2ad60c54.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="289" loading="lazy" src="data:image/svg xml,” width=”500″>

4. How to sort ls command output based on file extensions

To sort the ls command output alphabetically based on file extensions, use the -X command-line option.

ls -X

Here’s the above command in action:

<img alt="sort ls command output based on file extensions" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/07/echo/sort-files-based-on-ext.png62cbf2ad8d589.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="189" loading="lazy" src="data:image/svg xml,” width=”500″>

As you can see in the screenshot above, among the files with name containing an extension, ‘echo.c‘ is listed first, and ‘byzanz_window.py’ is listed at the end.

5. How to sort files based on modification time

If you want to quickly know which files or directories were modified recently, then you can use the ls command’s -t option.

ls -t

The -t command line option displays newest entries first.

6. How to list subdirectories recursively

If you want, you can make the ls command display contents of subdirectories as well. This can be done using the -R command line option.

ls -R 

Following screenshot shows the above command in action:

<img alt="list subdirectories recursively with ls command" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/07/echo/ls-list-subdir-recursively.png62cbf2adc1072.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="237" loading="lazy" src="data:image/svg xml,” width=”355″>

7. How to list filenames along with their inode numbers

The ls command also lets you list file/directory names along with their respective inode numbers. The tool provides the -i command line option to access this feature.

ls -i

Here’s the above command in action:

<img alt="list filenames along with their inode numbers" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/07/echo/ls-list-inode-num.png62cbf2adf27aa.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="235" loading="lazy" src="data:image/svg xml,” width=”500″>

As you can see in the screenshot above, the numbers on the left are inode numbers, while the names on the right are corresponding file/directory names.

8. How to display detailed information about files and directories

The ls command can also print detailed information about files and directories. To enable this output format (also known as long listing format), you need to use the -l command-line option.

<img alt="display detailed information about files and directories" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/07/echo/ls-l-long-listing.png62cbf2ae3b811.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="237" loading="lazy" src="data:image/svg xml,” width=”500″>

As you can see in the screenshot above, the output is divided into 8 columns. Here’s what these columns convey:

  • The first column shows file permissions
  • The second column shows the number of hard links 
  • The third and the fourth ones are owner and group names
  • fifth is the file size
  • Sixth and seventh are date and time of last modification
  • The last is the name of the file.

9. How to display author information

You can also ask the ls command to list each file’s author when producing long format directory listings. For this, you have to combine –author option with -l.

ls -l –author

<img alt="display author information" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/07/echo/ls-author.png62cbf2aea1e35.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="208" loading="lazy" src="data:image/svg xml,” width=”500″>

As you can see in the screenshot above, the newly-added fifth column is for the author.

Note: The official GNU website says, “In GNU/Hurd, file authors can differ from their owners, but in other operating systems the two are the same.”

10. How to print C-style escapes for non-graphic characters (like newlines and spaces)

Suppose the current directory contains a file with a name having a new-line character. If you try listing the contents of that directory, that particular file name will be shown containing a question mark (?), which signifies a non-printable character.

<img alt="print C-style escapes for non-graphic characters" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/07/echo/ls-question-mark.png62cbf2aeca64c.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="144" loading="lazy" src="data:image/svg xml,” width=”370″>

However, the ls command provides an option that – when used – prints C-style escape characters for non-printable characters. The option in question is -b.

<img alt="print C-style escapes for non-graphic characters – part 2" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/07/echo/ls-newline-escape.png62cbf2af1933a.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="145" loading="lazy" src="data:image/svg xml,” width=”371″>

So you can see in the screenshot above, the -b option lead to ls printing ‘n’ for the newline character. Similarly, spaces get represented by ‘[space]’ in this mode.

11. How to print file size in units other than bytes

If you want to know the size of a file, you’ll have to use the -l option (that enables long listing format) we discussed in point 3 above. The size gets displayed in bytes by default. However, there may be situations where-in you would want the size in kilobytes or megabytes.

The ls command has an option that lets you specify the unit in which you want the file size to be displayed. The option in question is –block-size. For example, if you want the size to be displayed in kilobytes, you can run the following command:

ls -l –block-size=k [file-name]

<img alt="print file size in units other than bytes" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/07/echo/ls-kilobytes-size.png62cbf2af58b13.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="62" loading="lazy" src="data:image/svg xml,” width=”500″>

Keep in mind that the size figure produced with -l command-line option is the actual file size, or the amount of data it contains. If, instead, you want to know the size of the file on the file system or allocated size (yes, it can be different), then you’ll have to use the -s command.

ls -s –block-size=k [file-name]

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2022/07/echo/ls-s-size-on-filesystem1.png62cbf2afa04c6.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="58" loading="lazy" src="data:image/svg xml,” width=”500″>

For more information on the difference between file size produced by -l option and -s option, head here.

12. How to make ls display only filenames and file sizes in output

If you want the ls command output to only contain file/directory names and their respective sizes, then you can do that using the -h option in combination with -l/-s command line option.

For example:

ls -s -h

<img alt="ls display only filenames and file sizes in output" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/07/echo/ls-display-only-filename-size.png62cbf2afe2c28.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="261" loading="lazy" src="data:image/svg xml,” width=”500″>

13. How to make ls not list backup files in the output

In Linux, backup files are represented with names ending with a tilde(~). The ls command provides an option (-B) using which you can ask the tool to not list backup files in output, in case that’s what’s needed. 

For example, the following screenshot shows two ls commands. The first one outputs the name of a backup file, but the second one (which uses the -B option) doesn’t.

<img alt="ls not list backup files in output" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/07/echo/ls-b-no-backup.png62cbf2b03ee07.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="89" loading="lazy" src="data:image/svg xml,” width=”406″>

14. How to make ls specifically indicate the type of file in the output

By default, the output that the ls command display is color-coded, where different colors represent different types of files. However, if you want, you can also have the tool append an indicator (for this purpose) to entries in output. This feature can be accessed using the -F option.

Here’s an example:

<img alt="make ls specifically indicate type of files in output" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/07/echo/ls-f-type-indicator.png62cbf2b083c4e.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="112" loading="lazy" src="data:image/svg xml,” width=”500″>

Observe the extra ‘/’ with example directory, and ‘*’ with test executable. Here’s the complete list of indicators and along with information on what they indicate:

  • @ indicates a symbolic link 
  • * indicates an executable 
  • = indicates a socket file 
  • | indicates a named pipe 
  • > indicates a door 
  • / indicates a directory

15. How to change ls command output format

If you want, you can also change the output format for the ls command. The tool’s –format option lets you do this. This option requires you pass a value that indicates the type of output format you want. Available values are:  ‘verbose’ or ‘long’, ‘commas’, ‘horizontal’ or ‘across’, ‘vertical’, and ‘single-column’.

For example, use –format=commas when you want a comma-separated output:

<img alt="change ls command output format" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/07/echo/ls-format-comma.png62cbf2b1aae6a.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="82" loading="lazy" src="data:image/svg xml,” width=”500″>

Similarly, use –format=single-column when you want the output to be displayed in a single column:

<img alt="change ls command output format – result" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/07/echo/ls-format-single-column.png62cbf2b23b29f.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="273" loading="lazy" src="data:image/svg xml,” width=”395″>

16. How to make ls hide a particular type of files

If you want, you can also force the ls command to hide a particular type of files in output. The –hide option (which requires a shell pattern to be passed to it) lets you do this. For example, if you don’t want the tool not to display .txt files in the output, here’s the command you should run:

ls –hide=*.txt

The following screenshot shows the output both without and with the –hide option.

<img alt="make ls hide particular type of files" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/07/echo/ls-hide-files.png62cbf2b2897aa.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="160" loading="lazy" src="data:image/svg xml,” width=”500″>

Conclusion

While ls is a very basic command, how effectively it can be used depends on the user’s knowledge of the command line options the tool provides. Do try the options we’ve discussed here in tutorial, and in case of any doubt or query, head to its man page.