Normally, the work of system administrators consists of an almost permanent use of the terminal; this is because servers with GNU/LINUX-based systems, for security reasons, do not have a graphical desktop environment. And this means that we have to use the terminal for basically everything from the simplest or most complex tasks. Today we will learn the ls command utility that allows us to list files and folders from the terminal. Here we will also describe different combinations and options that can be used with ls command to get different output.
LIST FILES IN LINUX: “ls” COMMAND
As it was said, the command ls allows you to list files and folders of a specific path, is easy to use and is very useful and used in the daily work. The command has several options to improve the listings. In a terminal, we can invoke the help of the command ls in this way.
ls --help
Above command will display all options available for ls command to list the files and folders. In this way we get the syntax of usage and options as output per terminal.
As all options will not cover in one single screenshot so i have scrolled the above screen and taken another screenshots to cover all options in screenshots that has been displayed below.
As we can see the documentation is extensive, but don’t worry we will examine the most useful and used options. The basic syntax of the command is:
ls [options] [file|dir]
Although it is possible to use ls from the most basic form only by typing:
#List files from your given directory. ls
With this we only list the files that are in the directory where we are located in the terminal. For example, if we wanted to list everything in the root directory of our GNU/LINUX distribution it would be with the command:
#List everything in the root directory ls /
It was easy, wasn’t it?
Most Important Options of the Command “ls”
ls as well as all GNU/LINUX commands have options that modify the basic command execution result. Here is a list of the most outstanding ones for this command.
Options | Description |
-a | Do not ignore entries starting with . |
-A | Do not list implied . And .. |
-d | List directories themselves, not their contents |
-i | print the index number of each file |
-l | Use a long listing format. Displays information such as permissions and owner of the folder or file. |
-r | reverse order while sorting |
-R | List subdirectories recursively. Displays the contents of the directory subfolders. |
-s | print the allocated size of each file, in blocks |
-S | sort by file size, largest first |
-t | sort by modification time, newest first |
-U | do not sort; list entries in directory order |
-X | sort alphabetically by entry extension |
Examples of Command “ls”
Here i have given different examples of command ls that you can use to fulfill different requirements.
List Hidden Files
We can use option -a if we want to list the hidden files of a specific folder, it would be enough to write:
ls –a path
In my case it would be something like that:
ls –a /home/angelo/
You can see all hidden files as well in above screenshot. We can identify hidden files by looking at the output as such files or folders start with dot (.).
Display Files with their Index Number
We can list files with their index number by running below command. To show files/folders with their index number we will use the option –i along with command ls. You can see i have used below command and output is showing in below image.
ls -i /home/angelo/
List Files with full Details (Permissions, Index no, Owner, Create Date etc.)
We can use ls command to display information such as permissions and owner of the folder or file. We will be using option -I to show complete information about files and folders as shown in below screenshot.
ls -l /home/angelo/
This output shows us more specific information about what we have in the folder: First it shows the permissions on the folder, its index number, the owner of the folder, who created the folder, its size in kb, the date it was modified and the folder name.
List Files or Folders in a Reverse Order
ls command also gives an option to sort files and folders in reverse order. We can use -r option to show files and folders in a reverse order as shown in below screenshot after running below command.
ls –r /home/angelo/
List Subdirectories Recursively
We can List subdirectories recursively by adding the -R option with ls command. It will display the contents of the directory subfolders. Run below command to display the result.
ls –R /home/angelo/
Display list of Directories Only
We can use -d and an asterisk option to list and show only the directories. Have a look at below command that has displayed the list of directories under /home/angelo.
ls –d */home/angelo/
Display Files Sort by their File Size
Run below command to display files sort by file size. It is possible with the option –S.
ls -S /home/angelo/
Sort by Date/Time
We can option -t to display files sort by date/time. Run below command to see the output that is also showing in below screenshot.
ls –t /home/angelo/
Display Files as they were Created
Use option -U to show folders and files in no order at all. It will display just as they are created.
ls –U /home/angelo/
Sort by File Extension
We can use option -X with ls command to sort files by their extension names. You can see this in below screenshot.
ls –X /home/angelo/
Sort by Modified Files
We can use combination of options -ltr to show last modification file or directory date. Simply run below command to display the output.
ls -lrt ls -lrt [path]
In conclusion we can say that the ls command, even when it is relatively easy to use, is one of the tools most used by system administrators.
I hope you like this article. Please follow our Facebook page and Twitter handle to get latest updates.
Read More:
- RHEL Interview Questions & Answers
- Linux User Administration – Add, Delete & Modify User Accounts
- Install HTop and Use it to Monitor Linux Processes
- How to Change Host Name of RHEL system?
- How to Reset or Change Linux User Account Passwords?
- How to Use CAT Command on Linux System? - April 24, 2018
- How to Install Zabbix on Ubuntu Server 16.04? - March 29, 2018
- 11 Examples of ls Command to List Files in Linux - March 20, 2018
Leave a Comment