I have a number of zip files in a certain folder. I want to capture file name and the size of the files in a CSV format in day wise and put it in crontab so I can report on a daily basis, and then mail it in each report.

Files are in /somedirectory/archive/test:

-rw-rw-r--+ 1 AAAA AAAA 9.3M May  30 17:09 XXXXX_20140530_0401_28.txt.gz
-rw-rw-r--+ 1 AAAA AAAA 9.3M May  30 17:09 XXXXX_20140530_0401_29.txt.gz
-rw-rw-r--+ 1 AAAA AAAA 9.3M May  31 17:09 XXXXX_20140531_0401_01.txt.gz

Now I want to crate a cronjob to run everyday and capture the same CSV which we prepared in day wise.

example test30052014.csv:

"File Name","size","date"
"XXXXX_20140530_0401_28.txt.gz", "9.3","May 30"
"XXXXX_20140530_0401_29.txt.gz", "9.3","May 30"


"File Name","size","date"
"XXXXX_20140531_0401_01.txt.gz", "9.3","May 31"
  • 1
    Why do you need the date in the CSV if it is already part of the filename. And I can recommend always using YYYYMMDD when using a date in the .csv filename (as with your tarfiles), so listing the files by filename orders them chronologically ( and you don't get May 30th next to June 30th).
    – Anthon
    Jun 3 '14 at 8:10

a simple way to do this would be to put in a script:

echo "File Name,Size,date" > /tmp/output_file
ls -l --time-style=+%m.%d.%Y /path/to/file |grep -v "^total|^d"|awk '{print $NF","$4","$5 }'  >> /tmp/output_file

This will remove the sum up at the beggining of the ls and also exclude the directories.

  • Shell command using awk and an example of the time-style parameter. Exactly what I needed.
    – John Reid
    Oct 30 '14 at 11:22

Here you go.


# Go to where the files are located
cd /somedirectory/archive/test

# Save IFS variable (if needed) and set it to newline.
IFS=$(echo -en "\n\b")

# List the files and for each one save the size, time and name. Then, save results.
echo '"File Name","size","date"' > output.csv
for line in `ls -lrt *.txt.gz`; do
        size=$(echo $line |awk -F ' ' '{print $5}');
        date=$(echo $line |awk -F ' ' '{print $6" "$7}');
        file=$(echo $line |awk -F ' ' '{print $9}');
        echo '"'$size'","'$date'","'$file'"' >> output.csv

# Return IFS to original status.

exit $code


$ cat output.csv 
"File Name","size","date"
"0","Jun 3","XXXXX_20140530_0401_28.txt.gz"
"0","Jun 3","XXXXX_20140530_0401_29.txt.gz"
"0","Jun 3","XXXXX_20140531_0401_01.txt.gz"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.