On Linux, the “ls” command is one of the most commonly used. Used to display a list of files and subdirectories 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 normal users and system administrators.
The ability to see what files are in a directory is what makes ls so important. It will be used frequently to display the contents of the directory. Although it is not a complicated command, it comes with a number of options to list files with additional information. Although ls is always enough to list content, you are likely to find some of these options very useful.
Linux “ls” command options
A Linux command has the following basic syntax
:ls [ Options ] [File]
The following are some frequently used options in Linux ls commands:
Options Description ls -a list of all files, including the hidden file that begins with ‘.’.
ls -d list directories – with ‘ */’. List ls -l with long format – show permissions. ls -F Add the flag (one of */=>@|) to the entries. ls -lh This command will show you the file sizes in human-readable format. List ls -r in reverse order. ls -i list file’s inode(index) number. ls -ltr See reverse output order by date. ls -t sort by time and date. ls -n Used to print the group ID and owner ID instead of their names. ls -m A comma-separated list of entries should fill the width. ls -g This allows you to exclude the owner and group information columns. ls -q Force printing non-graphic characters in file names such as the character ‘?’;. ls -Q Place double quotation marks around the names of the entries.
Basic
examples of the Linux ls command
Here, we will look at the basics of ls command examples in a Linux environment with all available options.
The ‘
- ls’ command is used to enumerate
- complete information about
- Classify files with special characters
- index number
- View last edited file
- file size in human-readable format
- reverse
- UID and GID list
- List all files and directories
- subdirectories without other
- help
- to enumerate files and
- directories
files and directories Show hidden files and directories Show
files
Show file
Display
Show reverse output order by date List all files and directories in
order
of files and directories List files and directories separated by a comma
without owner details Show
files Display version of ls command Show
The
page for the ls command The ‘ls’ command is used
The contents of your current working directory, which is just a technical way of indicating the directory your terminal is currently in, will be listed if you run the “ls” command without further options. ls
ls Show hidden files and directories
Use the -a option of the ls command to display hidden files and directories in the current directory.
ls -a
Files that begin with the period are hidden (.). The current directory (.) as well as the parent directory (..) are displayed by “ls -a”.
ls -a
Show complete information about files
The “
ls -l” option displays the contents of the current directory in a long list format, one per line. The line starts with file or directory permission, owner and group name, file size, date and time of creation/modification, file/folder name as some of the attributes.
ls -l
-l command Classify files with
special characters
The ls command categorizes files using the -F parameter. Means, Directories ending with a forward slash (/), Files that can be executed with a trailing asterisk (*), Symbolic links with a final speed symbol (@), FIFO with a slash final vertical (|) and Regular files that contain nothing.
ls -F
Show
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 when 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
ls -t
Show file size in human-readable format
Another frequent ls option is -h or –human-readable and -h should be used with -l and –s for print sizes like 1K 234M 2G, etc. This will display file sizes in human-readable format instead of bytes.
ls -lh When you use ls -lh, it displays all the information about the file or directory name and
when you use ls -sh it displays only the size and name of the file or directory name.
Using ls -lh:Linux ls –
lh command Using
ls -sh:
Show reverse output order by date
In the preceding command, the l argument is used for the long list format, the t argument sorts all files and directories according to the modification time and lists the most recent first, and the r argument is used to reverse the sort order.
ls -ltr As a result, the ls -ltr command lists all directories and
file names at length by ordering the modified date
in reverse order. <img src="https://www.atatus.com/blog/content/images/2022/06/linux-command-ls-i-1.png" alt=""
List all files and directories in reverse order The “ls -r” option lists
all files and directories in reverse order
. Files and directories are sorted alphabetically in reverse.
ls -r
-r command UID and GID list 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
ls -n List files and directories separated by a comma The “
ls -m” command displays all files and directories separated by a comma.
ls -m
-m command
List all files and directories
without owner details The “ls -g” option is similar to the “ls -l” option, however, the ‘-g
‘ option ignores the file owner and directory details. ls –g
=”https://www.atatus.com/blog/content/images/2022/06/linux-command-ls-lh-1.png” alt=”” />Linux ls -g command Show subdirectories without other files This “
ls -d */” command can be used to show only subdirectories and hide all other files.
ls -d */Linux Command ls -d */”
Display version of
ls command Using this “ls -version” command
, you can check the version of the ls command. ls -version
<img src="https://www.atatus.com/blog/content/images/2022/06/linux-command-ls-sh-1.png" alt=""
ls -version Show ls command
Help page
By using this “ls -help”, you can get a guide to the ls command. You have more options. Some of them are given below for 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 commands in Linux. Even if you are familiar with these commands, you may not be familiar with all the specified circumstances.
Monitor your entire
application with Atatus
Atatus is a full-stack observability platform that allows you to review issues as if they occurred in your application. Instead of guessing why errors occur 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, log monitoring, synthetic monitoring,
uptime monitoring, and API analytics. It works perfectly with any application, regardless of the framework, and has plugins.
Atatus can be beneficial to your business as it provides a complete view of your application, including how it works, where performance bottlenecks exist, which users are most affected, and what bugs break your code for your frontend, backend, and infrastructure.
If you’re not already an Atatus customer, you can sign up for a 14-day free trial.