1

I am using the stat commands to show information of all files in a directory, by using the below commands in a script:

  stat -c"%n|%s|%y" <FOLDER>/* | sed -r 'h; s/\|.*$//; s/^/wc -l </e; G;     s/\n/|/; s/^([^|]*\|)([^|]*\|)/\2\1/; s/( [^ ]*){1}$//;s#/#|#'  > Stats.csv

It gives me file information in a pipe-delimited file, Folder Name | File Name | Row counts | File Size | Date files arrived

An example:

AVIL|Applicability_Master_100K.txt|100000|6264377|2019-05-23 12:51:45.454241257
AVIL|Vendor_Master_20190528.txt|22677|3950461|2019-05-29 13:49:43.465444584 
AVIL|Sales_Order_20190527.txt|15056|4328799|2019-05-29 13:50:03.670264272 
AVIL|Purchase_Order_20190529.txt|10110|1514505|2019-05-29 13:49:22.360588383 

I want to change Mb to MB and get rid of milliseconds in the date column.

Thanks

4
  • 1
    Are you sure the file isn't already sized in MB? Files aren't generally sized in Mb - or is this purely fixing up an incorrect label? May 29, 2019 at 23:07
  • du -h <filename> gives me 3.8M (human readable MB or can be GB) du -k <filename> gives me 3860 (Mega bytes ? not sure) i want to incorporate du -h <file name> in the above mentioned stats -c command May 29, 2019 at 23:44
  • %w is not working for me it is showing " -- " in the date column, i am using x86_64 GNU/Linux May 30, 2019 at 17:28
  • Am I correct in understanding that field four, File Size, is now in bytes (not Mb as originally stated in your original post), and you want that changed to MB (i.e., divided by 10^6)? If so, how many decimal points do you want to show? For example, the first line of your example has a value for File Size of 6,264,377 bytes; do you want that to show as 6MB, 6.3MB, 6.26MB, or some other value? Please click edit and revise the next to the last line of your question accordingly. Please do NOT click [Add Comment] as when more comments arrive, old comments are pushed off the screen.
    – K7AAY
    May 30, 2019 at 20:26

1 Answer 1

1

To remove the last 10(ten) characters of the line, the milliseconds and period of the Date files arrived column, use rev and cut and add this at the end of your expression:

| rev | cut -c 10- | rev  
2
  • Thanks for the response, i am using stat -c"%n|%s|%y" -- wondering if there is any way to pass %n or some other valuses -- number %s size (in MB or GB format) % y (date time only) May 29, 2019 at 23:48
  • Here is the output AVIL|Applicability_Master_100K.txt|100000|6264377|2019-05-23 12:51:45.454241257 AVIL|Vendor_Master_20190528.txt|22677|3950461|2019-05-29 13:49:43.465444584 AVIL|Sales_Order_20190527.txt|15056|4328799|2019-05-29 13:50:03.670264272 AVIL|Purchase_Order_20190529.txt|10110|1514505|2019-05-29 13:49:22.360588383 May 30, 2019 at 17:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .