ls Command in Linux
The ls command is one of the most used commands in the Linux terminal to display the files and directories or path in the terminal. So, using the ls command is a basic skill for navigating the Linux file system, handling files, and managing directories.
What is the ls Command in Linux
The ls command is used to list all directories and files in the Linux terminal. When we run the command basically it displays the contents of you current directory or folder so that users can view the names of file, directories, and their information. The ls command use the default option to show the files in alphabetical order but it can be modified also with different options to display extra information such as permissions, ownership, and size.
Syntax of `ls` command in Linux
ls [option] [file/directory]
Commonly Used Options in `ls` Command
The ls command has several options that allow users to customize the output. Here are the most common options used with ls:
Options | Description |
---|---|
-l | known as a long format that displays detailed information about files and directories. |
-a | Represent all files Include hidden files and directories in the listing. |
-t | Sort files and directories by their last modification time, displaying the most recently modified ones first. |
-r | known as reverse order which is used to reverse the default order of listing. |
-S | Sort files and directories by their sizes, listing the largest ones first. |
-R | List files and directories recursively, including subdirectories. |
-i | known as inode which displays the index number (inode) of each file and directory. |
-g | known as group which displays the group ownership of files and directories instead of the owner. |
-h | Print file sizes in human-readable format (e.g., 1K, 234M, 2G). |
-d | List directories themselves, rather than their contents. |
Practical Examples of the ls command
Here are some examples of how to use the ls command in Linux for different tasks:
1. Open Last Edited File Using `ls -t `
This options lists all the files in the folder which is sorted by time which means that newest file comes first. When we use the head option in it they simply picks the first file from the list (which is your most recently changed file).
ls -t | head -1

Note: This will open the last file you edited (i.e second.txt)
2. Display One File Per Line Using `ls -1 `
If you want to list all files and folders in your current directory with each name on a new line, you can use the ls -1
command
ls -1

3. Display All Information About Files/Directories Using `ls -l`
The ls -l
command in Linux is used to list the detailed information about the files and directories in the current folder.
ls -l

-rw-rw-r-- 1 maverick maverick 1176 Feb 16 00:19 1.c 1st Character – File Type: First character specifies the type of the file. In the example above the hyphen (-) in the 1st character indicates that this is a normal file. Following are the possible file type options in the 1st character of the ls -l output.
Field Explanation
Field | Explanation |
---|---|
– normal file | Represents a regular file |
d : directory | Indicates the file is a directory |
s : socket file | Indicates the file is a socket |
l : link file | Indicates the file is a symbolic link |
Field 1 – File Permissions | Next characters specify the file’s permission. Every 3 characters represent read, write, execute permissions for user (root), group, and others respectively. Example: -rw-rw-r-- means read-write for user, read for group, and no permission for others. -rwxrwxrwx means full access. |
Field 2 – Number of links | Specifies the number of links to the file. For example, 1 means there’s only one link to this file. |
Field 3 – Owner | Shows who owns the file. Example: the owner could be a username like maverick . |
Field 4 – Group | Specifies the group the file belongs to. Example: maverick group. |
Field 5 – Size | Indicates the file size in bytes. For instance, 1176 means the file is 1176 bytes in size. |
Field 6 – Last modified date and time | Shows when the file was last modified. For example, Feb 16 00:19 indicates the last modification time. |
Field 7 – File name | The name of the file. For instance, the filename is 1.c . |
4. Display File Size in Human Readable Format Using `ls -lh`
If you want see the file size, permissions, owner and date than use the below command:
ls -lh
-l
: lists files in long format (with permissions, owner, size, and date)-h
: shows the file size in a human-readable format like KB, MB, or GB instead of just raw bytes

5. Display Directory Information Using `ls -ld`
When you use “ls -l” you will get the details of directories content. But if you want the details of the directory then you can use -d option as., For example, if you use ls -l /etc will display all the files under the etc directory. But, if you want to display the information about the /etc/ directory, use -ld option as shown below.
ls -l /etc
-l
means long listing format (it shows permissions, owner, size, etc.).

If you want to see the details about a directory itself (and not the files inside it), you can use:
ls -ld /etc
-d
tells the system to show info about the directory itself, not what’s inside it.

6. Order Files Based on Last Modified Time Using `ls -lt`
If you want to see which files were recently changed or updated than sue the below command:
ls -lt
It lists all files and folders sorted by the last modified time

7. Order Files Based on Last Modified Time (In Reverse Order) Using `ls -ltr `
If you want to see which files were changed or updated last then use the below command:
ls -ltr
-l:
shows detailed info like size, permissions, and modification time-t:
sorts files by last modified time (newest first)-r:
reverses the order (oldest first)
To sort the file names in the last modification time in reverse order. This will show the last edited file in the last line which will be handy when the listing goes beyond a page.

8. Display Hidden Files Using ls -a (or) ls -A
In Linux some file or folder starts with a dot (.
) which means that it is hidden. These hidden files are usually used to store settings or configuration details for apps and the system.
ls -a
The above command shows all files, including the hidden ones like .bashrc
, .git
, or .config
, along with the current .
and parent ..
directories.

ls -A
To show the hidden files, but not the ‘.’ (current directory) and ‘..’ (parent directory).

9. Display Files Recursively Using ls -R $ ls /etc/apt
This command lists all files and folders inside /etc/apt
which includes the subfolders and their contents.
ls /etc/apt

ls -R /etc/apt
To show all the files recursively. When you do this from /, it shows all the unhidden files in the whole file system recursively.

10. Display File Inode Number Using `ls -i`
Every file in Linux has a unique ID number called an inode number. This number keeps track of important details about the file like its location on disk, size, permissions, and timestamps but not its name.
To see the inode number of a file or folder use the below command:
ls -i

If you list the content of the /etc/apt
directory along with their inode numbers.
ls -i /etc/apt

11. Hide Control Characters Using `ls -q`
Sometimes, files or folders may have irregular or non-printable characters which is called control characters in their names basically they can mess up your terminal display or be confusing to read.
Use ls -q
command is a safe way to list files while hiding those confusing characters.
ls -q
To print question marks instead of the non-graphics control characters.

12. Display File UID and GID Using `ls -n`
it shows the UID (User ID) and GID (Group ID) of the files and folders instead of just showing usernames and group names.
This command tells you which user and group actually owns a file or folder by showing the UID (User ID) and GID (Group ID) of the files and folders instead of just showing usernames and group names.
ls -n ~/kv
Lists the output like -l, but shows the uid and gid in numeric format instead of names.

13. Visual Classification of Files with Special Characters Using `ls -F`
The ls -F
command in Linux helps you quickly identify file types by adding special symbols at the end of each file name.
ls -F
- / – directory.
- nothing – normal file.
- @ – link file.
- * – Executable file

14. Visual Classification of Files with Colors Using `ls -F`
Recognizing the file type by the color in which it gets displayed is another kind in classification of file. In the below output directories get displayed in blue, soft links get displayed in green, and ordinary files get displayed in default color.
ls --color=auto

15. Change the way time information is displayed using --time-style flag.
When you list files using the ls -l
command in Linux, it shows the last modified time of each file. This flag lets you customize the format of the time/date shown next to each file or folder.
ls -l --time-style=long-iso
This command will display the time in YYYY-MM-DD HH:MM format. There are other options like
- locale
- full-iso
- iso

Conclusion
The ls command
in Linux is an essential tool for managing files and directories. With its various options, you can tailor the output to suit your needs—whether you need detailed information about files, hidden files, or want to sort and categorize your data. By mastering the ls
command, you'll become more efficient at managing and navigating the Linux file system, improving your overall workflow.