ls Command in Linux : in Linux ls we use for listing of the file/directory.
1. ls -t
Open Last edited in the current directory. If you have two file test1 and test2 and you want to display which is the lasted edited file. Then use command:
ls -t | head -1
root@localhost ~> ls -t | head -1
test2
2. ls -1
Display 1 file per line using -1. This will simply display single entry per line
root@localhost ~> ls -1
anaconda-ks.cfg
cd.iso
Desktop
Documents
Downloads
epel-release-6-5.noarch.rpm
root@localhost ~>
3. ls -l
Display all information about a file/directory
ls -l
root@localhost ~> ls -l
-rw-------. 1 root root 439373 0048 Aug 12 21:32 test123.txt
Here :
i. First character of the file specifies the type of the file it is.
in the example above hyphen(-) in the 1st character indicates that this is normal file. Following are the possibilities at the first character.
- Normal file
d directory
s socket file
l link file
ii. Field 1 - File permission. Net 9 character specifies the file permission. They are devided into 3 blocks of consists of 3 character each - read, write and execute permission for user, group and world. Here in this example see that first hyphen indicates the file type(in this case it is normal) and then next three charachers rw- indictates that user has read and write permission and not the execute permssion. Next three are --- indicates that group has no permission and same for the world ---.
iii. Field 2 - Number of links. Second filed specifies number of links for the file. In this example we see that 1, it indicates only one link.
iv. Field 3 - Owner. It indicates who is the owner of that file, in this example we see that root is the owner.
v. Field 4 - Group. It indicates which group it belongs to. Here the group is root again.
vi. Field 5 - Size. Indiates the file size here in this case file size is 439373.
viii. Field 6 - Last modified date and time. It indicates that when the lastly the file was modified, here in this example it is 'Aug 12 21:32' .
viiii. Field 7 - File Name. Last field indicates the file name. Here in this case it is test123.txt.
4. ls -lh
Display the file size in human readable format, here h stands for human readable format.
-rw-------. 1 root root 1.6k 0048 Aug 12 21:32 test123.txt
Here you can see 1.6K, K-KB, M-MB, G-GB.
5. ls -ld
Diplay all the directorys.
See if you use ls -l it will display all the files/directories.
root@localhost ~> ls -l
-rw-r--r--. 1 root root 4393730048 Aug 12 21:32 test123.txt
drwxr-xr-x. 2 root root 4096 Aug 7 21:12 Desktop
Now when you type ls -ld then it will only display all the directories.
root@localhost ~> ls -ld
drwxr-xr-x. 2 root root 4096 Aug 7 21:12 Desktop
Here the first filed d indicates directory.
6. ls -lt
Displays the last modified file first.
root@localhost ~> ls -lt
-rw-r--r--. 1 root root 41 Aug 14 2011 test2
-rw-r--r--. 1 root root 29 Aug 14 2011 test1
-rw-r--r--. 1 qemu qemu 4393730048 Aug 12 21:32 cd.iso
7. ls -ltr
Diplays the last modified files in reverse order. To sort the file names in the last modification time in reverse order.
root@localhost ~> ls -ltr
-rw-r--r--. 1 qemu qemu 4393730048 Aug 12 21:32 cd.iso
-rw-r--r--. 1 root root 29 Aug 14 2011 test1
-rw-r--r--. 1 root root 41 Aug 14 2011 test2
8. ls -a /ls -A
If you want to display hidden files in a directory we use ls -a.
root@localhost ~> ls -a
. Desktop .gstreamer-0.10 Pictures test1
.. Documents .gtk-bookmarks pidgin-2.9.0.tar.bz2 test2
It will show the files including the '.' (Current Directory) and '..' (Parent Directory) to show the hidden files but not '.' (Current Directory) and '..' (Parent Directory) us option -A.
root@localhost ~> ls -A
anaconda-ks.cfg .lesshst
.bash_history .local
.bash_logout .mozilla
.bash_profile Music
9. ls -R
Displays files recursively.
root@localhost ~> ls /etc/sysconfig/networking/
devices profiles
This will display all files in directory /etc/sysconfig/networking/
And to display recursively is the option -R
root@localhost ~> ls -R /etc/sysconfig/networking/
/etc/sysconfig/networking/:
devices profiles
/etc/sysconfig/networking/devices:
/etc/sysconfig/networking/profiles:
default
/etc/sysconfig/networking/profiles/default:
10. ls -i
Displays the inode number of a file.
root@localhost ~> ls -i /etc/sysconfig/networking/
4457181 devices 4457182 profiles
11. ls -n
Displays the UID and GID of a file.
root@localhost ~> ls -l /etc/sysconfig/networking/
drwxr-xr-x. 2 root root 4096 Jun 22 2010 devices
Here you see the devices file has User and group as root and root respectively.
when we use ls -n instead we see following output.
root@localhost ~> ls -n /etc/sysconfig/networking/
drwxr-xr-x. 2 0 0 4096 Jun 22 2010 devices
Here you see root and root is replaced by a number 0, it is because root has UID and GID both 0(zero).
12. ls -F
Visual Classification of Files With Special Character, if you use ls -l then checking for the first character to determine the type of file. When we use ls -F it classifies the file with different special character for a different kind of files.
root@localhost ~> ls -F
anaconda-ks.cfg Documents/ epel-release-6-5.noarch.rpm.1 Music/ Public/ test1 wget-log
cd.iso Downloads/ hybrid-portsrc_x86_64-v5_100_82_38.tar.gz Pictures/ rpmbuild/ test2 Desktop/ epel-release-6-5.noarch.rpm hybrid-wl/
We can also explore some more options with ls like.
ls -c -- c indicates Character devices
ls -p -- p indicates it is a named pipe (FIFO) as shown below.
ls -d --To get the list of hidden directory.
No comments:
Post a Comment