In Linux, the ls command is one of the fundamental tools. It lists files and directories with or without various additional information. The ls command is a part of the GNU core utility package. It should be available on any Linux distro.
This guide will showcase how to use the ls command, specifically “ls -l” in combination with other options.
Linux ls command
The ls command takes the location of a directory and prints all the files and directories within the location. It can also print additional file information like file permissions, file ownership, file size, etc.
This is the command structure that all ls commands must follow.
If no directory is specified, then ls performs its action on the current directory. For example, the following command will list all the files and directories (name only).
Listing files and directories in long list format
By default, the ls command will print the name of all the files and directories only. To get the additional information and a cleaner view, use the flag “-l”.
In long list format, ls shows the following info about each file.
- File type
- File permissions
- Hard links
- Date and time
There are numerous options available to modify the output of the ls command. All the following options are combined with “ls -l” to use the long list format by default.
Showing backslash after directories
By default, the ls command uses coloring to distinguish between files and directories. However, it may not be reliable in various situations; for example, you’re accessing through a console that doesn’t support coloring.
In such a situation, use the flag “-F” to show a backslash “/” after the name of a directory.
The ls command will show the list in a specific order (generally by file name in alphabetic order). However, it also supports sorting based on other values.
To sort the output in reverse order, use the flag “-r”.
To sort the files and directories by time and date of creation/modification, use the flag “-t” instead.
We can also sort the output alphabetically by entry extension. To do so, use the flag “-X”.
Showing hidden files
By default, the ls command won’t show hidden files. These are the files that start with “.” at the beginning of the file name.
To show the hidden files, use the flag “-a”.
A directory tree shows the hierarchy of the target directory and its sub-directories and files. Generally, we use the tree command to check the directory tree.
However, the ls command can also showcase the directory tree (although not-so-good-looking). To print the tree view, use the flag “-R”.
Showing inode number
In Linux, each file has its unique inode value. Inode stores metadata for every single file on the partition. These data are stored at the start of each partition. It stores all the file info except the file name and the data itself.
To show the inode value of each file and directories, use the flag “-i”.
Showing UID and GID
In Linux, each file has its UID (unique identifier) and GID (group ID). To show the UID and GID of the files, use the flag “-n”.
By default, the ls command prints file size in bytes. We can tell to print all the values in human-readable format for easier understanding of them.
These are only a handful of options that the ls command supports. For the full list of supported options as well as detailed explanations, check out the man page.
In this guide, we learned about the ls command in Linux. It’s one of the fundamental tools available on all Linux distros. This guide showcases many common methods of using the ls command. Once mastered, it can be more convenient to use it than a GUI file browser.