find command

Asked by Austin Texas

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
http://manpages.ubuntu.com/manpages/precise/en/man1/find.1.html#contenttoc7
but the closest I could get is
find $HOME -cmin -5 -print '%ADa'
and that does not work.
any ideas?

Question information

Language:
English Edit question
Status:
Solved
For:
Ubuntu findutils Edit question
Assignee:
No assignee Edit question
Solved by:
tuxar
Solved:
Last query:
Last reply:
Revision history for this message
Best tuxar (tuxar) said :
#1

Hello,
Please try the next code:

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

or

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

Revision history for this message
Austin Texas (linuxmint18) said :
#2

Those are both very interesting. And they both work.
Thanks

Revision history for this message
Austin Texas (linuxmint18) said :
#3

Thanks tuxar, that solved my question.

Revision history for this message
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

Revision history for this message
Ralph Corderoy (ralph-inputplus) said :
#5

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.

Revision history for this message
Austin Texas (linuxmint18) said :
#6

Thanks, Ralph
Your input is very much appreciated.