As the name suggests, the “ls” stands for the list. The Linux ls command is used to list the contents of the directory or directories.
The ls command is a command-line utility for listing the contents of a directory or directories given to it through standard input. It writes results to standard output. The ls command supports displaying different information about documents, sorting on a range of alternatives, and recursive listing.
Ls command is among the most frequently used command in Linux
We use ls command daily basis and often even though we might not conscious and never use all of the ls option available. In this guide, we are going to be discussing the basic ls command where we’ve attempted to cover as many parameters as possible.
The general syntax of ls command
The syntax of the ls command is:
$ ls [options] [file|directory]
A few examples of ls command with parameters or options are given below with a little description where required.
The simple ls command without any option
The ls with no parameters list files and directories in a bare format where we will not have the ability to look at details such as file types, size, modified date and time, file permissions and links etc.
Use the –a option lists all files including hidden starting with ‘.’ This is the command example:
$ ls –a
List all subdirectories
Show all subdirectories by using the ‘*’:
$ ls *
List just text files
You may use the * wildcard with “.txt” to show only the text files:
$ ls *.txt
List directories only
$ ls -d */
List Files and Directories with’/’ Character in the end
Using -F option in the ls command will include the’/’ Character in the end of each directory.
$ ls -F
How to List Directories/Files in Reverse Order
For showing the files and directories in the reverse order, use the –r option in the ls command as shown below:
$ ls -r
Recursively listing Sub-Directories
The ls -R option will list very long list directory trees.
$ ls -R
How to Sort Files by File Size
In order to display the files sorted by file size, use the –lS option. This will sort the files from big to small size:
$ ls -lS
How to Show Help Page
Te see the help regarding ls command, just run the –help option in ls command:
$ ls --help
List Directory Information
With ls -l command list files under directory /data. Wherein with -ld parameters show information of data directory.
$ ls -l /data
Screen UID and GID of Files use –ln option
$ ls -n
List directory /home/user/Documents/Books with complete path.
$ ls /home/user/Documents/Books
List user’s home directory
In order to display the user’s home directory, for example “/home/user”, use the following command:
$ ls ~
List with the long format command
$ ls -l
Sort by date/time command
To sort the files/directories by date/time, use the –t options as shown below:
$ ls -t