Tuesday 4 September 2018

14 useful ‘ls’ command examples for linux beginners

ls Command, LPI Study Materials, LPI Guides, LPI Tutorial and Materials

The ls (list) command displays a list of files and directories. It supports several options some of them are listed in the below table along with a short description of each. In this post we will discuss 14 different examples of ls command which can be useful for beginners.

Syntax of ls command :

# ls [options] [file | directory]

Option Description 
-a Lists hidden files also. If a file or directory name starts with a dot, it is referred to as hidden. 
-F  Displays file types. Shows / for directories, * for executable files, @ for symbolic links and nothing for text files. 
-lh   Displays long listing with file sizes in human readable format. 
-l  Displays long listing with detailed file information including file type, permissions, link count, owner, group, size, date and time 
of  last modification, and name of the file.-ld Displays long listing of the specified directory, but hides its contents. 
-R  Lists contents of the specified directory and all its sub-directories (recursive listing). 
-lt  Lists all files sorted by date and time with the newest file first. 
-ltr  Lists all files sorted by date and time with the oldest file first. 

Note : For more detailed options of ls command please refer ls man page.

Example:1 list the files & directory of your current working directory


[root@localhost /]# ls
bin boot cgroup dev etc home lib lib64 lost+found media mnt opt proc root sbin selinux srv sys tmp usr var
[root@localhost /]#

Example:2 To list files in the current directory with detailed information


[root@localhost /]# ls -l
total 94
dr-xr-xr-x.  2 root root  4096 Nov 23 03:13 bin
dr-xr-xr-x.  5 root root  1024 Nov 23 05:29 boot
drwxr-xr-x  10 root root  4096 Nov 23 03:42 cgroup
drwxr-xr-x  18 root root  3680 Dec 12 21:32 dev
drwxr-xr-x. 64 root root  4096 Dec 12 21:32 etc
drwxr-xr-x   8 root root  4096 Jul 19 02:43 home
dr-xr-xr-x.  8 root root  4096 May  3  2014 lib
dr-xr-xr-x.  8 root root 12288 Nov 23 03:13 lib64
-rw-r--r--   1 root root    72 Dec 12 21:40 linux-stuff
drwx------.  2 root root 16384 May  3  2014 lost+found
drwxr-xr-x.  2 root root  4096 Sep 23  2011 media
drwxr-xr-x.  3 root root  4096 Jun 22 01:14 mnt
..................................................

Example:3 List the contents of a specific directory


[root@localhost /]# ls -l /tmp/
total 8
-rw-r--r-- 1 root root 0 Aug 3 14:31 bad-blocks.txt
drwx------. 2 root root 4096 May 3 2014 gpg-auCCFn
-rw-------. 1 root root 0 May 3 2014 yum.log
-rw------- 1 root root 1428 Nov 23 03:58 yum_save_tx-2014-11-23-03-58BHO8Jg.yumtx

To list only directory entries instead of contents, use ‘-ld‘ option. Example is shown below

[root@localhost /]# ls -ld /tmp/
drwxrwxrwt. 4 root root 4096 Dec 12 21:34 /tmp/

Example:4 To display all files in the current directory with their file types


[root@localhost /]# ls -F
bin/ cgroup/ etc/ lib/ linux-stuff media/ opt/ root/ selinux/ sys/ usr/
boot/ dev/ home/ lib64/ lost+found/ mnt/ proc/ sbin/ srv/ tmp/ var/
[root@localhost /]#

When we use ‘-F‘ option with ls command , then in the output ‘/’ will appended at every directory and for normal files nothing is appended.

Example:5 To list all files in the current directory with detailed information and sorted by date & time with the newest file first.


