In Linux, to find the total size of files in a directory that has similar file names, we can use “Grep” Command with the combination of two other commands ( ls & awk). Lets see how to do it.

The whole command looks like this:
 
ls -l | grep 'lb*' | awk '{ sum+=5} END {print sum}' 

Lets split it up,

1) List the files in current directory 
 
ls -l                                                                                

This command lists the files in the current directory in order.

Output:
-rwxrwxrwx 1 cg cg       16384 Mar 11 16:49 aria_log.00000001                                
-rwxrwxrwx 1 cg cg             52 Mar 11 16:49 aria_log_control                                  
-rwxrwxrwx 1 cg cg  50331648 Mar 11 16:49 ib_logfile0                                            
-rwxrwxrwx 1 cg cg  50331648 Mar 11 16:47 ib_logfile1                                            

2) Grep and list only files having similar name
 
ls -l | grep 'lb*' 

This command filters output of "ls -l ", which lists only files which has "ib" in the file names.

Output:
-rwxrwxrwx 1 cg cg  50331648 Mar 11 16:49 ib_logfile0                                             
-rwxrwxrwx 1 cg cg  50331648 Mar 11 16:47 ib_logfile1 

3) Find the size of selected files 
 
ls -l | grep 'lb*' | awk '{ sum+=5} END {print sum}' 

This "awk" command  will sum the size listed in the 5th column (see the above output) and prints the total size of files.
 


Comments (0)
Leave a Comment

loader Posting your comment...