16 Practical Examples of Linux LS command for Beginners

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.

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.

1. How to list hidden files/directories

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 

2. How to skip current (.) and previous directory (..) entries in 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. 

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:

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:

As you can see in the screenshot above, among the files with name containing an extension, 'echo.c' is listed first, and '' 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:

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:

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.

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

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

As you can see in the screenshot above, the newly-added fifth column is for 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 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.

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.

So you can see in the screenshot above, the -b option lead to ls printing '\n' for 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 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]

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 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]

For more information on 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

13. How to make ls not list backup files in 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.

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

By default, the output that the ls command displays 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:

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:

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:

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

16. How to make ls hide 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.


While ls is a very basic command, how effectively it can be used depends on 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.

16 Practical Examples of Linux LS command for Beginners