If you have worked in Linux, you may be familiar with the
ls command. Ls is short for “list”. This command lists information about directories and any type of files in the working directory.
In this article, you will learn to use crucial
ls commands. Even if you have existing knowledge of these commands, this article will help you refresh your knowledge.
- Linux system
- Access to the terminal or command prompt
ls Command Syntax
Let’s start by explaining the syntax of the
[options] – This lets you add additional instructions to the
ls command. Examples will be provided in the following sections of the article.
Note: Additional instructions are case sensitive, so
-r displays different output than
ls Commands With Examples
The most basic
ls command is without the use of options. It prints out files and directories in their bare format. With this command, you will not be able to see file types, dates, and permissions.
To execute this command, type
ls in terminal window and press Enter on your keyboard.
ls Command Additional Options
The additional options detailed below give users more flexibility when using the
ls -F in a terminal window to add “
/” at the end of each directory. This command will help you to distinguish directories from files. See image below for further details:
When you type the
ls -m command, the terminal prints out directories and files separated by a comma:
ls -Q command to add quotation marks to all directories and files as in the image below:
To get the Inode (index node) number of all directories and files, type
ls -i in your terminal:
There are three different sorting options that you can use:
You have the option to sort directories and files in the reverse order by typing
ls -r. See example in the image below:
ls -t command to sort directories and files by time and date of creation or modification:
To sort directories and files alphabetically by entry extension type
ls -X in a terminal:
Note: If you want to learn how to sort file contents, refer to Linux sort command.
View Hidden Files
When using the basic
ls command, you can’t see hidden files and files starting with “
.”. Type the
ls -a command to display them as seen in the image below:
To get a full list of hidden files, type
ls -la in your terminal. The output displays information about the user, size of the file, and date and time of modification.
Note: To learn how to hide and see hidden files in Linux, refer to our Show Hidden Files in Linux article.
Directory Trees With the ls Command
If you want to access long listing directory trees, type
ls -R. The example below displays the expected output.
ls -lR in the terminal to display additional information on the directory tree, such as the owner of the file, size, and date and time of the last modification, as seen in the image below:
Advanced ls Commands
So far, you have learned simple
ls commands that display a limited amount of information about directories and files. You should use advanced
ls commands to access extensive information about your files and directories. Advanced
ls commands are:
ls -l /tmp
ls -ld /tmp/
View Long Listing Format
ls -l command to print out a long listing format of files and directories. The output displays information, such as file or folder name, owner of the file and its permission, size and modified date and time as in the image below:
List UID and GID of Files
ls -n command in terminal to display the UID (unique identifier) and GID (group ID) assigned to all files and directories:
Display Files in Human Readable Format
If you want to check the size of files and directories in a human readable format, type
ls -lh in terminal. The output displays content as in the image below:
View Reverse Output Order by Date
When you type
ls -ltr, the terminal prints out a long listing format of files and directories with the latest modification date:
List Files by Size
ls -lS command to receive long listing format of files and directories sorted by file size, from largest to smallest.
Display Files Under /tmp Directory
If you want to access files in the /tmp directory, type
ls -l /tmp in terminal. The output should be similar to the one seen below:
ls -ld /tmp/ when you want to check information about the /tmp directory only, without accessing any files in the directory.
View All ls Commands
To list all
ls command options, type
ls --help. The system lists all available options.
Note: Next, learn how to create files in Linux text editors, such as Vim. See our Vim Cheat Sheet article that lists the most crucial Vim commands.
After reading this article, you have learned how to use the most important
ls commands in Linux. Working with files and directories is crucial, and next we recommend learning how to create a file in Linux.