Linux Commands

How to Run ‘ls’ Command in Linux?

In Linux operating systems, the Command-line interface is a very effective feature that permits the user to interact with the system. The “ls” is one of the most powerful commands that displays all the directories and files from the system. The “ls” is an elementary command that any Linux user should know. It displays basic information about files and folders inside the file system. It is included in the GNU core utility kit, which comes standard with all Linux distributions.

In this post, we will cover the “ls” command in-depth and how it works with different flags:

Syntax:

The syntax of the “ls” command is:

$ ls [ option ]

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.

$ ls

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:

$ ls /

Displaying subdirectory:

To display the subdirectory of any specific directory, pass the directory name as an argument with the “ls” command.

$ ls /<directory_name>
$ ls /home

It displays that the user is the subdirectory of the home.

$ ls /home /home/user

Display all the subdirectories/ files.

Displaying only directories:

Execute the below command to show all the directories:

$ ls –d */

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:

$ ls Documents

Displaying files with subdirectories:

If you want to see what is inside the directories. Run the below-mentioned command:

$ ls *

Displaying the files in long list format:

The “-l” (lowercase L) option is used to list all existing files and directories with their details:

$ ls –l

file access drwxr-xr-x
number of links to file 2
File owner User
File group User
File size 4096
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 /<filename>
$ ls –l /home

Displaying hidden files:

Use the “-a” option with the “ls” command to display all hidden files:

$ ls -a

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:

$ ls -al

Displaying files in reverse order:

User “-r” option to display the files and directories in reverse order:

$ ls -r

Displaying the files alphabetically with extension:

Use the “-x” option to display the files with extensions and directories alphabetically:

$ ls -x

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:

$ ls –t

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:

$ ls -ltr

Displaying files in human-readable order:

Use the “-h” option to display the files in a readable format for humans:

 $ ls -h

Add “/” character with all directories:

Use the “-p” option to append the “/” character with each directory:

$ ls -p

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:

$ ls –l -h

Displaying files size:

To display the file and folder size, use the “-s” option with the “ls” command:

$ ls -s

Sorting by size:

The “-lS” option sorts files and directories by their sizes. The larger files will be displayed first:

$ ls -lS

Displaying UID and GID:

Use the “-n” option with the ls command to display the User ID and Group ID of files and folders.

$ ls -n

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:

$ ls –R

Displaying the last edited file:

To print the latest modified file in a directory, combine head with ls command:

$ ls –t | head -1

Displaying the oldest edited file:

To print the very first edited file in a directory, combine the tail with the ls command.

$ ls –t | tail -1

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:

$ ls -i

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:

$ ls -1

Checking version:

Use the “–version” as a parameter to check the version of the ls command.

$ ls --version

Conclusion:

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.

About the author

Aqsa Maqbool

As a Software engineer, I am passionate to write about various IT related
articles but have deep interest in Linux. I spend most of my time reading Linux related blogs and IT related books. I want to serve the world with my writing skills.