Linux "ls" Command with Examples
In Linux, the command "ls" is one of the most commonly used. It's used to display a list of files and sub-directories in the current directory. If you're new to using the command line, the first command you should learn is probably ls.This command can be used by both regular users as well as system administrators.
The ability to view what files are in a directory is what makes ls so important. It will be used frequently to display directory contents. Although it is not a complicated command, it does come with a number of options for listing files with additional information. Even while ls is always enough to list contents, you're likely to find a few of these options highly handy.
Linux Command "ls" options
A Linux command has the following basic syntax:
ls [ Options ] [File]
Following are some frequently used options in Linux ls commands:
Options | Description |
---|---|
ls -a | list all files including hidden file starting with '.'. |
ls -d | list directories - with ' */'. |
ls -l | list with long format - show permissions. |
ls -F | Append indicator (one of */=>@|) to entries. |
ls -lh | This command will show you the file sizes in human readable format. |
ls -r | list in reverse order. |
ls -i | list file's inode(index) number. |
ls -ltr | View Reverse Output Order by Date. |
ls -t | sort by time & date. |
ls -n | It is used to print group ID and owner ID instead of their names. |
ls -m | A list of entries separated by commas should fill the width. |
ls -g | This allows you to exclude the owner and group information columns. |
ls -q | Force printing of non-graphic characters in file names as the character `?';. |
ls -Q | Place double quotations around the entry names. |
Basic examples of Linux ls command
Here, we will look at the basics of ls command examples in a Linux environment with all the available options.
- The 'ls' command is used to list files and directories
- Display the hidden files and directories
- Display complete information about the files
- Classify the files with special characters
- Display File Index Number
- View Last Edited File
- Display File Size in Human Readable Format
- Display Reverse Output Order by Date
- List all the files and directories in reverse order
- List UID and GID of files and directories
- List the files and directories seperated by a comma
- List all the files and directories without the owner details
- Display sub-directories without other files
- Display version of ls command
- Display ls command help page
The 'ls' command is used to list files and directories
The contents of your current working directory, which is just a technical way of stating the directory that your terminal is presently in, will be listed if you run the "ls" command without any further options.
ls
Display the hidden files and directories
Use the -a option of the ls command to show hidden files and directories in the current directory.
ls -a
The files that start with the dot are hidden (.). The current directory (.) as well as the parent directory (..) are displayed by "ls -a".
Display complete information about the files
The "ls -l" option displays the contents of the current directory in a long listing format, one per line. The line begin with the file or directory permission, owner and group name, file size, created/modified date and time, file/folder name as some of the attributes.
ls -l
Classify the files with special characters
The ls command categorises the files using the -F parameter. It signifies, Directories that ends with a slash (/), Files that can be executed with a trailing asterisk (*), Symbolic links with a trailing at the rate symbol (@), FIFOs with a trailing vertical bar (|), and
Regular files that contain nothing.
ls -F
Display File Index Number
For internal purposes, you may need to know the index number of a file. To display the index number, use the "ls -i" option. You can remove files with special characters in their names by using the index number.
ls -i
View last edited file
The most recently modified file is displayed first as the file is sorted by modification time. Use the ls and head commands together to access the most recently edited file in the current directory.
ls -t
ls -t | head -1
Display File Size in Human Readable Format
Another frequent ls option is -h or -human-readable and -h should be used with -l and -s to print sizes like 1K 234M 2G etc.. This will display the file sizes in human-readable format rather than bytes.
ls -lh
When you use ls -lh
, it will display the entire information about the file or directory name and when you use ls -sh
it will display only the size and the filename or directory name.
Using ls -lh:
Using ls -sh:
Display Reverse Output Order by Date
In the above command l argument is used for long listing format, t argument sorts all the files and directories based on the modification time and lists the newest first, and r argument is used to reverse the sorting order.
ls -ltr
As a result, the ls -ltr command long lists all the directories and file names by sorting the modified date in reverse order.
List all the files and directories in reverse order
The option "ls -r" lists all files and directories in reverse order. The files and directories are all arranged in reverse alphabetical order.
ls -r
List UID and GID of files and directories
The "ls -n" command displays the UID (User ID) and GID (Group ID) of each file and directory, one per line. A typical user and group (UID and GID) have 1000, but the root UID and GID have zero.
ls -n
List the files and directories separated by a comma
The "ls -m" command displays all files and directories separated by a comma.
ls -m
List all the files and directories without the owner details
The "ls -g" option is similar to the "ls -l" option, however the '-g' option skips the file and directory owner details.
ls -g
Display the Sub-directories without other files
This "ls -d */" command can be used to display only the sub-directories and hide all other files.
ls -d */
Display Version of ls command
By using this "ls --version" command, you can check the version of ls command.
ls --version
Display ls command Help page
By using this "ls --help", you can a guide for ls command. It has more options. Some of them are given below for the reference.
ls --help
WindUp
In this article, Some of the options for the ls command are listed above with the examples. It is one of the simplest command in linux. Even if you are familiar with this commands, you might not be familiar with all of the specified circumstances.
Monitor Your Entire Application with Atatus
Atatus is a Full Stack Observability Platform that lets you review problems as if they happened in your application. Instead of guessing why errors happen or asking users for screenshots and log dumps, Atatus lets you replay the session to quickly understand what went wrong.
We offer Application Performance Monitoring, Real User Monitoring, Server Monitoring, Logs Monitoring, Synthetic Monitoring, Uptime Monitoring and API Analytics. It works perfectly with any application, regardless of framework, and has plugins.
Atatus can be beneficial to your business, which provides a comprehensive view of your application, including how it works, where performance bottlenecks exist, which users are most impacted, and which errors break your code for your frontend, backend, and infrastructure.
If you are not yet a Atatus customer, you can sign up for a 14-day free trial .
#1 Solution for Logs, Traces & Metrics
APM
Kubernetes
Logs
Synthetics
RUM
Serverless
Security
More