The LS command is one of the first commands that any Linux newbie will learn after installing their first Linux operating system. Not only does it come in handy with Linux Desktop systems, but it’s also an excellent utility for server management.
LS command lists files and directories in Linux and Unix systems. Other than listing the files and directories, you can use this command to get more information about a file or directory. This data includes who owns the file, size of the data, permissions given to the file, and even the time of the creation of the file.
ls command uses in Linux with examples
The basic syntax of the LS command is as follows:
ls [option] [path_to_file/directory]
It is not a surprise that we use the LS command daily, but have we exhausted all the options that come with this great utility? In this article, we will look at 13 LS commands that may be of help in your everyday interaction with Linux systems.
1. List files and directory without additional options
It is one of the basic LS commands that every Linux user executes daily. It only lists files in a directory without any further information like permissions and file owner. Take a look at the image below.
From the above image, we ran the LS command on the Desktop, and it listed all the files present.
2. List files and Directories using the LS -L
The LS -L command lists files and directories together with additional information like permissions, owner, date of creation, etc. Let’s look at the image below before getting to every column and data presented.
From the above image, we see the information listed under seven columns. We will look at every single column and what it represents.
- Column One: It shows the file type and the user permissions assigned to the file.
- Column two: Indicates the number of blocks of memory taken by the file.
- Column three: It represents the owner or user who created the file.
- Column four: In Linux systems, users are associated with different groups. This column represents the group to which the owner belongs.
- Column five: It represents the size of the directory or file. It is calculated in bytes.
- Column six: This is the column that deals with time stamps on the file—the date of creation and modifications.
- Column seven: It represents the original name of the file or directory as given by the user.
3. List files with LS -A
In every operating system, we have hidden files and folders that are not displayed even with the LS -L command. Unless you have enabled the “show hidden files feature.” In Linux systems, hidden files are those that start with a dot(.) letter. The LS -A command lists all data, including hidden files (dot-files).
From the above image, we first ran the LS command, which listed only a few files. When we ran the LS -A command, on the same directory, it listed all files, including the hidden files.
4. List files with the LS -LH
The LS -LH command is commonly known to list files in a human-readable format. It is mainly useful when it comes to the size of the data. Unlike the LS -L command alone, which lists the size of files in bytes only, the LS -LH command will show the exact size in bytes(b), kilobytes(kb), megabytes(MB), etc. See the image below.
From the image above, the LS -L command file size in bytes, which is not so human-friendly. The LS -LH command output the files with the file sizes in a straightforward format.
5. List files with the LS -F
Suppose you want to know directories in an output of an LS command, then we will use the LS -F, which adds a forward slash at the end of every directory name. See the image below.
Note, unlike other previous commands, the F option is in capital letters.
6. Use LS -R to list files in reverse order
If you wish to list files in reverse order with the last file in the directory listed first, use the LS -R command. See the image below.
From the above image, we first executed the LS command, and the first file was 1wolf14.zip, while the last was prince-of-persia.zip. With LS -R command, the files were reversed. prince-of-persia.zip became the first while 1wolf14.zip was at the last index.
7. List files and directory contents recursively with LS -R
Suppose you want to list files together with the contents of directories present in the path, then we will use the recursive (R) feature that comes with Linux systems. We will use the LS -R command.
From the image above, we first executed the LS command, which listed the files and directories present. In the second command, we ran LS -R command, which not only listed files and directories present, but also contents inside those directories.
8. Show the latest modified file with LS -LTR
You can use LS -LTR command to show the latest modified files or directories in order. The latest modified is listed as the last while the oldest outputted as the first. Take a look at the image below.
From the image, we see the oldest modified file is the ‘DSC_0627.JPG’ edited on March 13th, while the newest is ‘youtubeDownloader’ modified on April 10th.
9. List files and directories by size using LS -LAS
You can also sort your files by size using the ls -las command. The most extensive file will be displayed first while the smallest last. See the image below.
10. List the inode number of files and directories using LS -I
Inode numbers, also referred to index numbers, are unique identifiers assigned to every file and directory upon creation. To list files and directories together with their inode numbers, use the LS -I command. See the image below.
11. List UID and GID values with the LS -N
To list files and directories together with their User Identifier(UID) and Group Identifier(GID) values, use the LS -N command. See the image below.
In the above image, column three and four represent the UID and GID values, respectively.
12. List information of a directory using the LS -LD
When we run the LS -L command on a directory, it will output all the contents and information about the file. If you wish to list only the information about the directory/folder, then we will use the LS -LD command. See the image below.
From the above image, we first executed the LS command on the lib folder. It outputted the contents of the lib directory. The second command, we ran the LS -LD command that returned information about the lib directory.
13. List all usages of LS command using LS -HELP, and MAN
Assuming you are a Linux newbie who wants to learn about the LS command or a user who wants to exhaust all the options that come with the LS command, there are two ways you can achieve that. Use the LS –HELP command.
Else, use the MAN LS command.
Those are some of the LS commands you can use in your everyday activity with Linux systems. The command-line way of listing files is quite useful for managing servers without a graphical user interface or in combination with other commands like grep to filter results. Do you have a command that you want to share with our readers? Feel free to post in the comment section below. If you find this article useful, feel free to share the link with friends.