7

Is there a way to run ls or find to get the list of files within a directory and then run stat to get all of the specific information (i.e. File Group, File Name, File Owner, File Size (displayed in K, M, etc.) & Permissions? I was trying something along the lines of:

find .content/media -type f | stat
ls -l .content/media | stat

Answer:

    find ./content/"subdirectory name"/ -type f -exec stat -c '%n : %U : %A : %G : %s' {} +
  • 1
    find will recursively list all the subdirectories within its argument. ls will not; for the equivalent of the ls pipe, just do stat ./content/media/*. – Nick Matteo Apr 17 '16 at 17:55
13

Use stat on the -exec action of find:

find .content/media/ -type f -exec stat -c '%n : %U : %G : %s' {} +

Change the format sequences of stat to meet your need.

| improve this answer | |
  • 1
    Could you explain what the -exec command does? I understand that it runs a section of "program". In this instance is it running executing -stat as a program after running -find? – lettda Apr 17 '16 at 16:08
  • 2
    @lettda -exec is an action of find..you can use it to run some other program over the result of find ..check man find – heemayl Apr 17 '16 at 16:25
12

If you have GNU find, you can use -printf:

find content/media/ -type f -printf '%p : %u : %g : %k'
| improve this answer | |
6

Throw xargs into the mix. E.g.:

ls | xargs stat
| improve this answer | |
  • This will fail with spaces or newlines in the filenames. – Martin Ueding Apr 17 '16 at 11:07
  • 3
    To handle newlines or spaces you would use: find -print0 | xargs -0 stat. – dshepherd Apr 17 '16 at 15:21
  • If one is using ls that's a different answer. – Ken Ingram Sep 20 '19 at 1:12
3

What is wrong with a simple Bash for-loop?

for f in ./*
do
    stat "$f"
done

With the quotes and the ./ prefix, this is safe against the worst file names.

| improve this answer | |
  • 1
    Or just stat * – Nick Matteo Apr 17 '16 at 17:56
  • Oh, if stat supports that, even better. I would still go with ./* as a file starting with -h would cause undesired behavior otherwise. – Martin Ueding Apr 17 '16 at 18:14
  • Use stat -- * then. – David Foerster Apr 17 '16 at 19:22
  • Yes, but the program needs to support the double dash, not all do. Therefore the dot-slash is more universal. – Martin Ueding Apr 17 '16 at 19:29
0
find .content/media -type f -exec stat -c '%n : %U : %G : %s : %x : %y : %z' {} +

%n     File name,
%U     User name of owner,
%G     Group name of owner,
%s     Total size, in bytes,
%x     Time of last access,
%y     Time of last modification,
%z     Time of last change.
| improve this answer | |

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.