8

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
  • 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/*. Apr 17, 2016 at 17:55

5 Answers 5

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.

2
  • 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, 2016 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, 2016 at 16:25
12

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

find content/media/ -type f -printf '%p : %u : %g : %k'
8

Throw xargs into the mix. E.g.:

ls | xargs stat
3
  • This will fail with spaces or newlines in the filenames. Apr 17, 2016 at 11:07
  • 3
    To handle newlines or spaces you would use: find -print0 | xargs -0 stat.
    – dshepherd
    Apr 17, 2016 at 15:21
  • If one is using ls that's a different answer.
    – Ken Ingram
    Sep 20, 2019 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.

4
  • 1
    Or just stat * Apr 17, 2016 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. Apr 17, 2016 at 18:14
  • Use stat -- * then. Apr 17, 2016 at 19:22
  • Yes, but the program needs to support the double dash, not all do. Therefore the dot-slash is more universal. Apr 17, 2016 at 19:29
1
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.

You must log in to answer this question.

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