Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I need to delete all files in a directory that were created in 2009. What command can I use to do that?

share|improve this question
add comment

3 Answers 3

Most filesystems don't store the creation time of a file, so the best you can do is check the last time the file was modified.

If you have a recent version of GNU find (e.g. on Linux or Cygwin) or FreeBSD or OSX, you can directly compare the date of a file with that of another file. In addition, these versions of find can use the file creation time (called its birth time, indicated with a B) if it's available on your system. Replace B by m below to use the modification time rather than the birth time.

find /path/to/directory -newerBt '2009-01-01' ! -newerBt '2010-01-01' -type f -delete

Run it once without -delete first to make sure these are the files you want. The command above will also delete files in subdirectories; if this is not desired, add -mindepth 1 -maxdepth 1 after the directory name.

If your version of find doesn't have the -newerXY primary, you'll need to create timestamp files to mark the boundaries of the range of times you want to match.

touch -t 200901010000 /tmp/from
touch -t 201001010000 /tmp/to
find /path/to/directory -newer /tmp/from ! -newer /tmp/to -type f -exec rm {} +

Zsh's glob qualifiers can match files in a time interval, but the boundaries can only be indicated relative to the current date (e.g. N days ago).

rm /path/to/directory/*(.m+566^m+931)

You can also use timestamp files for precise dates, but you lose in terseness.

touch -t 200901010000 /tmp/from
touch -t 201001010000 /tmp/to
rm /path/to/directory/*(.e\''[[ $REPLY -nt /tmp/from && $REPLY -ot /tmp/to ]]'\')
share|improve this answer
add comment

Others have said find but you may also want to check out tmpwatch.

share|improve this answer
add comment

The find command, with the proper options should be able to do exactly what you want.

Since you phrased your question pretty much like a homework assignment, I'm going to leave a thorough read of the manual page as an exercise for the clued; this tutorial provided by @jfgagne should help quite a bit.

share|improve this answer
1  
'ctime' is not the creation time of the file, it is the last time the inode structure changed. So it will be updated when file permissions or ownership is changed. See this tutorial for more info. –  jfgagne Jul 21 '11 at 14:49
    
Whoops, my bad. Corrected my answer and added your tutorial -- thanks! –  Shadur Jul 21 '11 at 14:57
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.