P.S. if you do not want to descend into sub-directories, change the find part of the command, adding
Code:
-maxdepth 1 |
Quote:
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! |
Quote:
You just need to add Code:
-a -mtime -60 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" \ |
Quote:
Code:
find . \( -mtime -30 -a -mtime -14 \) -printf "%p:%Aa\n" | awk -F":" '$2=="Sun"{print $1}' | xargs rm -f |
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) |
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 |
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! |
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.
|
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.
|
I thought it would, but I didn't test it. Like I said - there may be mistakes... :-) You get the rough idea though?
|
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 |
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! |
Bump! a d bump!
|
All the breakdown I can provide I did already is post #15.
|
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. |