Sample Text

Different uses Of 'ls' Command in Unix

1. Open Last Edited File Using ls -t
To open the last edited file in the current directory, use the combination of ls, head and vi commands as shown below.
ls -t sorts the file by modification time, showing the last edited file first. head -1 picks up this first file.
$ vi `ls -t | head -1`

2. Display All Information About Files/Directories Using ls -l
To show long listing information about the file/directory.
$ ls -l
-rw-r----- 1 Ritesh team-dev 98975204 Jun 15 18:35 Ritesh.txt
§  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 9 character specifies the files permission. Each 3 characters refers to the read, write, execute permissions for user, group and world in this example, -rw-r—– indicates read-write permission for user, read permission for group, and no permission for others.
§  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 ‘ramesh’.
§  Field 4 – Group: Fourth field specifies the group of the file. In this example, this file belongs to ”team-dev’ group.
§  Field 5 – Size: Fifth field specifies the size of file. In this example, ‘9275204’ indicates the file size.
§  Field 6 – Last modified date & time: Sixth field specifies the date and time of the last modification of the file. In this example, ‘Jun 13 15:27’ 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 mthesaur.txt.gz.

3. Display File Size in Human Readable Format Using ls -lh
Use ls -lh (h stands for human readable form), to display file size in easy to read format. 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.

6. Order Files Based on Last Modified Time Using ls -lt
To sort the file names displayed in the order of last modification time use the -t option.

7. Order Files Based on Last Modified Time (In Reverse Order) Using 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. This is my default ls usage. Anytime I do ls, I always use ls -ltr as I find this very convenient.

8. Display Hidden Files Using ls -a (or) 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). To show the hidden files, but not the ‘.’ (current directory) and ‘..’ (parent directory), use option -A.

9. Display Files Recursively Using ls -R
To show all the files recursively, use -R option. 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

11. Hide Control Characters Using ls -q
To print question mark instead of the non graphics control characters use the -q option.
ls -q
12. Display File UID and GID Using ls -n
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
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.

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

15. Useful ls Command Aliases
You can take some required ls options in the above, and make it as aliases. We suggest the following.


No comments:

Post a Comment

Contact Form

Name

Email *

Message *