8

What's exactly the command to show files younger than 2 days? I thought about something like this:

$ find / -mtime -2

...but I'm not sure how to print the date of the found files. My target is to find file on a mounted ntfs system which are new or changed within the last days.

Moreover it would be very helpful to sort the results from the newst to the oldest. Is that possible?

2
  • If you edit your question to ask for something else, you should at least inform those, who answered confidently, that their answer is not longer valid, since new visitors will mostly vote on their current experience and not on the hard to examine history. More so, adding additional requirements later is IMHO bad style. Open a new question for that. With the requirements in mind, people might have choosen a different approach. Feb 8 '12 at 2:37
  • It was just a spelling mistake in my question text. The topic itself was correct. In body, I wrote "younger" instead of "older". My code example was not changed too. But basically you are right.
    – Bevor
    Feb 8 '12 at 7:39
10

Files created or modified less than 48 hours ago
sorted from the newest to the oldest:

find / -mtime -2 -printf "%T@" -ls | sort

I have found %T@ from man find: last modification time (seconds since epoch)

5
  • I'd like to see the date of the found files, not just the files itself. I updated the question too, because it would be very helpful to sort the results from the newest to the oldest.
    – Bevor
    Feb 7 '12 at 21:28
  • The last command is what I'm searching for, thanks.
    – Bevor
    Feb 7 '12 at 21:53
  • I needed "younger than" 2 days, so from today to two days before (my question was wrong at first, I wrote "older than", my mistake) but it works when I set -2 instead of +2. I didn't tested it on ntfs now, I have to wait until tomorrow for that, but I think it works there too (as on my local Ubuntu). If not, I'll let you know.
    – Bevor
    Feb 7 '12 at 22:11
  • Works perfect :)
    – Bevor
    Feb 8 '12 at 19:10
  • Great :-D See you
    – oHo
    Feb 8 '12 at 19:17
2

If you pipe to xargs, then you can easily adjust the output/format options of ls to show date, sort by date, etc.

find / -type f -mtime -2 -print0 | xargs -0 ls -lt

1

This is easier in zsh, thanks to its glob qualifiers.

print -l /**/*~/(proc|dev|sys)/*(m-2om)

This lists all files under /, excepts for files under /proc or /dev or /sys, which have been modified within the last 2 days (m-2). The files are listed in reverse chronological order of modification time (om), one per line (print -l).

If you want to list the file metadata as well, you don't need to sort in the shell, only filter. You may run into a platform-dependent command line length limit if there are too many matching files.

ls -dlt /**/*~/(proc|dev|sys)/*(m-2)

If you want to match only regular files, add . inside the parentheses.

With only POSIX tools, you can use ls -dlt for sorting and find to generate the file list. Again, you may run into a platform-dependent command line length limit if there are too many matching files. If you want to match only regular files, add -type f before or after mtime -2.

find / -mtime -2 -exec ls -dlt {} +
1

(Note: One sentence of the question originally asked for dates, older than 2 days in the text, while the headline had it the other way round. The need for sorting the output was introduced later too).

find / -mtime -2 -ls 

With +2 you see files older than 2 days, with -2 younger than two days, and with just 2: 2 days old files.

With -ls you get a ls -l like output, where you can look at the files date.

find / -ctime -2 -ls 

With ctime instead of mtime, you get the creation date instead of the modification date, but depending on the file systems capabilities and settings in the /etc/fstab this might or might not work.

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.