Thursday, March 3, 2016

List the file size in the readable format ( B, MB, KB , GB)


List the Files with size in the readable format


 Command -    ls -h

                       h - option to display the files in the readable format like B, KB,MG, GB etc




How to list the directories alone


(1)  List directories Using ls command


      There is no direct switch / option to list the directories alone in the ls command .

      Command    =>  ls -d */     - This command is little tricky one . Refer the below explanation to understand this command   

             

      Usage          =>  -d option  to list the directory names but -d option need an argument. -d just eliminates displaying content of the directory ( ls * will display file and directories contents )  & display the directory name alone. argument here is */.    "*"  indicates the meta character which display all contents ( file, directory , link etc ) .  Forward slash (/) indicates the end of the directory .  Unix internally represents / is for directory in the end of the directory name. so */ means  any directory.  In Summary, when */ passed as argument to -d option, it picks the directories list and ignore the content of directories and display only the directory names


(2)  List the directories using echo command


 Command :  echo */  




 Usage:     Just echo *  will display all content of a directory .  */ display only directory . * indicates 0 or more characters & forward slash ( / ) indicates directory

(3) List directories using ls and grep commands


Command : ls -l | grep "^d"





Usage:      ls -l lists the directories and files. the permission field's ( 1st filed) 1st char denotes the file (f) / directory (d).  the grep command uses the regular expression carrot ( ^) which read the 1st character and then "d" denote first directory.  so ^d indicates search the first character which has 'd' and get its list.  If you want to display the files alone instead of directory, ls -l | grep "^-"

 (4) List directories using ls -F


Command: ls -F | grep "/$"





Usage:     -F option will differentiate adding forward slash (/) at the end of the directory name. grep command searches last character as "/" and list only those lines which are directories names

Thursday, February 25, 2016

How to sort the files and directories size wise

   1 . Command -  Using ls command

     
     Command              ->   ls -lS     
                                              l  - Long Listing  which lists permission, links, user, group, size ,
                                                  modified/created date, file name             
                                             S  - Sort the files by Size in Descending order by default. 
                                                    Note -S is a capital letter  
     Command Usage   ->  List the files and directories file size wise ( default descending order) 





   2 . Command -  Using ls & sort commands


     Command              ->    ls -l | sort -k 5 -r   

                                             k - indicates the key and 5 indicates 5th key ( size)
                                             r - reverse in descending order size wise
                             
     Command Usage   ->  This command list the file in long format and pass the output into
                                            sort  command. Sort command takes the 5th key ( size ) and order
                                            by size in descending order.  
  
  Note                             ->   pipe symbol (|) is to pass the output of 
                                              command to next command.