The Wayback Machine - https://web.archive.org/web/20240915193440/https://www.geeksforgeeks.org/ls-command-in-linux/
Open In App

ls Command in Linux

Last Updated : 05 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

ls is a Linux shell command that lists directory contents of files and directories.  It provides valuable information about files, directories, and their attributes. 

Syntax of `ls` command in Linux

ls [option] [file/directory]

‘ls’ will display the contents of the current directory. By default, ‘ls’ lists files and directories in alphabetical order.

Commonly Used Options in `ls` command in Linux

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.

Some practical examples of the ls command are shown below. 

1. Open Last Edited File Using `ls -t `

It sorts the file by modification time, showing the last edited file first. head -1 picks up this first file. To open the last edited file in the current directory, use the combination of ls and head commands as shown below.

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 `

ls -1

ls -1

 3. Display All Information About Files/Directories Using `ls -l`

ls -l 

To show long listing information about the file/directory.

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

  • – normal file
  • d : directory
  • s : socket file
  • l : link file
  • Field 1 – File Permissions: Next characters specifyes the files permission. Every 3 characters specify read, write, execute permissions for user(root), group and others respectively in order. Taking the above example, -rw-rw-r– indicates read-write permission for user(root), read permission for group, and no permission for others respectively. If all three permissions are given to user(root), group and others, the format looks like -rwxrwxrwx
  • Field 2 – Number of links: Second field specifies the number of links for that file. In this example, 1 indicates only one link to this file.
  • Field 3 – Owner: Third field specifies owner of the file. In this example, this file is owned by username ‘maverick’.
  • Field 4 – Group: Fourth field specifies the group of the file. In this example, this file belongs to” maverick’ group.
  • Field 5 – Size: Fifth field specifies the size of file in bytes. In this example, ‘1176’ indicates the file size in bytes.
  • Field 6 – Last modified date and time: Sixth field specifies the date and time of the last modification of the file. In this example, ‘Feb 16 00:19’ specifies the last modification time of the file.
  • Field 7 – File name: The last field is the name of the file. In this example, the file name is 1.c.

4. Display File Size in Human Readable Format Using `ls -lh` 

ls -lh

ls -lh (h stands for human readable form) : To display file size in easy-to-read format. i.e i.e M for MB, K for KB, G for GB. 

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

ls -l /etc

ls -ld /etc
ls -ld /etc

ls -ld /etc

6. Order Files Based on Last Modified Time Using `ls -lt` 

ls -lt

To sort the file names displayed in the order of last modification time. You will find it handy to use it in combination with -l option. 

ls -lt

ls -lt

7. Order Files Based on Last Modified Time (In Reverse Order) Using `ls -ltr `

ls -ltr 

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 

ls -a

 To show all the hidden files in the directory, use ‘-a option’. Hidden files in Unix starts with ‘.’ in its file name.It will show all the files including the ‘.’ (current directory) and ‘..’ (parent directory).

ls -a

ls -a

ls -A 

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

ls -A

ls -A 

 [Note: . and .. are not displayed here] 

9. Display Files Recursively Using ls -R $ ls /etc/apt

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` 

Sometimes you may want to know the inode number of a file for internal maintenance. Use -i option as shown below to display inode number. Using inode number you can remove files that have special characters in its name. 

ls -i
ls -i

ls -i

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

ls -i /etc/apt

11. Hide Control Characters Using `ls -q` 

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` 

 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` 

 ls -F

 Instead of doing the ‘ls -l’ and then the checking for the first character to determine the type of file. You can use -F which classifies the file with different special characters for different kinds of files.

  • / – directory.
  • nothing – normal file.
  • @ – link file.
  • * – Executable file
 ls -F

 ls -F

14. Visual Classification of Files with Colors Using `ls -F` 

ls --color=auto

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.

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

Conclusion

In this article we have discussed practical implementation of `ls` command in linux, in which we have discussed its basic syntax and most commonly used options in `ls` command. One can easily understand the working of all the options by understanding the practical implementation discussed in this article. Users can have good knowledge in `ls` command. It is also important for the system administrator to know these options for working smoothly in daily work routine.

ls Command in Linux – FAQs

What is the ls command in Linux?

The ls command in Linux is used to list the contents of a directory. It displays the names of files and directories within the directory you specify. By default, it lists the contents of the current directory.

How to list all files, including hidden files, using the ls command?

To list all files, including hidden files (those starting with a dot .), you can use the -a option:

ls -a

This command shows all files, including hidden ones, in the current directory.

How to see detailed information about files and directories using ls?

To display detailed information about files and directories, use the -l option with ls:

ls -l

This command provides a long listing format that includes file type, permissions, number of links, owner, group, size, and modification date and time.

How to list files in a specific directory using ls?

To list the contents of a specific directory, provide the path to the directory as an argument to ls:

