Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm in a putty session and would like to delete files from a directory which were created in the year 2009.

Please could you give me the exact command?

share|improve this question
add comment

3 Answers

up vote 2 down vote accepted

First off, the fact that you are using ssh doesn't really matter. What matters is the 'shell' it has presented to you and what tools you have available. I'm going to assume you're in 'Bash' and have 'find' available.

I'm also going to assume that by 'created in year 2009' you mean 'older than 2010' as it makes it easier for me... if not then look past the break!

Try find:

find /path/to/directory/ -ctime +567 -delete

The above will delete anything more than 567 days old (roughly back to 2009 as of the date of this post)

Instead of -delete you 'could' use -print first. When you're sure its printing only what you want to delete, then use -delete.


If you don't want stuff deleted from 2008 then you 'could' do the following:

find /path/to/directory/ -ctime +567 -ctime -932 -delete

Those numbers probably aren't right though, you can fine-tune it.


Relevant sections of man find:

   -ctime n
      File's status was last changed n*24 hours ago.  See the comments
      for -atime to understand how rounding affects the interpretation
      of file status change times.


   Numeric arguments can be specified as

   +n     for greater than n,

   -n     for less than n,

   n      for exactly n.
share|improve this answer
1  
You can simply combine the two date options as in 'find /path/to/files -ctime +567 -ctime -932 -delete'. The -ctime options to find and logically and-ed, while the -932 refers to a date of less than 932 days in the past. –  eldering Jul 21 '11 at 15:11
    
Bah that did occur to me but it sounded too sensible to be true so I ignored it. I will edit my answer! –  PriceChild Jul 21 '11 at 15:33
1  
there's an error in your edited find command: the second ctime argument should have a minus (-932) to indicate newer than 932 days instead of older. –  eldering Jul 22 '11 at 8:38
    
Bah yes very true. It was also wrong in the original >.< –  PriceChild Jul 22 '11 at 8:48
add comment

Your best bet is to write a shell script on the target server. Plink provides no processing for stdin. See: http://stackoverflow.com/questions/603187/how-to-send-commands-to-putty

share|improve this answer
add comment

I assume you are using PuTTY to connect to some computer running some distribution of Linux using SSH. Unfortunately, most Linux distributions do not record the creation date for a file. You can only find out when a file was last modified and when it was last accessed.

If you want to find all files in the current directory that were last modified in 2009, use this command:

find * \( ! -newermt '2010-01-01' \) -newermt '2008-12-31 23:59' -type f

If you want to search subdirectories too, change the * to . and remove -type f like so:

find . \( ! -newermt '2010-01-01' \) -newermt '2008-12-31 23:59'

The previous two commands will only print out the file names. If you are certain you want to delete the files, add -delete to the end:

find * \( ! -newermt '2010-01-01' \) -newermt '2008-12-31 23:59' -type f -delete
share|improve this answer
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.