3

I'd like to list all the files in HDD including size, last modified date and absolute path.

idea format :

[file size] [last modified date] [absolute path] [ filename] 

I tried:

ls -lhR | awk -v PWD=$PWD '{printf("%4s %4s %2s %5s %s/%s\n", $5, $6, $7, $8, PWD, $9); }' >> /tmp/report.list 

It worked perfectly until it hit some files/directories with space in-between.... So I tried:

find . -name "*" -exec du -sh {} \; -exec date -r {} \; -exec echo " " \; >> /tmp/report.list

But the output seems spread into 3 lines instead of 2, also the printed path is not the absolute path to the file.

What should I do?

2 Answers 2

4

You can use find's -printf action to output those attributes directly e.g.

find "$PWD" -printf '%s\t%Tx\t%h\t%P\n'
  • %s: File's size in bytes.
  • %Tx: File's last modification time, in locale's date representation
  • %h: Leading directories of file's name
  • %P: File's name with the name of the command line argument under which it was found removed.

The directory path (%h) is printed relative to the command-line path argument with which the find command is invoked: by specifying the working directory using $PWD, it becomes absolute.

1

You can use the -printf option in find:

find / -type "f" -printf "%s\t%TY-%Tm-%Td\t%p\n" > file.txt

This makes a file with format:

[size in bytes]TAB[Last modified date in YYYY-mm-dd format]TAB[FULLPATH]

It will run on the full file structure.

Replace each \t with a space if you don't want it tab delimited. Using man find you can find other format options for the -printf option in find.

2
  • Thanks, however, it print in bytes. some of my files are Gigabytes large. what I did now (kind of clumsy) is: find . -not -path '/\.' -type f ( ! -iname ".*" ) -exec du -sh {} \; -exec date -r {} \; > /tmp/find.list awk 'NR%2{printf $0" ";next;}1' /tmp/find.list >> /tmp/report.list Jun 5, 2015 at 9:01
  • @MartinWang If this is your answer, than you should write an answer. =)
    – A.B.
    Jun 8, 2015 at 15:22

You must log in to answer this question.

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