The Linux LS command: with practical examples

ls is used a lot by the Unix users. The two letter command is very powerfull, when combined with the right optins or wildcards.

In this article I will use practical examples followed by explanations, so that it will be understandable for everybody.

1. Display only one file per line with ls -1

The ls -1 command displays the output vertically.

$ ls -1
anakin
dir1
file1
myfifo
myfunctions

2. Display full information about files and folders: ls -l

Showing ls in the long listing format:

Example 1:

$ ls -llrwxrwxrwx 1 razvan razvan 9 2012-05-22 00:43 anakin -> skywalker
drwxrwxr-x 2 razvan razvan 4096 2012-05-22 00:42 dir1
-rw-rw-r-- 1 razvan razvan 0 2012-05-22 00:39 file1
prw-rw-r-- 1 razvan razvan 0 2012-05-22 00:37 myfifo

The 1st character: File Type – The first character on each raw specifies the file type:

  • l : symbolik link (usually called link)
  • d : directory
  • – : normal file
  • s : socket file
  • p : named pipe
  • b : block device
  • c : character device

Field 1 – File Permissions – The next 9 characters represent the file permisions. Each 3 characters refer to read, write, execute, for user, group and other, in this order. rwxrwxrwx means that the file has read, write and execute permissions for everybody (user, group, others) while rw-rw-r– means that the file has read and write permissions for user and group and only reading permissions for the others (that are not the user and don’t belong to the user’s group).

Field 2 – Hard link count – In “Example 2”, the directory terminfo has 15 links pointing for it.

Example 2:

$ ls -l /lib | grep terminfo
drwxr-xr-x 15 root root    4096 2011-09-16 22:29 terminfo

Field 3 – Owner – In the 3rd field it is displayed the owner of the file or directory. In “Example 2”, the owner is root.

Field 4 – Group – The 4th field shows the name of the group that owns the file or directory. In “Example 2”, the directory is owned by the root group. Also, a file or directory can be owned by a user, but not be owned by that user’s group. In “Example 3”, the owner is razvan (belonging to the group razvan) and the group owning the file is root.

Example 3:

ls -l skywalker
-rw-rw-r-- 1 razvan root 2 2012-05-22 17:46 skywalker

Field 5 – In the 5th field it is displayed the size of the file or directory, in bytes. According to “Example 3”, the file’s size is 2 bytes.

Field 6 – Last modified date and time . The file in “Example 3” was last modified in 22.05.2012 , at 17:46.

Field 7 – The file’s name. The file in “Example 3” is name skywalker.

3. Display folder information with ls -ld.

If you use ls -l on a folder, you will get information about the files (or folders) contained by that folder, not about the folder itself. ls -ld is for displaying info about directories.

$ ls -l /etc
drwxr-xr-x 3 root root 4096 2011-10-12 17:30 acpi
-rw-r--r-- 1 root root 2981 2011-10-12 17:27 adduser.conf

$ ls -ld /etc
drwxr-xr-x 141 root root 12288 2012-05-22 16:57 /etc

4. Display file and folder size in human readable format: ls -lh and ls -ldh.

ls -lh shows a file’s size while ls -ldh shows a folder’s size (h is for human readable, the sizes are displayed in K, M, G.)

$ ls -lh /etc/passwd
-rw-r--r-- 1 root root 1.7K 2012-05-22 16:57 /etc/passwd
$ls -lhd /etc
drwxr-xr-x 141 root root 12K 2012-05-22 16:57 /etc

5. Display files sorted by size:

ls -lSr is to display the files sorted, from the smallest to the biggest

$ ls -lSr /etc | head
total 1136
-rw-r--r-- 1 root root 0 2011-03-15 23:05 odbc.ini
-rw-r--r-- 1 root root 6 2012-05-21 22:49 hostname
-rw-r--r-- 1 root root 7 2012-05-21 22:50 papersize
-rw-r--r-- 1 root root 10 2012-05-21 22:49 adjtime
lrwxrwxrwx 1 root root 13 2012-05-21 22:38 motd -> /var/run/motd
lrwxrwxrwx 1 root root 15 2012-05-21 22:38 blkid.tab -> /dev/.blkid.tab

And ls -lS is for displaying the files sorted, but from the biggest to the smallest

$ ls -lS | head
-rw-r--r-- 1 root root 64904 2012-05-21 22:56 ld.so.cache
-rw-r--r-- 1 root root 58753 2011-10-04 23:53 bash_completion
-rw-r--r-- 1 root root 23958 2010-11-15 10:07 mime.types
-rw-r--r-- 1 root root 19666 2011-05-24 18:26 services
-rw-r--r-- 1 root root 15752 2009-07-25 18:13 ltrace.conf

6. Display files sorted by their modification time

The -t option is for sorting the files from the first file modified to the last one. (so ls -lt , for seeing that the command works)

$ ls -lt /etc | head
total 1136
-rw-r--r-- 1 root root 0 2012-05-22 21:35 A
-rw-r--r-- 1 root root 91 2012-05-22 20:17 resolv.conf
drwxr-xr-x 2 root root 4096 2012-05-22 16:57 ssh
-rw-r----- 1 root shadow 1254 2012-05-22 16:57 shadow
-rw-r--r-- 1 root root 1667 2012-05-22 16:57 passwd

Also, files can be displayed sorted from the last file modified to the first one (in reversed order) by using ls -ltr.

$ ls -ltr /etc | head
total 1136
-rw-r--r-- 1 root root 2064 2006-11-23 21:33 netscsid.conf
-rw-r--r-- 1 root root 1343 2007-01-09 20:39 wodim.conf
-rw-r--r-- 1 root root 112 2007-06-22 18:08 apg.conf
-rw-r--r-- 1 root root 15752 2009-07-25 18:13 ltrace.conf

7. Display hidden files and folders in Linux

In Linux, the hidden files and folders have their names starting with . and are not displayed by ls -l.

ls -a or (ls -A) displays all, the hidden and the non hidden files and folders.

$ ls -A
.bash_history .esd_auth .linuxmint .sudo_as_admin_successful
.bash_logout .gconf .local Templates
.cache .gnome2 .mozilla Videos
.config .gnome2_private Music .viminfo
.dbus .gstreamer-0.10 Pictures work
Desktop .gtk-bookmarks .profile .Xauthority
.dmrc .gvfs Public .xsession-errors
Documents .ICEauthority .pulse
Downloads .lesshst .pulse-cookie

8. Display files recursive by using the -R option.

ls -R displays all the files and folders contained by the listed directory.

$ ls -R starwars
starwars:
anakin darth vader obi one yoda

starwars/anakin:
force light of side the

starwars/darth vader:
dark force of side the

starwars/obi one:
jedi knight

starwars/yoda:
green jedi little master

9. Open the last modified file by using ls -t, combined with vim and head.

$ vim light
[:wq]
$ vim force
[:wq]
$ vim `ls -t | head -1`
[this has opened force, because it is the last file modified]

Explanation:

Vim is a text editor. ls -t displays the last modified files, sorted from the first edited to the last, while head -1 displays only the first result.

The vim `ls -t | head -1` command is simply awesome and should be saved as an alias.

10. Display a file’s/folder’s inode:

$ ls -i
662069 Desktop 662172 Music 662173 Pictures 662174 Videos
662169 Documents 428742 myscripts 662112 Public
662083 Downloads 428113 naboo 662084 Templates

11. Display only a specified type of file by using ls and grep.

grep is a text filter. If I want to list only the links, for example, I will use ls -l /etc | grep ^l

$ ls -l | grep ^l

lrwxrwxrwx 1 root root 15 2012-05-21 22:38 blkid.tab -> /dev/.blkid.tab
lrwxrwxrwx 1 root root 13 2012-05-21 22:38 motd -> /var/run/motd
lrwxrwxrwx 1 root root 28 2012-05-21 22:38 nologin -> /var/lib/initscripts/nologin
lrwxrwxrwx 1 root root 23 2012-05-21 22:38 vtrgb -> /etc/alternatives/vtrgb

Explanation: ls -l displays full detailes about the file and grep filters the text. grep ^l chooses only the lines starting with the l character.

Obviously, ls -l | grep ^d will display only directories, while ls -l | grep ^- will display normal files.

The ls command is even more powerfull combined with wildcards. I wrote an article about wildcards and how to use them here, if you are interested.

Share and Enjoy

  • Twitter
  • Facebook
  • Google Plus
  • Email
  • RSS
Tagged with: , , ,
Posted in The Linux and Unix Articles!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Subscribe

  

Subscribe to get the latest Linux news and how to guides directly on your e-mail!