-1

I want to retrieve all the .log files from the directory /var/log, store the results in a csv file along with each log file's respective file size in kB.

I started by getting the files using find:

find . -type f -name "*.log"

how to save them in the file?

  • What did you try beyond running find? – Satō Katsura Jul 19 '16 at 14:21
  • 1
    GNU find has -printf which may be useful – ilkkachu Jul 19 '16 at 14:24
1

With GNU find:

find . -name '*.log' -printf '%p,%s\n'

That will print the filename and the file's size in bytes, separated by a comma. Use %f instead of %p if you only want the file's basename (i.e. without the path).

To display as kilobytes (units of 10^3, "KB") or kibibytes (units of 2^10, "KiB"), you'll need to post-process the output. See A standard tool to convert a byte-count into human KiB MiB etc; like du, ls1 for several ways of doing this. Using awk or perl is probably easiest because you only want to modify the 2nd field of each line.

0

This would be my best guess and then put the contents in a file in your home directory called var-log.csv

find . -type f -name "*.log" -exec ls -s {} \; > ~/var-log.csv
0

This might help:

ls -l --block-size=K *.log | awk {'print $9","$5'} > nameSize.csv
  • This only works sometimes. Something that only works some of the time is broken. Don't Parse ls – cas Jul 20 '16 at 4:21
-1

ls -l will give you the all the data you need and more:

: ls -l /var/log/*.log
...
-rw-r--r-- 1 root root 123456 Jul 11 17:28 /var/log/xinetd.log
...

Then you can extract the fields you need using awk:

: ls -l /var/log/*.log | awk '{print $5,$9}'
123456 /var/log/xinetd.log

If you want it separated with some other char:

: ls -l /var/log/*.log | awk '{print $5","$9}'
123456,/var/log/xinetd.log

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.