Trying to get 5th field from ls -l output

Bash Shell/Oracle Linux 6.4

Following is an ls -lh output. Files which are equal to or higher than 1Gigabytes will displayed with G in the 5th column of the output.
I am trying to find all files which are equal to or higher than 1G

File sizes are shown in the 5th column. So, I tried

cut -d' ' -f5

. But it returned blank lines, so I think it may be counting each individual space as a field when there is more than one space together

ls -lrth *.dmp | cut -d' ' -f5

-rwxrwxrwx 1 migration gdma       32K Mar 15 17:38 MIG_DUMPFILE_POL_20140315_173834.dmp
-rw-r----- 1 oracle    asmadmin  1.7G Mar 17 09:27 MIG_DUMPFILE_OM411_20140317_091908.dmp
-rw-r----- 1 oracle    asmadmin   28K Mar 20 15:36 MIG_DUMPFILE_AI317_20140320_153623.dmp
-rw-r----- 1 oracle    asmadmin   74M Mar 20 16:55 MIG_STG_UIM_MSISDN_TGT_NM423_20140320_165437.dmp
-rw-r----- 1 oracle    asmadmin   68M Mar 20 17:02 MIG_STG_UIM_MSISDN_TGT_NM424_20140320_170224.dmp
-rw-r----- 1 oracle    asmadmin   92M Mar 20 17:11 MIG_STG_UIM_MSISDN_TGT_NM425_20140320_171042.dmp
-rw-r----- 1 oracle    asmadmin  231M Mar 21 16:14 MIG_DUMPFILE_STG_LKP_BSS_BA_20140321_161411.dmp
-rw-r----- 1 oracle    asmadmin   93M Mar 21 16:34 MIG_DUMPFILE_OM412_20140321_163354.dmp
-rw-r----- 1 oracle    asmadmin  446M Mar 21 20:00 MIG_DUMPFILE_AM410_20140321_195913.dmp
-rw-r----- 1 oracle    asmadmin  146M Mar 21 20:08 MIG_DUMPFILE_AM411_20140321_200815.dmp
-rw-r----- 1 oracle    asmadmin  319M Mar 21 20:12 MIG_DUMPFILE_STG_LKP_BSS_CA_20140321_201126.dmp
-rw-r----- 1 oracle    asmadmin   28K Mar 23 14:27 MIG_DUMPFILE_AI317_20140323_142700.dmp
-rw-r----- 1 oracle    asmadmin   36K Mar 24 09:53 MIG_DUMPFILE_TEST_20140411_060116.dmp
-rwxrwxrwx 1 migration gdma      537M Mar 24 10:20 MIG_DUMPFILE_LKP_ast_20140324_102003.dmp
-rwxrwxrwx 1 migration gdma       56K Mar 24 10:33 MIG_DUMPFILE_LKP_LTE1_20140324_103354.dmp
-rwxrwxrwx 1 migration gdma       56K Mar 24 10:34 MIG_DUMPFILE_LKP_LTE2_20140324_103429.dmp
-rw-r----- 1 oracle    asmadmin  1.4G Mar 26 00:36 MIG_DUMPFILE_AM414_20140326_003118.dmp
-rw-r----- 1 oracle    asmadmin   42M Mar 26 01:45 MIG_DUMPFILE_SR414_20140326_014525.dmp
-rw-r----- 1 oracle    asmadmin  475M Mar 26 02:24 MIG_DUMPFILE_OM410_20140326_022241.dmp
-rw-r----- 1 oracle    asmadmin  2.4G Mar 26 09:27 MIG_DUMPFILE_OM411_20140326_091908.dmp

Expected output:

-rw-r----- 1 oracle    asmadmin  1.7G Mar 17 09:27 MIG_DUMPFILE_OM411_20140317_091908.dmp
-rw-r----- 1 oracle    asmadmin  1.4G Mar 26 00:36 MIG_DUMPFILE_AM414_20140326_003118.dmp
-rw-r----- 1 oracle    asmadmin  2.4G Mar 26 09:27 MIG_DUMPFILE_OM411_20140326_091908.dmp

Can this be acheived without awk or sed ?

Is there any reason for NOT using sed or awk? I often see such requests for home works?

ls -lrht *.dmp | awk '$5 ~ /G/'
1 Like

Its because I don't know awk and sed :slight_smile:

Thank you Srinishoo

Question1.
In your solution

awk '$5 ~ /G/' 

, what is the role of tilde (~) character and /G/ ?

Question2.
How did you manage to ignore the varying spaces between the fields ?

ie. If you Subtitute space with red underscores for readability as shown below, you can see 4 underscores between user and group
and 1 underscore between group and size field

-rw-r----- 1 oracle____asmadmin_1.7G Mar 17

A1: a tilde is a regex match and /pattern/ is how those are quoted, verus text which is quoted as "text" .

A2:
by default, fields are separated by any amount of whitespace.

1 Like
$ find -name "*.dmp" -size +1G -exec ls -lrth {} \;
1 Like

With a recent bash, you can make use of its "here strings" and get rid of awk etc:

ls -lh| while read line; do read x x x x SIZE x <<< "$line"; [ "${SIZE//[^G]}" ] && echo "$line"; done

What does -lrth do when ls is run once per file? It certainly won't sort by time or reverse-time.

If you ended it with + instead of ; then find would feed multiple filenames into ls, allowing it to sort. As long as find finds no more files than can fit in one commandline, that is. Otherwise, ls would be run more than once, and you'd end up with two different sorts concatenated together.

1 Like