When using Linux, you are going to almost always be hooked to the terminal. And one of the common commands that you’ll have at the back of your hand will be the “ls” command.
What is the ls command in Linux?
The “ls” command is short for “List” and is used to list directories and files in the folder where the command is run. Simple isn’t it?
Now just like it is with everything else in Linux, this command has multiple options for listing files. Let’s go over the most common options that you’ll use very often.
You can read through the manual pages for a comprehensive understanding of the command (or you can use the “–help” option to get a simplified list of available options).
How to use the ls command?
It’s a pretty straightforward command, but combined with the available options, it can be pretty useful. Let’s understand some common usage of the ls command.
1. ls command default or basic usage
To simply list the files in a directory, you can enter the command as is, and you’ll receive an output with the list of files in a tabulated format as the default setting.
[user@localhost ~]# ls
2. Listing all files with the ls command
The default usage does not show you the hidden files in the directory that you’re working in. Even if you haven’t manually hidden any files, Linux saves configuration files as hidden (by adding a period before the name of the file). We do this with the use of the “-a” option. Give the below command a try and see what hidden files you find.
[user@localhost ~]# ls -a
You see, in the same directory, I had the “.Xauthority” and “.xsession” configuration files hidden which are now revealed with the use of the ls -a command.
3. Getting a detailed list of files
On multiple occasions, you will need to figure out what permissions a user has for a file or folder. When you log in as “root” you have full access to any file that you want, but as a standard user, that is not the case. We can find the full list of files with the “-l” option. Since I’m on a full access system, I do not have to worry about file permissions but you can run the following command to see what kind of access you have.
[user@localhost ~]# ls -l
4. Listing files in a sorted format
Let’s introduce a new concept to this tutorial. Combining command options. You can combine options by either writing them separately like “ls -l -a” or “ls -la”. Both will provide the same output. When sorting, we’ll want the full detailed list of files so if we sort for size, we can also see the size instead of just the sorted list.
Here are a few options you can use to sort your lists (the Linux terminal is case sensitive, so -S is not the same as -s):
- -S sorts by size
- -t sorts by modification time
- -r reverses the default order of the file list
- -h will show the file size in a human-readable format i.e. kb, MB, GB etc. (this is not for sorting but helps when sorting by size)
I’ll combine all the above options with the -l option so we see the full detailed list.
[user@localhost ~]# ls -lS -h OR [user@localhost ~]# ls -lSh [user@localhost ~]# ls -lt [user@localhost ~]# ls -lr
5. Listing all files recursively
In the above examples, you must have observed two listed files are highlighted in blue color. Those are folders. What if I want to get a list of all the files within my current working folder, along with all the files in all the folders too? The ls command makes it simple for us to do. With the use of the “-R” option, you can get just that. It will first list all the files within the current working directory (cwd which is also another command in Linux), and then list the folders and the files within them. Let’s see a quick example.
[user@localhost ~]# ls -R
You should now have a good and practical understanding of the use of the ls command in Linux. Play around with the options, use the “–help” option to find out more comprehensive options that the command offers. If you’re further interested in learning about the command, Linux manual pages for the ls command provide you with really detailed information.
To access the man pages, just enter “man <any command you wish to know more about>”. For example, to read man pages for ls, enter:
[user@localhost ~]# man ls