find command

Asked by Austin Texas on 2012-07-26

When I enter the command
find $HOME -cmin -5
I get the list of all files created in the last 5 minutes
I am trying to figure out how to display the date and time in those results.
I have searched the man page
but the closest I could get is
find $HOME -cmin -5 -print '%ADa'
and that does not work.
any ideas?

Question information

English Edit question
Ubuntu findutils Edit question
No assignee Edit question
Solved by:
Last query:
Last reply:
Best tuxar (tuxar) said : #1

Please try the next code:

find $HOME -cmin -5 -printf '%TY-%Tm-%Td %TT %p\n'


find $HOME -cmin -5 -exec ls -la {} \;

Austin Texas (linuxmint18) said : #2

Those are both very interesting. And they both work.

Austin Texas (linuxmint18) said : #3

Thanks tuxar, that solved my question.

Austin Texas (linuxmint18) said : #4

I discovered another one which gives good results
find . -cmin -5 | while read FILE;do ls -d -l "$FILE";done

Hi Austin, no, you don't want to do the while-loop method.

tuxar's first solution has find printing the date and time. (The format string may be simpler as -printf '%Tx %TX %p\n'.) The second is causing ls(1) to be run for each and every path, that has considerable overhead. (As you note later, a -d is needed to stop ls from listing the contents of a directory rather than details on the directory itself and -a isn't wanted.) The overhead can be lessened with later finds by using "-exec ls -ld {} +". The plus instead of the semicolon causes find to collect up the paths until it has quite a few and then call ls for all of those in one go. This will greatly cut down the number of ls invocations but there will still be some compared with the completely-internal first solution.

Your find | while-loop has the pipeline as a textual interface with each line being taken as a path. If a path happens to contain a linefeed character then ls will be run twice, once with the first part and again with the second. Also, as that hints, you're back to running at least one ls per path, with all the overhead that brings.

The -printf is the best solution for efficiency and correctness given your precise requirements.

Austin Texas (linuxmint18) said : #6

Thanks, Ralph
Your input is very much appreciated.