5

I have forgotten the name of a file which is in a directory or one of its subdirectories. I remember the file's extension (.nb) and know approximately when it was created. How can I list all file with this extension in the current directory and subdirectories along with their creation dates? I'm using Mac OS X.

4 Answers 4

4

The easy way to do this is with zsh, which is installed by default on macOS. Its glob qualifiers let you select and sort files by timestamp and other metadata. Zsh doesn't support creation times, but it supports modification times and inode change times; one of them probably matches the creation time.

This lists the 100 ([1,100]) most recent (om) regular (.) files with the .nb extension in the current directory and its subdirectories (**/):

zsh -c 'ls -ltr **/*.nb(.om[1,100])'

This lists files modified between 7 and 14 days ago:

zsh -c 'ls -ltr **/*.nb(.m+6m-14)'
3

This should accomplish what you want.

find . -type f -name '*.nb'|xargs stat -f '%c %N'|sort

Explanation

find . -type f -name '*.nb' find all files with .nb extension.

xargs stat -f '%c %N' print out the files with the unix timestamp in the front.

sort sort it.

1
  • Creation date would be birth time in the mac world. You might consider replacing change time-%c with birth time-%B.
    – fd0
    Nov 3, 2016 at 12:26
0

For bash (not mac):

find . -type f -iname '*.nb'| xargs -d '\n' stat -c '%z %N'|sort

find all entries of type 'file' in the current directory and below, matching pattern

(-iname to ignore case)

pipe it to xargs

(-d '\n' change delimiter to linefeed to allow for spaces in file names)

then to stat

(-c for output format %z = last status change %N quoted file name)

and sort the output

-1
find -name '*.nb' -exec ls -rtl {} +

Will show detailed info on all files matching the pattern, and sort them from oldest to newest (you can have from newest to oldest by removing the r option from the ls command).

The + at the end means you execute the command at the end of the search instead of running it for each match.

NOTE: this will only work with a reasonable amount of matches, if the command line is too long for ls it will be split up in several ls's, resulting in several sorted subsets while the whole list will not be sorted correctly (thank you @Gilles and @rudimeier for pointing it out).

4
  • 1
    This will not work if there are too many files from one single ls command.
    – rudimeier
    Nov 2, 2016 at 14:59
  • @rudimeier Why is that ? Nov 2, 2016 at 15:47
  • 1
    @ValentinB. Because if the command line is too long, find will execute ls several times, with successive subsets of the files to fit within the limit. Each subset will be sorted but not the whole. Nov 2, 2016 at 22:06
  • Thank you for the explanation @Gilles. I will edit my answer accordingly. Nov 3, 2016 at 8:42

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.