16 Practical Examples of Linux LS command for Beginners
On this page
- 1. How to list hidden files/directories
- 2. How to skip current (.) and previous directory (..) entries in the output
- 3. How to display files/directories in reverse order
- 4. How to sort ls command output based on file extensions
- 5. How to sort files based on modification time
- 6. How to list subdirectories recursively
- 7. How to list filenames along with their inode numbers
- 8. How to display detailed information about files and directories
- 9. How to display author information
- 10. How to print C-style escapes for non-graphic characters (like newlines and spaces)
- 11. How to print file size in units other than bytes
- 12. How to make ls display only filenames and file sizes in output
- 13. How to make ls not list backup files in the output
- 14. How to make ls specifically indicate the type of file in the output
- 15. How to change ls command output format
- 16. How to make ls hide a particular type of files
Whenever it's required to take a look at the contents of a directory on the command line in Linux or any other UNIX-like operating system, ls is the command that's used. The ls command lists the contents of directories 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:
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.
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.
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.
Here's the above command in action:
As you can see in the screenshot above, among the files with names 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.
The -t command line option displays the 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.
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.
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:
- 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
- The sixth and seventh are the date and time of the 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
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.
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 leads 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 the 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]
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]
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.
ls -s -h
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.
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:
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 to 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 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:
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 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.