Open In App

ls Command in Linux

Last Updated : 11 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

OptionsDescription
-lknown as a long format that displays detailed information about files and directories.
-aRepresent all files Include hidden files and directories in the listing.
-tSort files and directories by their last modification time, displaying the most recently modified ones first.
-rknown as reverse order which is used to reverse the default order of listing.
-SSort files and directories by their sizes, listing the largest ones first.
-RList files and directories recursively, including subdirectories.
-iknown as inode which displays the index number (inode) of each file and directory.
-gknown as group which displays the group ownership of files and directories instead of the owner.
-hPrint file sizes in human-readable format (e.g., 1K, 234M, 2G).
-dList 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
ls -t

ls -t

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

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 
ls -l

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

FieldExplanation
 normal fileRepresents a regular file
d : directoryIndicates the file is a directory
s : socket fileIndicates the file is a socket
l : link fileIndicates the file is a symbolic link
Field 1 – File PermissionsNext 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 linksSpecifies the number of links to the file. For example, 1 means there’s only one link to this file.
Field 3 – OwnerShows who owns the file. Example: the owner could be a username like maverick.
Field 4 – GroupSpecifies the group the file belongs to. Example: maverick group.
Field 5 – SizeIndicates the file size in bytes. For instance, 1176 means the file is 1176 bytes in size.
Field 6 – Last modified date and timeShows when the file was last modified. For example, Feb 16 00:19 indicates the last modification time.
Field 7 – File nameThe 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
ls -lh

ls -lh

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.).
ls -l /etc

ls -l /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.
ls -ld /etc

ls -ld /etc

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

ls -lt

ls -lt

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.

ls -ltr

ls -ltr 

 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

ls -a

ls -A

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

ls -A

ls -A 

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 /etc/apt

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. 

ls -R /etc/apt

ls -R /etc/apt 

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
ls -i

ls -i

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

ls -i /etc/apt
ls -i /etc/apt

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.

ls -q

ls -q 

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. 

ls -n ~/kv

ls -n ~/kv

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
 ls -F

 ls -F

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

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
image

–time -style flag

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.



Next Article

Similar Reads

close