Tuesday 16 October 2012

Practical ls Command Examples For Fun & Profit

The power of linux lies in the shell through which we can perform complex job in no time. While the directory listing command 'ls' seems to be very simple command, the linux shell provides the power to use switches and pipes to do anything from terminal. Check out this list with practically useful examples using ls.

Display all files including hidden files/folders

ls -a


Display one file/folder per line

ls -1


Count number of files & folders

ls -1 | wc -l


Human readable file sizes (eg. Mb or Gb)

ls -lh


Alphabetically sort the listing

ls -X


Only list the folders in current directory

ls -d */
ls -p | grep /




Display folders in current directory consisting certain patterns

ls -l D* | grep :$
ls -l *a* | grep :$


List files by descending order of modification time

ls -lt
ls -l --sort=time #alternative long version


List files by descending order of creation time

ls -lct


List files in reverse order

ls -ltr
ls -l --sort=time --reverse #alternative long version


List files in descending order of file size

ls -lSh
ls -lh --sort=size
ls -lSh1 *.avi #find largest AVI file
rm `ls -S1 | head -1` #delete largest file in current folder


List files in ascending order of file size

ls -lShr
ls -lh --sort=size --reverse #alternative long version


Display directories in recursive manner

ls -R


Display the files/folders created today

ls -l --time-style=+%F | grep `date +%F`


Display the files/folders created this year

ls -l --time-style=+%y | grep `date +%y`


Any more example that fires up in your mind? Feel free to share over here ;)