[root@localhost /]# ls -lt
total 94
-rw-r--r--   1 root root    72 Dec 12 21:40 linux-stuff
dr-xr-xr-x  13 root root     0 Dec 12 21:36 sys
drwxrwxrwt.  4 root root  4096 Dec 12 21:34 tmp
drwxr-xr-x  18 root root  3680 Dec 12 21:32 dev
drwxr-xr-x. 64 root root  4096 Dec 12 21:32 etc
dr-xr-xr-x  95 root root     0 Dec 12 21:31 proc
dr-xr-xr-x.  5 root root  1024 Nov 23 05:29 boot
drwxr-xr-x  10 root root  4096 Nov 23 03:42 cgroup
dr-xr-xr-x.  2 root root  4096 Nov 23 03:13 sbin
dr-xr-xr-x.  8 root root 12288 Nov 23 03:13 lib64
dr-xr-xr-x.  2 root root  4096 Nov 23 03:13 bin
dr-xr-x---.  5 root root  4096 Nov 23 02:53 root
drwxr-xr-x. 18 root root  4096 Aug 24 05:14 var
drwxr-xr-x   8 root root  4096 Jul 19 02:43 home
............................

Example:6 To display all files in the current directory with their sizes in human readable format( e.g 1K, 234M, 2G )


[root@localhost /]# ls -lh
total 94K
dr-xr-xr-x. 2 root root 4.0K Nov 23 03:13 bin
dr-xr-xr-x. 5 root root 1.0K Nov 23 05:29 boot
drwxr-xr-x 10 root root 4.0K Nov 23 03:42 cgroup
drwxr-xr-x 18 root root 3.6K Dec 12 21:32 dev
drwxr-xr-x. 64 root root 4.0K Dec 12 21:32 etc
drwxr-xr-x 8 root root 4.0K Jul 19 02:43 home
dr-xr-xr-x. 8 root root 4.0K May 3 2014 lib
dr-xr-xr-x. 8 root root 12K Nov 23 03:13 lib64
-rw-r--r-- 1 root root 72 Dec 12 21:40 linux-stuff
drwx------. 2 root root 16K May 3 2014 lost+found
drwxr-xr-x. 2 root root 4.0K Sep 23 2011 media
drwxr-xr-x. 3 root root 4.0K Jun 22 01:14 mnt
drwxr-xr-x. 2 root root 4.0K Jun 22 01:25 opt
......................................

Example:7 To list all files, including the hidden files, in the current directory with detailed information


[root@localhost /]# ls -la
total 102
dr-xr-xr-x. 23 root root 4096 Dec 12 21:49 .
dr-xr-xr-x. 23 root root 4096 Dec 12 21:49 ..
-rw-r--r-- 1 root root 0 Dec 12 21:32 .autofsck
-rw-r--r-- 1 root root 0 Jul 19 02:17 .autorelabel
dr-xr-xr-x. 2 root root 4096 Nov 23 03:13 bin
dr-xr-xr-x. 5 root root 1024 Nov 23 05:29 boot
drwxr-xr-x 10 root root 4096 Nov 23 03:42 cgroup
drwxr-xr-x 18 root root 3680 Dec 12 21:32 dev
drwxr-xr-x. 64 root root 4096 Dec 12 21:32 etc
drwxr-xr-x 8 root root 4096 Jul 19 02:43 home
dr-xr-xr-x. 8 root root 4096 May 3 2014 lib
dr-xr-xr-x. 8 root root 12288 Nov 23 03:13 lib64
-rw-r--r-- 1 root root 0 Dec 12 21:48 .linux-rocks
-rw-r--r-- 1 root root 72 Dec 12 21:40 linux-stuff
-rw-r--r-- 1 root root 0 Dec 12 21:49 .linux-tips
drwx------. 2 root root 16384 May 3 2014 lost+found
drwxr-xr-x. 2 root root 4096 Sep 23 2011 media
..................................................................

Example:8 To list contents of the /etc directory recursively


[root@localhost /]# ls -R /etc/
/etc/:
adjtime
aliases
aliases.db
alternatives
anacrontab
audisp
audit
bash_completion.d
bashrc
blkid
centos-release
..........................

Example:9 List the files sorted by their size


[root@localhost /]# ls -lhS
total 13M
-rw-r--r-- 1 root root 12M Dec 12 22:05 linux-stuff
drwx------. 2 root root 16K May 3 2014 lost+found
dr-xr-xr-x. 8 root root 12K Nov 23 03:13 lib64
dr-xr-xr-x. 2 root root 4.0K Nov 23 03:13 bin
drwxr-xr-x 10 root root 4.0K Nov 23 03:42 cgroup
drwxr-xr-x. 64 root root 4.0K Dec 12 21:32 etc
drwxr-xr-x 8 root root 4.0K Jul 19 02:43 home
dr-xr-xr-x. 8 root root 4.0K May 3 2014 lib
drwxr-xr-x. 2 root root 4.0K Sep 23 2011 media
drwxr-xr-x. 3 root root 4.0K Jun 22 01:14 mnt
..............................................
Above command will display those files and directory first which are bigger in size.

