Delete Files based on year from SSH

1

0

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?

Viralk

Posted 2011-07-21T14:13:23.533

Reputation: 113

Answers

2

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.

Pricey

Posted 2011-07-21T14:13:23.533

Reputation: 4 262

1You 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. – Jaap Eldering – 2011-07-21T15:11:03.757

Bah that did occur to me but it sounded too sensible to be true so I ignored it. I will edit my answer! – Pricey – 2011-07-21T15:33:47.800

1there'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. – Jaap Eldering – 2011-07-22T08:38:57.300

Bah yes very true. It was also wrong in the original >.< – Pricey – 2011-07-22T08:48:26.577

1

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

William Jackson

Posted 2011-07-21T14:13:23.533

Reputation: 7 646

0

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

mbrownnyc

Posted 2011-07-21T14:13:23.533

Reputation: 188