5

I basically want to combine the output of

find /

with the output of:

find / | xargs -L1 stat -c%Z

The first command lists the files in the / directory and the second lists the timestamp of each file. I want to combine these two such that I get the file and the timestamp on one like, like:

/path/to/file 1501834915
  • 3
    Note that file timestamp more commonly refers to the last modification time of the file (stat -c%Y with GNU stat) as opposed to the last status change time (%Z) – Stéphane Chazelas Aug 4 '17 at 9:16
9

If you have GNU find, you can do it entirely using find:

find / -printf '%p %C@\n'

The format specifiers are:

     %p     File's name.
     %Ck    File's last status change time in the format specified by
            k, which is the same as for %A.
     %Ak    File's last access time in the  format  specified  by  k,
            which  is  either `@' or a directive for the C `strftime'
            function.  The possible values for k  are  listed  below;
            some  of  them might not be available on all systems, due
             to differences in `strftime' between systems.

             @      seconds  since  Jan.  1,  1970,  00:00  GMT,  with
                    fractional part.

If you don't want fractional parts, use s instead of @ as the time format specifier. (There are a few systems without s, but Linux and *BSD/OSX do have s.)

find / -printf '%p %Cs\n'
  • 1
    It's likely he has GNU find. His stat could have been GNU or busybox stat, but since busybox xargs doesn't have -L, it's likely he's on a GNU system. – Stéphane Chazelas Aug 4 '17 at 9:22
  • For me, this is not entirely the answer. For me it outputs /path/to/file 1501831518.9714507480. I found a solution to my problem and will post an answer. – Gamer1120 Aug 4 '17 at 9:44
  • 2
    @Gamer1120 so strip the fractional part of the timestamp with sed. that's still going to be much faster than forking stat many thousands of times. e.g. find / -printf '%p %C@\n' | sed -e 's/\.[^.]*$//' – cas Aug 4 '17 at 12:39
  • Makes sense. I'll try. – Gamer1120 Aug 4 '17 at 13:26
  • 2
    @Gamer1120 Use s instead of @ if you don't want the fractional part. – Gilles Aug 5 '17 at 22:51
3

why don't you ask find to stat for you ?

find / -exec stat -c'%n %Z' {} +

find will run stat of every entry (file or directory).

  • Yup, that works! – Gamer1120 Aug 4 '17 at 9:45
0

I solved it by using

find / | while read filename
    do
    echo -n "$filename " && stat -c%Z $filename
done

but I won't use this solution, as Archemar's answer looks nicer.

  • 1
    to be honest muru's solution is even finer as find won't fork stat, so it should be faster as well (now there is fractionnal part in time ...) – Archemar Aug 4 '17 at 9:51
  • But I don't get the desired output :( See my answer on their solution. – Gamer1120 Aug 4 '17 at 9:58

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.