In this post, we will cover the “ls” command in-depth and how it works with different flags:
The syntax of the “ls” command is:
How to use the “ls” command:
Displaying all files:
To see all the files and folders, Fire up the terminal and type “ls” without any option or argument.
It displays all the directories and files that are existing in that particular directory.
Using “ls” Command with different options:
List Root directory:
To display the content of the root directory, use:
To display the subdirectory of any specific directory, pass the directory name as an argument with the “ls” command.
$ ls /home
It displays that the user is the subdirectory of the home.
Display all the subdirectories/ files.
Displaying only directories:
Execute the below command to show all the directories:
Displaying the content of a specific Folder/Directory:
If you want to display the content of any directory, type directory name with the ls command:
Displaying files with subdirectories:
If you want to see what is inside the directories. Run the below-mentioned command:
Displaying the files in long list format:
The “-l” (lowercase L) option is used to list all existing files and directories with their details:
|number of links to file||2|
|Date and time||Mar 6 06:39:|
|File/Folder name||Desktop, Videos…|
Displaying specific file or directory content:
Use the “-l” option to print the complete information of any specific directory, run the following command:
$ ls –l /home
Displaying hidden files:
Use the “-a” option with the “ls” command to display all hidden files:
A file that begins with a dot(.) are hidden files.
Displaying the content of hidden files:
Use a combination of “-a” and “-l” with the “ls” command to display the content of all hidden files:
Displaying files in reverse order:
User “-r” option to display the files and directories in reverse order:
Displaying the files alphabetically with extension:
Use the “-x” option to display the files with extensions and directories alphabetically:
Displaying most recent modified files and directories:
To sort the files and folders according to their modified date, recently modified files will be displayed on top; use the “-t” option with the “ls” command:
Use the combination of the “-l” “t” and “r” options to look at the contents of recently edited files and directories in reverse order. Consider the below-mentioned command:
Displaying files in human-readable order:
Use the “-h” option to display the files in a readable format for humans:
Add “/” character with all directories:
Use the “-p” option to append the “/” character with each directory:
Displaying the long list format in human-readable format:
Use “-l” and “-h” options to display the size of files in a human-readable format:
Displaying files size:
To display the file and folder size, use the “-s” option with the “ls” command:
Sorting by size:
The “-lS” option sorts files and directories by their sizes. The larger files will be displayed first:
Displaying UID and GID:
Use the “-n” option with the ls command to display the User ID and Group ID of files and folders.
Displaying files and directories recursively:
The “-R” option shows the files recursively. That means the folders and their contents will display recursively, which helps us to identify the deepest path:
Displaying the last edited file:
To print the latest modified file in a directory, combine head with ls command:
Displaying the oldest edited file:
To print the very first edited file in a directory, combine the tail with the ls command.
Displaying files Inode number:
Use the “-I” option to print the files/directories in the inode number. As you can see, the file name is preceded by a number:
Displaying one file/directory in one line:
We can display that only one file is specified in a single line. This is commonly used to include a file list as an input to other commands or programs. Use -1 (one) with ls:
Use the “–version” as a parameter to check the version of the ls command.
The ls command can display detailed information about files and directories and display hidden files, sort them by their sizes, and perform recursive listing. We have thoroughly explained the usage of the ls command with various options.