Example:10 List the inode numbers of files & directories using ‘-i’ option


[root@localhost /]# ls -li 
total 12378
 786436 dr-xr-xr-x. 2 root root 4096 Nov 23 03:13 bin
 2 dr-xr-xr-x. 5 root root 1024 Nov 23 05:29 boot
 655361 drwxr-xr-x 10 root root 4096 Nov 23 03:42 cgroup
 3 drwxr-xr-x 18 root root 3680 Dec 12 21:32 dev
 393217 drwxr-xr-x. 64 root root 4096 Dec 12 21:32 etc
 2 drwxr-xr-x 8 root root 4096 Jul 19 02:43 home
 917506 dr-xr-xr-x. 8 root root 4096 May 3 2014 lib
 262146 dr-xr-xr-x. 8 root root 12288 Nov 23 03:13 lib64
 2668 -rw-r--r-- 1 root root 12582912 Dec 12 22:05 linux-stuff
...................................................................

Example:11 Display the current version of ls command using ‘–version’


[root@localhost /]# ls --version 
ls (GNU coreutils) 8.4
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Example:12 List uid & gid of files or directories using ‘-n’ option


[root@localhost /]# ls -n 
total 14476
-rw-rw-r-- 1 1000 1000 193 Aug 16 16:10 1
-rw-rw-r-- 1 1000 1000 262 Nov 28 12:35 Anjali and Aakash help Yohan-B3MzR5cTpd2mLBSZfkPKppip8vCTKUdd.mp4.part
-rw-rw-r-- 1 1000 1000 1849048 Jun 7 2014 BINGO from Super Simple Songs-9mmF8zOlh_g.mp4.part
drwxr-xr-x 18 1000 1000 4096 Dec 13 08:59 Desktop
drwxr-xr-x 2 1000 1000 4096 Sep 27 13:09 Documents
drwxr-xr-x 2 1000 1000 4096 Dec 2 10:10 Downloads
drwx------ 3 1000 1000 4096 Nov 30 15:36 Dropbox
-rw-rw-r-- 1 1000 1000 94296 Nov 20 02:00 dropbox_2.10.0_amd64.deb
-rw-r--r-- 1 1000 1000 8980 Apr 20 2014 examples.desktop
-rw-rw-r-- 1 1000 1000 203 Oct 26 16:02 fs.sh
drwxrwxr-x 2 1000 1000 4096 Sep 1 14:31 Google Drive
................................................................

Example:13 Default Aliases of ls command


Type the alias command on the terminal to display the default aliases set for ls command.

[root@localhost /]# alias 
alias cp='cp -i'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

Example:14 Enable timestamps in the output of ls command using “–time-style” option


[root@localhost /]# ls -l --time-style="+%Y-%m-%d $newline%m-%d %H:%M"
total 12378
dr-xr-xr-x. 2 root root 4096 2014-11-23 11-23 03:13 bin
dr-xr-xr-x. 5 root root 1024 2014-11-23 11-23 05:29 boot
drwxr-xr-x 10 root root 4096 2014-11-23 11-23 03:42 cgroup
drwxr-xr-x 18 root root 3680 2014-12-12 12-12 21:32 dev
drwxr-xr-x. 64 root root 4096 2014-12-12 12-12 21:32 etc
drwxr-xr-x 8 root root 4096 2014-07-19 07-19 02:43 home
dr-xr-xr-x. 8 root root 4096 2014-05-03 05-03 05:24 lib
dr-xr-xr-x. 8 root root 12288 2014-11-23 11-23 03:13 lib64
-rw-r--r-- 1 root root 12582912 2014-12-12 12-12 22:05 linux-stuff
drwx------. 2 root root 16384 2014-05-03 05-03 05:19 lost+found
drwxr-xr-x. 2 root root 4096 2011-09-23 09-23 07:50 media
......................................................................

Related Posts

0 comments:

Post a Comment