LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   rm by date (https://www.linuxquestions.org/questions/programming-9/rm-by-date-664415/)

matthewg42 08-22-2008 06:17 PM

P.S. if you do not want to descend into sub-directories, change the find part of the command, adding
Code:

-maxdepth 1

raconteur 08-22-2008 06:42 PM

Quote:

Originally Posted by matthewg42 (Post 3256508)
OK, if you want to descend into sub-directories as well as working in the current directory, here's the command for you:
Code:

find . -type f -mtime +14 -printf "%Cw %p\n" | grep -v ^0 | cut -c3- | xargs rm -f

Heh, I was just now goofing around to see if I could get all of this on one line (just to see if it could be done)... but I gave it up because the phone rang.

Your example doesn't quite meet the requirements, if I read this correctly:

"[...]remove all files but for the last 2 weeks and every Sunday for a two month period[...]"

But it is still well done, closer than I got in the time I fiddled with it.

I might still be able to do it all on one line, but its Friday and I have a date ;-) Have fun!

matthewg42 08-22-2008 07:00 PM

Quote:

Originally Posted by raconteur (Post 3256523)
Heh, I was just now goofing around to see if I could get all of this on one line (just to see if it could be done)... but I gave it up because the phone rang.

Your example doesn't quite meet the requirements, if I read this correctly:

"...remove all files but for the last 2 weeks and every Sunday for a two month period..."

I missed out the two month period, didn't I?

You just need to add
Code:

-a -mtime -60
...to the find command, assuming you think of a month as 30 days. You might need some brackets too to make it work right.

Something like this (I've used \ to break the lines to make it more readable - maybe...)
Code:

find . -type f \( -mtime +14 -a -mtime -60 \) -printf "%Cw %p\n" \
  | grep -v ^0 \
  | cut -c3- \
  | xargs rm -f

Have a nice date. Don't talk about Linux too much.

ghostdog74 08-22-2008 10:12 PM

Quote:

Originally Posted by investmentbnker75 (Post 3256193)
Thanks again Matthew, I wanted to try a variation of this, if it is possible. I would like to take files for the past two months and have a script that will keep the past two weeks and then every Sunday only. Is that possible?

not tested. To test, remove xargs
Code:

find . \(  -mtime -30 -a -mtime -14 \) -printf "%p:%Aa\n" | awk -F":" '$2=="Sun"{print $1}' | xargs rm -f

investmentbnker75 08-23-2008 11:05 AM

Thanks everyone for the time answering my questions. So the way i would try to run this is in a directory called /home/anyuser will live the script along with the directories the script will need to go into and then perform the function we're talking about. A ls -l would look like:

dir1
dir2
dir3
dir4
filecleanup.sh

In each of the directories called dir1, 2, etc..., etc are .tgz files called:
misc.20080101.gz
misc.20080102.gz
misc.20080103.gz
up to
misc.20080315.gz

So these .gz files will increment everyday and the script will run by cron once a day and keep only everything from the past two weeks from when the script will run plus every sunday from the past 60 days. (since the question posed earlier was if im using a 30 day period as a month)

matthewg42 08-23-2008 12:44 PM

You want to make sure the script does not delete itself. Also, when running from cron, you get a very minimal environment with a limited PATH, so you need to make sure any programs you call are in that limited PATH, or make sure you modify the PATH in your script.

I would recommend it looks something like this (note the \ at the end of lines must be the last character on the line - no spaces or anything should follow it) :

Code:

#!/bin/bash

find /home/anyuser/dir* -type f \( -mtime +14 -a -mtime -60 \) -printf "%Cw %p\n" \
  | grep -v ^0 \
  | cut -c3- \
  | xargs rm -f

Note that by explicitly telling find to start in /home/anyuser/dir* instead of "." it will not see the script itself and thus avoid problems with removing itself.

investmentbnker75 08-23-2008 05:58 PM

Matthew,

thanks for the response. What you suggested does keep everything from the past 14 days but then removes everything else but the first 4 days. so theres a big gap be between. Lets say for example we have files 01-60that are from june22-aug21, it removes everything but files 01,02,03,04 (sat-tues) and then 49-60.

It should remove everything but files 02,09,16,23,30,37,44,(all Sundays) and 49-60 (the last two weeks).

Thanks again for the help!

matthewg42 08-23-2008 06:45 PM

Maybe the modification date on the files is not set right - have the files been modified since they were created? Note that *nix filesystems do not store a creation timestamp, only access time and last modification time.

investmentbnker75 08-23-2008 07:22 PM

So your command should work in that scenario? What i did was create test directories with test files in them and then modified the dates with touch -t date testfile to create a test scenario before running them on legit files just in case.

matthewg42 08-23-2008 07:30 PM

I thought it would, but I didn't test it. Like I said - there may be mistakes... :-) You get the rough idea though?

jlinkels 08-23-2008 08:33 PM

No one mentioned this, but while you are experimenting, change 'rm' to 'echo'. Unless you are a coder who never makes any mistakes, it is likely that you make just that tiny little mistake which wipes out your hard drive.

Always substitute rm with echo while developing!

jlinkels

investmentbnker75 08-23-2008 09:39 PM

Hi Matthew,

I dont get the rough idea unfortunately. im a novice to linux. But if you give up, give me the break down of what your last command does and ill work on it from here and try to get it to work. Very much appreciate all your help!

investmentbnker75 08-25-2008 03:36 PM

Bump! a d bump!

matthewg42 08-25-2008 05:57 PM

All the breakdown I can provide I did already is post #15.

investmentbnker75 08-25-2008 10:39 PM

Thanks Matthew for trying. Maybe someone else can hit this nail on the head. Id really like to get this to work.


All times are GMT -5. The time now is 07:48 PM.