ls /path/to/directory

Replace /path/to/directory with the actual path of the directory you want to list.

How to sort files by modification time using the ls command?

To sort files by their modification time, use the -t option:

ls -lt

Combining -l and -t will list files in long format and sort them by modification time, with the most recently modified files appearing first.



Previous Article
Next Article

Similar Reads

Ccat – Colorize Cat Command Output command in Linux with Examples
ccat is a command-line tool for Linux and OSX, which is similar to the cat command in Linux. But the difference between cat and ccat is that the ccat shows the content of the file with the syntax highlighted. Currently, ccat supports the following programming languages. JavaScriptJavaRubyPythonGoCJSONInstallation of Ccat First, we are going to see
2 min read
How to Display Command History in Linux | history Command
The command-line interface in Linux provides powerful tools for users, and mastering command history is essential for efficient navigation and retrieval of previously executed commands. The history command is a valuable utility that allows users to view and search through their command history. In this comprehensive guide, we will explore the vario
4 min read
select command in Linux with examples
select command in Linux is used to create a numbered menu from which a user can select an option. If the user enters a valid option then it executes the set of command written in select block and then ask again to enter a number, if a wrong option is entered it does nothing. If user enters nothing and simply press 'enter' the option menu gets print
1 min read
atrm command in Linux with examples
atrm command is used to remove the specified jobs. To remove a job, its job number is passed in the command. A user can only delete jobs that belong to him. Only superuser can delete any job even if that belongs to another user. Syntax: atrm [-V] job [job...] Options: -V : Used to print the version number atrm -V job : Job number of the job which i
1 min read
expand Command in LINUX with examples
Whenever you work with files in LINUX there can be a situation when you are stuck with a file containing many tabs and whatever you need to do with a file requires that file with no tabs but with spaces. In this situation, the task looks quite simple if you are dealing with a small file but what if the file you are dealing with is very big or you n
3 min read
rcp Command in Linux with examples
There comes a time while using LINUX when there is a need to copy some information stored in a file to another computer. This can be done simply using rcp command line utility . Obviously there exists some other methods to complete the above mentioned task which are more secure (like scp or rsync) but this command lets you do this in the simple way
4 min read
du Command in LINUX
While working on LINUX, there might come a situation when you want to transfer a set of files or the entire directory. In such a case, you might wanna know the disk space consumed by that particular directory or set of files. As you are dealing with LINUX, there exists a command line utility for this also which is du command that estimates and disp
9 min read
dmesg command in Linux for driver messages
dmesg command also called as "driver message" or "display message" is used to examine the kernel ring buffer and print the message buffer of kernel. The output of this command contains the messages produced by the device drivers. Usage of dmesg : When the computer boots up, there are lot of messages(log) generated during the system start-up. So you
5 min read
fmt command in Linux with examples
fmt command in LINUX actually works as a formatter for simplifying and optimizing text files. Formatting of text files can also be done manually, but it can be really time-consuming when it comes to large text files, this is where fmt comes to rescue. fmt re-formats each paragraph in the file specified, writing to standard output. Here's the syntax
4 min read
unexpand command in Linux with Examples
To convert the leading spaces and tabs into tabs, there exists a command line utility called unexpand command. The unexpand command by default convert each spaces into tabs writing the produced output to the standard output. Here's the syntax of unexpand command : Syntax : $unexpand [OPTION]... [FILE]... where, OPTION refers to the options compatib
2 min read
uniq command in Linux with examples
The linux uniq command is basically used to remove all repeated lines in a file. This command is used when a line is repeated multiple times and replace these multiple lines with one line. This command is designed to work on sorted files. Purpose : Remove repetitious lines from the sorted 'input-file' and send unique lines to the 'output-file'. If
2 min read
anacron command in linux with examples
anacron command is used to execute commands periodically with a frequency specified in days. Its main advantage over cron is that it can be used on a machine which is not running continuously. In cron if a machine is not running on time of a scheduled job then it will skip it, but anacron is a bit different as it first checks for timestamp of the j
3 min read
SED command in Linux | Set 2
We have discussed some of the SED command options in Sed Command in Linux/Unix with examples SED is used for finding, filtering, text substitution, replacement and text manipulations like insertion, deletion search, etc. It's a one of the powerful utilities offered by Linux/Unix systems. We can use sed with regular expressions. I hope atleast you h
8 min read
enable and disable command in Linux
Enables and disables are the built-in shell commands. enable command is used to start the printers or classes whereas the disable command is used to stop the printers or classes. Syntax For enable Command: enable [-a] [-dnps] [-f filename][name ...] Syntax For disable Command: disable [-c] [-W] [ -r [ reason ] ] printer Options: -a : List all built
1 min read
for command in Linux with Examples
for command in Linux is used to repeatedly execute a set of command for every element present in the list. Syntax: for NAME [in WORDS ... ] ; do COMMANDS; done Example: Options: help for : It displays help information.
1 min read
autoheader command in Linux with Examples
autoheader command in Linux is used to create a template file of C "#define" or any other template header for configure to use. If the user will give autoheader an argument, it reads the standard input instead of reading configure.ac and also writes the header file to the standard output. This command scans the configure.ac file and figures out whi
1 min read
expect command in Linux with Examples
expect command or scripting language works with scripts that expect user inputs. It automates the task by providing inputs. // We can install expect command using following if not installed // On Ubuntu $sudo apt install expect // On Redhat based systems $ yum install expect First we write a script that will be needing inputs from users and then we
2 min read
function command in Linux with examples
The function is a command in Linux that is used to create functions or methods. It is used to perform a specific task or a set of instructions. It allows users to create shortcuts for lengthy tasks making the command-line experience more efficient and convenient. The function can be created in the user's current shell session or saved in the shell
2 min read
fg command in Linux with examples
fg command in linux used to put a background job in foreground. Syntax: fg [job_spec] job_spec may be: %n : Refer to job number n. %str : Refer to a job which was started by a command beginning with str. %?str : Refer to a job which was started by a command containing str. %% or %+ : Refer to the current job. fg and bg will operate on this job if n
1 min read
bg command in Linux with Examples
bg command in linux is used to place foreground jobs in background. Syntax: bg [job_spec ...] job_spec may be: %n : Refer to job number n. %str : Refer to a job which was started by a command beginning with str. %?str : Refer to a job which was started by a command containing str. %% or %+ : Refer to the current job. fg and bg will operate on this
1 min read
Clear Command in Linux with Examples
clear is a standard Unix computer operating system command that is used to clear the terminal screen. This command first looks for a terminal type in the environment and after that, it figures out the terminfo database for how to clear the screen. And this command will ignore any command-line parameters that may be present. Also, the clear command
2 min read
Continue Command in Linux with examples
continue is a command which is used to skip the current iteration in for, while, and until loop. It is used in scripting languages and shell scripts to control the flow of executions. It takes one more parameter [N], if N is mentioned then it continues from the nth enclosing loop. The syntax for the `continue` command in Linuxcontinue or continue [
2 min read
hostid command in Linux with examples
hostid is a command in Linux that is used to display the Host's ID in hexadecimal format. It provides a quick and straightforward way to retrieve the host ID, allowing administrators to associate it with software licenses or perform system-specific operations. Syntax of `hostid` command in Linuxhostid [OPTION] Options provided by hostedhostid --hel
1 min read
Dirname Command in Linux with Examples
dirname is a command in Linux that is used to remove the trailing forward slashes "/" from the NAME and prints the remaining portion. If the argument NAME does not contain the forward slash "/" then it simply prints dot ".". In other words, we can say that the `dirname` command is a useful tool for extracting the directory portion from a given path
2 min read
sdiff command in Linux with Examples
sdiff command in linux is used to compare two files and then writes the results to standard output in a side-by-side format. It displays each line of the two files with a series of spaces between them if the lines are identical. It displays greater than sign if the line only exists in the file specified by the File2 parameter, and a | (vertical bar
2 min read
factor command in Linux with examples
The factor command in Linux is used to print the prime factors of the given numbers, either given from command line or read from standard input. The numbers given through standard input may be delimited by tabs, spaces or newlines. Syntax: factor [NUMBER] Factor 100 (non-prime number): We get the prime factors 2 and 5 that make up 100. Factor 13 (p
1 min read
chpasswd command in Linux with examples
chpasswd command is used to change password although passwd command can also do same. But it changes the password of one user at a time so for multiple users chpasswd is used. Below figure shows the use of passwd command. Using passwd we are changing the password of the guest user. Here first you have to enter the password of the currently signed u
2 min read
biff command in Linux
biff is a mail notification system for unix that notifies the user at the command line when new mail arrives and tells from whom it is. Syntax: biff [ y | n ] Options: Y : This option is to enable the mail notification. N : This option is to disable the mail notification. Note: When mail notification is enabled, the header and first few lines of th
1 min read
dir command in Linux with examples
dir command in Linux is used to list the contents of a directory. How is dir command different from ls? dir command differs from ls command in the format of listing contents that is in default listing options. By default, dir command lists the files and folders in columns, sorted vertically and special characters are represented by backslash escape
3 min read
eval command in Linux with Examples
eval is a built-in Linux command which is used to execute arguments as a shell command. It combines arguments into a single string and uses it as an input to the shell and execute the commands. Syntax eval [arg ...] Example: In the below figure, you can see that cd Desktop command is stored in a variable "CD" as a shell command. Now you can use thi
1 min read