Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a folder where I sometimes get a few big files after doing some work. I delete them manually. I know it's possible to delete them after 1 day or more but I would like to delete them if they have been modified in the last few hours.

There are files in that folder I do NOT want to delete and they all have similar yet unpredictable filenames. I can't just use del to delete the newest ones, I have to rely on modify date.

share|improve this question
find /some/dir -type f -mmin -<minutes old> -print0 | xargs -0 rm

This finds all items under /some/dir that are files and are less than <minutes old> minutes old, and removes them. Don't forget the - before the number of minutes. Also, if mmin doesn't do the trick, try cmin.

share|improve this answer
If you're going to assume GNU find, this can be simplified to find /some/dir -type f -mmin -<minutes old> -delete. Run it first without the -delete option to show what it would delete. With POSIX find, find /some/dir -type f -mmin -<minutes old> -exec rm {} +. But this requires Unix utilities which are not bundled with Windows. – Gilles Aug 21 '10 at 10:41
Doh. Did the 'windows 7' tag get added after I answered? I didn't see it if not. I wasn't aware of -delete. To be frank I'm not a fan of -delete and -exec arguments to find; they seem like bloatware, and the escaping that -exec always required offended my delicate sensibilities. Still, additional knowledge is good; Thank you. – Slartibartfast Aug 22 '10 at 1:02
@slartibartfast: I added the Windows tag because the question body mentioned Windows. -exec is POSIX, whereas -print0 is not. (Mind, -mmin isn't POSIX either, so my objection wasn't justified.) Escaping find output for xargs is hard unless you have -print0; I like -exec ... + a lot better, and it doesn't require any escaping. – Gilles Aug 22 '10 at 23:31
Personal preference. I just think find+xargs is more unix-y (in the philosophical sense, not POSIX/non-POSIX). I also recommend grep -Z and possibly tar --null. (Note also that xargs can do controlled parallelism with -P) – Slartibartfast Aug 25 '10 at 1:42

IIRC the Windows Explorer can sort by modification time, so you could do that and select the files that were modified in the desired time range.

slartibartfast has given an answer that assumes you have Unix-like utilities installed. On Windows, this could be cygwin or GNU Win32 utilities or Microsoft Windows SFU or UWIN. If you install one of these, make sure to use its find and not the unrelated native Windows command.

share|improve this answer
I would first like to get this to work without installing any extra software and after im sure i can't be done i will try out some of the mentioned utilities. You said: "IIRC the Windows Explorer can sort by modification time, so you could do that and select the files that were modified in the desired time range." How do do this? – TMRW Aug 21 '10 at 12:06
@TMRW: Open the directory in Explorer. Select “Details” in the “Display” menu (the name may be different in your version of Windows). Then click on the header of the date&time column to sort by date&time. – Gilles Aug 21 '10 at 12:39
Yeah that's the manual way.I'm looking for an automated way via cmd. – TMRW Aug 21 '10 at 14:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .