28 April 2013

Some LS Command Practical Examples

3 comments


ls command is one of the most frequently used command during a Linux Session. Here are some ls command practical examples to make your Linux Command line experience more enjoyable and faster. Iam assuming that you know basic ls usage like ls -l, ls -a, ls -R etc.


Trick 1:

List Only directories in a Current Directory using ls -d */

$ ls -d */
bootstrap/        Documents/     Music/      sandeep/      VirtualBox VMs/

Trick 2:

List Only hidden directories in a Current Directory using ls -d .*/

$ ls -d .*/
./             .gimp-2.8/          .mplayer/          .ssh/ 
../            .gnome2/            .mysql/            .subversion/ 
.adobe/        .gnome2_private/    .nbi/              .swt/ 

Trick 3:

Display file size in human readable format using -h switch.

$ ls -lh
total 264K
drwxr-xr-x  5 rajkumar rajkumar 4.0K Jan 20 14:54 bootstrap
drwxrwxr-x  7 rajkumar rajkumar 4.0K Mar 29 13:15 Calibre Library
-rw-rw-r--  1 rajkumar rajkumar 4.8K Apr 20 17:36 C:\nppdf32Log\debuglog.txt
drwxrwxr-x  4 rajkumar rajkumar 4.0K Mar 30 22:08 css3
drwxr-xr-x  5 rajkumar rajkumar 4.0K Apr 28 19:12 Desktop

Trick 4:

Sort files according to the modification time using -t switch.

$ vim four.sh
$ ls -lt
total 264
-rw-rw-r--  1 rajkumar rajkumar     0 Apr 28 19:51 four.sh
drwxr-xr-x  5 rajkumar rajkumar  4096 Apr 28 19:12 Desktop
drwxrwxrwx 37 rajkumar rajkumar 20480 Apr 24 20:07 Downloads

Note: In above example we have just modified four.sh, so it was displayed on the top of the list.

Note: To open last edited file with vim, type following command in terminal

$ vim `ls -t | head -1` 

Trick 5:

List Files according to their size in descending using -S switch.

$ ls -lSh
total 264K
-rw-rw-r--  1 rajkumar rajkumar  64K Apr  8 14:11 hs_err_pid3385.log
-rw-rw-r--  1 rajkumar rajkumar  62K Apr 11 21:51 hs_err_pid4197.log
-rw-rw-r--  1 rajkumar rajkumar  22K Oct 13  2012 fbcmd_update.php
drwxrwxrwx 37 rajkumar rajkumar  20K Apr 24 20:07 Downloads

Trick 6:

List Files according to their size in ascending order using -rS switch.

$ ls -lShr
total 264K
-rw-rw-r--  1 rajkumar rajkumar    0 Apr 28 19:58 four.sh
-rw-rw-r--  1 rajkumar rajkumar   25 Apr 18 12:12 spe.sh
-rwxrwxrwx  1 rajkumar rajkumar   97 Apr 18 12:08 spe1.sh
-rw-r--r--  1 rajkumar rajkumar  179 Jun 17  2012 examples.desktop
-rwxrwxrwx  1 rajkumar rajkumar  228 Mar  3 08:56 if.py

Trick 7:

Consider a case in which you have to display time in a specified format like(YYYY-MM-DD), how to do it?. You can do it by using --time-style switch. Lets see few example to understand it.

$ ls -lh --time-style=+"%Y-%B-%d"
total 264K
drwxr-xr-x  5 rajkumar rajkumar 4.0K 2013-January-20 bootstrap
drwxrwxr-x  7 rajkumar rajkumar 4.0K 2013-March-29 Calibre Library
-rw-rw-r--  1 rajkumar rajkumar 4.8K 2013-April-20 C:\nppdf32Log\debuglog.txt
drwxrwxr-x  4 rajkumar rajkumar 4.0K 2013-March-30 css3
drwxr-xr-x  5 rajkumar rajkumar 4.0K 2013-April-28 Desktop

Note: You can use any time format after + sign. Lets see another example

$ ls -lh --time-style=+"%Y-%B-%d %H:%M:%S"
total 264K
drwxr-xr-x  5 rajkumar rajkumar 4.0K 2013-January-20 14:54:29 bootstrap
drwxrwxr-x  7 rajkumar rajkumar 4.0K 2013-March-29 13:15:12 Calibre Library
-rw-rw-r--  1 rajkumar rajkumar 4.8K 2013-April-20 17:36:00 C:\nppdf32Log\debuglog.txt
drwxrwxr-x  4 rajkumar rajkumar 4.0K 2013-March-30 22:08:08 css3

Note: You can use some default option like long-iso, iso, locale etc.

$ ls -lh --time-style=long-iso
total 264K
drwxr-xr-x  5 rajkumar rajkumar 4.0K 2013-01-20 14:54 bootstrap
drwxrwxr-x  7 rajkumar rajkumar 4.0K 2013-03-29 13:15 Calibre Library
-rw-rw-r--  1 rajkumar rajkumar 4.8K 2013-04-20 17:36 C:\nppdf32Log\debuglog.txt
drwxrwxr-x  4 rajkumar rajkumar 4.0K 2013-03-30 22:08 css3
drwxr-xr-x  5 rajkumar rajkumar 4.0K 2013-04-28 19:12 Desktop

Trick 8:

Display one file per line using -1 switch.

$ ls -1
0071765654_preassess.pdf
027__06__MAY12.pdf
bash_cookbook(Oreilly).pdf
bootstrap
eclipse-php-3.0.2.v20120611144-x86.tar.gz
iiitdpdf
innovate.svg
IPV6
IPv6 and the Future of the Internet.pdf

Trick 9:

Remember THOR film? Now you are thinking that why Iam talking about film here?. If you see it carefully than you will note that it is a nice trick to remember some of the options used with ls command. Another use of above trick is that you can use it to display files in reverse order of Modification time.

$ ls -thor
total 264K
-rw-r--r--  1 rajkumar  179 Jun 17  2012 examples.desktop
drwxr-xr-x  2 rajkumar 4.0K Jun 17  2012 Music
drwxrwxr-x  2 rajkumar 4.0K Aug 25  2012 wordpress theme
drwxrwxr-x  2 rajkumar 4.0K Sep  6  2012 lectureplan
-rw-rw-r--  1 rajkumar  22K Oct 13  2012 fbcmd_update.php
drwxr-xr-x  3 rajkumar 4.0K Dec 27 18:59 Videos
drwxr-xr-x  2 rajkumar 4.0K Jan 12 23:59 Templates
drwxr-xr-x  2 rajkumar 4.0K Jan 12 23:59 Public

Note: -r - Reverse order.
-t - Modification time.
-o - Display only Owner name.
-h - Human Readable format.

That's it.
Hope you Enjoyed the post.


If You Liked This Post Please Take a Time To Share This Post

You May Also Like...

3 comments: