-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?

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255

4 Answers4

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.

cas
  • 78,579
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
-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