ls Command in Linux - GeeksforGeeks (2024)

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

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.

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 Command in Linux - GeeksforGeeks (1)

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 Command in Linux - GeeksforGeeks (2)

ls -1

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

ls -l 

To show long listing information about the file/directory.

ls Command in Linux - GeeksforGeeks (3)

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 Command in Linux - GeeksforGeeks (4)

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 Command in Linux - GeeksforGeeks (5)

ls -l /etc

ls -ld /etc

ls Command in Linux - GeeksforGeeks (6)

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.

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 Command in Linux - GeeksforGeeks (8)

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 Command in Linux - GeeksforGeeks (9)

ls -a

ls -A 

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

ls Command in Linux - GeeksforGeeks (10)

ls -A

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

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

ls /etc/apt

ls Command in Linux - GeeksforGeeks (11)

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 Command in Linux - GeeksforGeeks (12)

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 Command in Linux - GeeksforGeeks (13)

ls -i

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 Command in Linux - GeeksforGeeks (15)

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 Command in Linux - GeeksforGeeks (16)

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 Command in Linux - GeeksforGeeks (17)

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 Command in Linux - GeeksforGeeks (18)

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

ls Command in Linux - GeeksforGeeks (19)

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.



ls Command in Linux - GeeksforGeeks (20)

GeeksforGeeks

ls Command in Linux - GeeksforGeeks (21)

Improve

Previous Article

Linux Commands

Next Article

How to Change the Directory in Linux | cd Command

Please Login to comment...

ls Command in Linux - GeeksforGeeks (2024)
Top Articles
Is It Better To Block Or Ignore An Ex? Answered With Stats
11 Tips On How To Move Cross Country Without Going Broke!
Splunk Stats Count By Hour
Sound Of Freedom Showtimes Near Governor's Crossing Stadium 14
Www.craigslist Virginia
Breaded Mushrooms
Celebrity Extra
Ross Dress For Less Hiring Near Me
The Idol - watch tv show streaming online
Chase Claypool Pfr
Strange World Showtimes Near Cmx Downtown At The Gardens 16
Ap Chem Unit 8 Progress Check Mcq
Hmr Properties
This Modern World Daily Kos
Dc Gas Login
Kvta Ventura News
Obsidian Guard's Cutlass
Inter-Tech IM-2 Expander/SAMA IM01 Pro
Craigslist West Valley
Espn Horse Racing Results
Home
Munis Self Service Brockton
Living Shard Calamity
Mals Crazy Crab
Roanoke Skipthegames Com
Water Temperature Robert Moses
Meta Carevr
Bidrl.com Visalia
What we lost when Craigslist shut down its personals section
Stephanie Bowe Downey Ca
Bj's Tires Near Me
Current Time In Maryland
Joplin Pets Craigslist
The Ride | Rotten Tomatoes
Steven Batash Md Pc Photos
Mississippi State baseball vs Virginia score, highlights: Bulldogs crumble in the ninth, season ends in NCAA regional
John F Slater Funeral Home Brentwood
CVS Near Me | Somersworth, NH
How are you feeling? Vocabulary & expressions to answer this common question!
Bella Thorne Bikini Uncensored
Felix Mallard Lpsg
968 woorden beginnen met kruis
Shane Gillis’s Fall and Rise
5A Division 1 Playoff Bracket
Smite Builds Season 9
Garland County Mugshots Today
Portal Pacjenta LUX MED
Unblocked Games - Gun Mayhem
Kidcheck Login
Philasd Zimbra
Escape From Tarkov Supply Plans Therapist Quest Guide
Primary Care in Nashville & Southern KY | Tristar Medical Group
Latest Posts
Article information

Author: Clemencia Bogisich Ret

Last Updated:

Views: 6184

Rating: 5 / 5 (60 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Clemencia Bogisich Ret

Birthday: 2001-07-17

Address: Suite 794 53887 Geri Spring, West Cristentown, KY 54855

Phone: +5934435460663

Job: Central Hospitality Director

Hobby: Yoga, Electronics, Rafting, Lockpicking, Inline skating, Puzzles, scrapbook

Introduction: My name is Clemencia Bogisich Ret, I am a super, outstanding, graceful, friendly, vast, comfortable, agreeable person who loves writing and wants to share my knowledge and understanding with you.