ls is a Linux shell command that lists directory contents of files and directories.Some practical examples of ls command are shown below.
1. Open Last Edited File Using ls -t
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.
[Note: This will open the last file you edited (i.e second.txt)]
2. Display One File Per Line Using ls -1
3. Display All Information About Files/Directories Using ls -l
$ ls -l : To show long listing information about the file/directory.
-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.
- – normal file
- d : directory
- s : socket file
- l : link file
- Field 1 – File Permissions: Next 9 character specifies the files permission. The every 3 characters specifies read, write, execute permissions for user(root), group and others respectively in order. Taking 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 (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.
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 directory then you can use -d option as., For example, if you use ls -l /etc will display all the files under etc directory. But, if you want to display the information about the /etc/ directory, use -ld option as shown below.
$ ls -l /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 be finding 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 be showing 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
$ 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 : To show the hidden files, but not the ‘.’ (current directory) and ‘..’ (parent directory).
[Note: . and .. are not displayed here]
9. Display Files Recursively Using ls -R
$ 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
Sometimes you may want to know the inone number of a file for internal maintenance. Use -i option as shown below to display inone number. Using inode number you can remove files that has special characters in it’s name.
$ ls -i
$ ls -i /etc/apt
11. Hide Control Characters Using ls -q
ls -q : To print question mark instead of the non graphics control characters.
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.
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 character for different kind of files.
- / – directory.
- nothing – normal file.
- @ – link file.
- * – Executable file
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 an another kind in classification of file. In the below output directories get displayed in blue, soft links get displayed in green, and ordinary files gets displayed in default color.
Linux manual page for ls command
This article is contributed by Kishlay Verma. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- 'dd' command in Linux
- SED command in Linux | Set 2
- who command in Linux
- od command in Linux with example
- while command in Linux with example
- du Command in LINUX
- less command in Linux with Examples
- fmt command in Linux with examples
- help Command in Linux with examples
- fc Command in Linux with Examples
- rm command in Linux with examples
- rcp Command in Linux with examples
- arp command in Linux with examples
- ZIP command in Linux with examples
- tar command in Linux with examples
Improved By : Swetha Tanamala 1