Examples on ls command

1 . To know the block size in a file system (ext2 or ext3) in linux

tune2fs -l /dev/sda1 | grep -i ‘block size’

Examples using the ls :

ls command mainly used to list the files in a directory.

Basic :

1 . To list all the files (including the hidden files)

ls -A (print all files hidden . , .. files)

2 . To print sizes of files in terms of blocks

ls –block-size=4K — files sizes will be listed multiples of 4K blocks

3 . To print file sizes in human redable format
(KB,MB,GB)

ls -lh

4 . To print the group id and user id of file who created it

ls -n backup_label.old

-rw——- 1 1005 100   172 2012-09-18 13:44 backup_label.old

5 . To print the file name surronded by double quotes

ls -Q

Example :

ls -Q

“backup_label.old”  “pg_clog”        “pg_log”        “pg_serial”    “pg_tblspc”    “pg_xlog”          “postmaster.pid”
“base”              “pg_hba.conf”    “pg_multixact”  “pg_stat_tmp”  “pg_twophase”  “postgresql.conf”
“global”            “pg_ident.conf”  “pg_notify”     “pg_subtrans”  “PG_VERSION”   “postmaster.opts”

Advanced :

1 . To print the large file in a directory

ls -1Sh –size | awk ‘NR==2{print}’

2 . To print the first large five files in a directories

ls -1S –size | sed -n ‘2,7p’

3 . To print the first small five files in a directory

ls -Slh |  tail -5

4 . To print only files not direcories using the ls

ls -l | grep -v ^d
 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s