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/)

investmentbnker75 08-21-2008 02:40 PM

rm by date
 
Guys,

Im stumped on 1 last line of a script im writing, i need to have the script go into each directory called test* and rm anything older than 18 days from the day its executed. Cron will run it daily.

Heres what i have except what will determine the old than 18 days for removal. Sorry im a newb. Thanks!


#!/bin/bash

for i in test01 test02 test03 test04 test05 test06 test07 test08 test09 test10 test11 test12 ; do
rm -rf anything older than 30 days
done

matthewg42 08-21-2008 02:57 PM

Use find to identify files with an mtime older than 30 days and then feed the identified files to rm with xargs:
Code:

find . -name 'test*' -mtime +30 -type f -print0 |xargs -0 rm -f

investmentbnker75 08-21-2008 03:19 PM

Thanks Matthew! So it should look like:

#!/bin/bash

for i in test01 test02 test03 test04 test05 test06 test07 test08 test09 test10 test11 test12 ; do
find . -name 'file*' -mtime +10 -type f -print0 | xargs -0 rm -rf
done

File equals:

file_backup_backup14_20080106.tgz
file_backup_backup14_20080107.tgz
etc...

raconteur 08-21-2008 03:55 PM

You don't really need xargs or the for loop (and the -type parameter, while probably good practice, isn't strictly necessary either if you are sure these files are all regular files)...

Code:

find . -name "file_backup*" -print -type f -mtime +10 -exec rm -f {} \;

bos.beemer 08-21-2008 04:40 PM

I think the xargs approach is faster because it supplies all the arguments to rm that applies to all the files at once. However using -exec will supply the arguments one at a time and thus a bit slow. Besides I don't think you need -print0 either.

find . -name 'test*' -mtime +30 -type f |xargs -0 rm -f

matthewg42 08-21-2008 05:22 PM

With the xargs method, you don't need to use the "for i in ... do ... done", just the single line I provided should do it. The only thing you might have to watch for is that find will recurse into sub-directories, and that the pattern passed to -name is specific enough to only get the files you want.

As a test to see if it is going to operate on the right files, do this first:
Code:

find . -name 'test*' -mtime +30 -type f -print0 |xargs -0 ls -l
If that lists only the files you want to delete, replace the "ls -l" with "rm -f".

investmentbnker75 08-22-2008 12:29 PM

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?

kenoshi 08-22-2008 12:40 PM

I thought using xargs is faster due to the fact that using -exec will fork a child of find for every argument, whereas xargs simply passes the argument to the resulting command?

matthewg42 08-22-2008 02:17 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 with the -mtime option to find. If you want such complex behavior, you will probably want to write a perl script or something like that.

*edit* actually you might be able to do it with the printf option, and the -mtime option, ORed together with -o.

raconteur 08-22-2008 03:11 PM

Actually this would be pretty simple to do in the shell if you're using GNU versions of 'ls'.

Code:

ls -l --time-style=+%a
will display the weekday of the modification time on the files.

There are other formats for the style (man ls for specifics, and man date for the formats)

Using that info and the find commands listed above you can construct a simple shell script to do what you want.

investmentbnker75 08-22-2008 03:29 PM

Matthew/Raconteur,

Thanks for the response, but i am confused what the line in the script would be to remove all files but for the last 2 weeks and every Sunday for a two month period. Could you post an example? Thanks a million for the time and the help!

raconteur 08-22-2008 05:11 PM

This is quick and dirty, and only cursorily tested, the usual caveats apply. If I were writing this for keepsies I'd probably do a few things differently.

Change the initial file pattern to match whatever your files are named. I called it filter.sh but of course you can name it whatever you wish...

Remove the sharps if you want a very verbose version.

Code:

#!/bin/bash

for file in `find . -name "backup*" -print`; do
# echo "filter is examining $file"
  older_than_two_months=`find . -wholename $file -mtime +60 -print`
  if [ -n "$older_than_two_months" ]; then
#  echo "filter found that $file is older than two months, deleting"
    rm -f $file
  else
    older_than_two_weeks=`find . -wholename $file -mtime +14 -print`
    if [ -n "$older_than_two_weeks" ]; then
#    echo "filter found that $file is older than two weeks"
      sunday=`ls -l --time-style=+%a $file | awk '{print $6}'`
      if [ "$sunday" != "Sun" ]; then
#      echo "filter found that $file was not modified on a Sunday, deleting"
        rm -f $file
#    else
#      echo "filter found that $file was modified on Sunday, ignoring"
      fi
#  else
#    echo "filter found that $file is newer than two weeks"
    fi
  fi
done

hth

matthewg42 08-22-2008 05:46 PM

Do you want to descend into sub-directories, or just remove files from one directory?

raconteur 08-22-2008 05:54 PM

Quote:

Originally Posted by matthewg42 (Post 3256479)
Do you want to descend into sub-directories, or just remove files from one directory?

Good point, my stab at this assumes one directory.
However, I think if you added a -type f parameter to the initial find it would pass the multi-level test.

matthewg42 08-22-2008 06:15 PM

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
Please backup before doing this - I take no responsibility if it deletes the wrong files because I might have mis-interpreted your needs or made a mistake.

It is probably a good idea to run it without the "| xargs rm -f" on the end first and verifiy that the files it lists are indeed the ones you want to delete, and only the ones you want to delete. i.e.
Code:

find . -type f -mtime +14 -printf "%Cw %p\n" | grep -v ^0 | cut -c3-
OK, now for an explanation.

The -printf option to find can be used to print more information that the default (which is just to print the file path & name. In this case I used the format string "%Cw %p\n". Please look at the find manual page and find %C. If you read this section of the page, you should be able to see that "%Cw" means the day of week number (0-6) the file was last modified. (0 is Sunday). I could also have used %Ca which would print the day of week like "Sun", "Mon" and so on, but this is locale dependent, so it would not work on a machine with German language settings for example.

The next bit, "%p\n" just means the file path & name with a new line character after it.

The "-mtime +14" means to print only files older than 14 days since the last modification.

The "-type f" means to only print files.

The "." as the first option to find means "start in the current directory".

In you run the find command without the following pipeline, you will see something like this:

Code:

0 ./file1
3 ./file2
6 ./bob/file3

The number is the weekday of modification of the file (0=Sun, 3=Wed, 6=Sat), then the file name in question.

We do not want to delete files which were modified on a Sunday, so we exclude the lines beginning with 0 with the next part of the pipeline:
Code:

grep -v ^0
If the example output of the find command above is what you get, after the grep part, it will look like
Code:

3 ./file2
6 ./bob/file3

The next part of the pipeline:
Code:

cut -c3-
...prints only characters from column 3 onwards - it cuts off the day of week and the space. We now have just a list of files which we want to remove:
Code:

3 ./file2
6 ./bob/file3

This is the input to xargs which calls the specified command (rm -f) for groups of strings read from standard input - i.e. remove the listed files.

I hope that is clear. If you have further questions about it, feel free to ask.

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.

Mr. C. 08-26-2008 01:54 AM

A couple of changes were required to matthewg42's code. The idea was correct, just a minor and/or mixup, and should have used modification time instead of status change time:

Ok, let's generate the test files, and set the modification dates:

Code:

$ for month in june july august ; do
  for day in {1..30} ; do
      touch -t $(date -d "$month $day" +%y%m%d1200) $(date -d "$month $day" +%y-%m-%d-%A);
  done;
done
$ ll
total 0B
-rw-r--r--  1 mrc  mrc    0B Jun  1 12:00 08-06-01-Sunday
-rw-r--r--  1 mrc  mrc    0B Jun  2 12:00 08-06-02-Monday
-rw-r--r--  1 mrc  mrc    0B Jun  3 12:00 08-06-03-Tuesday
-rw-r--r--  1 mrc  mrc    0B Jun  4 12:00 08-06-04-Wednesday
-rw-r--r--  1 mrc  mrc    0B Jun  5 12:00 08-06-05-Thursday
-rw-r--r--  1 mrc  mrc    0B Jun  6 12:00 08-06-06-Friday
-rw-r--r--  1 mrc  mrc    0B Jun  7 12:00 08-06-07-Saturday
-rw-r--r--  1 mrc  mrc    0B Jun  8 12:00 08-06-08-Sunday
-rw-r--r--  1 mrc  mrc    0B Jun  9 12:00 08-06-09-Monday
-rw-r--r--  1 mrc  mrc    0B Jun 10 12:00 08-06-10-Tuesday
-rw-r--r--  1 mrc  mrc    0B Jun 11 12:00 08-06-11-Wednesday
-rw-r--r--  1 mrc  mrc    0B Jun 12 12:00 08-06-12-Thursday
-rw-r--r--  1 mrc  mrc    0B Jun 13 12:00 08-06-13-Friday
-rw-r--r--  1 mrc  mrc    0B Jun 14 12:00 08-06-14-Saturday
-rw-r--r--  1 mrc  mrc    0B Jun 15 12:00 08-06-15-Sunday
-rw-r--r--  1 mrc  mrc    0B Jun 16 12:00 08-06-16-Monday
-rw-r--r--  1 mrc  mrc    0B Jun 17 12:00 08-06-17-Tuesday
-rw-r--r--  1 mrc  mrc    0B Jun 18 12:00 08-06-18-Wednesday
-rw-r--r--  1 mrc  mrc    0B Jun 19 12:00 08-06-19-Thursday
-rw-r--r--  1 mrc  mrc    0B Jun 20 12:00 08-06-20-Friday
-rw-r--r--  1 mrc  mrc    0B Jun 21 12:00 08-06-21-Saturday
-rw-r--r--  1 mrc  mrc    0B Jun 22 12:00 08-06-22-Sunday
-rw-r--r--  1 mrc  mrc    0B Jun 23 12:00 08-06-23-Monday
-rw-r--r--  1 mrc  mrc    0B Jun 24 12:00 08-06-24-Tuesday
-rw-r--r--  1 mrc  mrc    0B Jun 25 12:00 08-06-25-Wednesday
-rw-r--r--  1 mrc  mrc    0B Jun 26 12:00 08-06-26-Thursday
-rw-r--r--  1 mrc  mrc    0B Jun 27 12:00 08-06-27-Friday
-rw-r--r--  1 mrc  mrc    0B Jun 28 12:00 08-06-28-Saturday
-rw-r--r--  1 mrc  mrc    0B Jun 29 12:00 08-06-29-Sunday
-rw-r--r--  1 mrc  mrc    0B Jun 30 12:00 08-06-30-Monday
-rw-r--r--  1 mrc  mrc    0B Jul  1 12:00 08-07-01-Tuesday
-rw-r--r--  1 mrc  mrc    0B Jul  2 12:00 08-07-02-Wednesday
-rw-r--r--  1 mrc  mrc    0B Jul  3 12:00 08-07-03-Thursday
-rw-r--r--  1 mrc  mrc    0B Jul  4 12:00 08-07-04-Friday
-rw-r--r--  1 mrc  mrc    0B Jul  5 12:00 08-07-05-Saturday
-rw-r--r--  1 mrc  mrc    0B Jul  6 12:00 08-07-06-Sunday
-rw-r--r--  1 mrc  mrc    0B Jul  7 12:00 08-07-07-Monday
-rw-r--r--  1 mrc  mrc    0B Jul  8 12:00 08-07-08-Tuesday
-rw-r--r--  1 mrc  mrc    0B Jul  9 12:00 08-07-09-Wednesday
-rw-r--r--  1 mrc  mrc    0B Jul 10 12:00 08-07-10-Thursday
-rw-r--r--  1 mrc  mrc    0B Jul 11 12:00 08-07-11-Friday
-rw-r--r--  1 mrc  mrc    0B Jul 12 12:00 08-07-12-Saturday
-rw-r--r--  1 mrc  mrc    0B Jul 13 12:00 08-07-13-Sunday
-rw-r--r--  1 mrc  mrc    0B Jul 14 12:00 08-07-14-Monday
-rw-r--r--  1 mrc  mrc    0B Jul 15 12:00 08-07-15-Tuesday
-rw-r--r--  1 mrc  mrc    0B Jul 16 12:00 08-07-16-Wednesday
-rw-r--r--  1 mrc  mrc    0B Jul 17 12:00 08-07-17-Thursday
-rw-r--r--  1 mrc  mrc    0B Jul 18 12:00 08-07-18-Friday
-rw-r--r--  1 mrc  mrc    0B Jul 19 12:00 08-07-19-Saturday
-rw-r--r--  1 mrc  mrc    0B Jul 20 12:00 08-07-20-Sunday
-rw-r--r--  1 mrc  mrc    0B Jul 21 12:00 08-07-21-Monday
-rw-r--r--  1 mrc  mrc    0B Jul 22 12:00 08-07-22-Tuesday
-rw-r--r--  1 mrc  mrc    0B Jul 23 12:00 08-07-23-Wednesday
-rw-r--r--  1 mrc  mrc    0B Jul 24 12:00 08-07-24-Thursday
-rw-r--r--  1 mrc  mrc    0B Jul 25 12:00 08-07-25-Friday
-rw-r--r--  1 mrc  mrc    0B Jul 26 12:00 08-07-26-Saturday
-rw-r--r--  1 mrc  mrc    0B Jul 27 12:00 08-07-27-Sunday
-rw-r--r--  1 mrc  mrc    0B Jul 28 12:00 08-07-28-Monday
-rw-r--r--  1 mrc  mrc    0B Jul 29 12:00 08-07-29-Tuesday
-rw-r--r--  1 mrc  mrc    0B Jul 30 12:00 08-07-30-Wednesday
-rw-r--r--  1 mrc  mrc    0B Aug  1 12:00 08-08-01-Friday
-rw-r--r--  1 mrc  mrc    0B Aug  2 12:00 08-08-02-Saturday
-rw-r--r--  1 mrc  mrc    0B Aug  3 12:00 08-08-03-Sunday
-rw-r--r--  1 mrc  mrc    0B Aug  4 12:00 08-08-04-Monday
-rw-r--r--  1 mrc  mrc    0B Aug  5 12:00 08-08-05-Tuesday
-rw-r--r--  1 mrc  mrc    0B Aug  6 12:00 08-08-06-Wednesday
-rw-r--r--  1 mrc  mrc    0B Aug  7 12:00 08-08-07-Thursday
-rw-r--r--  1 mrc  mrc    0B Aug  8 12:00 08-08-08-Friday
-rw-r--r--  1 mrc  mrc    0B Aug  9 12:00 08-08-09-Saturday
-rw-r--r--  1 mrc  mrc    0B Aug 10 12:00 08-08-10-Sunday
-rw-r--r--  1 mrc  mrc    0B Aug 11 12:00 08-08-11-Monday
-rw-r--r--  1 mrc  mrc    0B Aug 12 12:00 08-08-12-Tuesday
-rw-r--r--  1 mrc  mrc    0B Aug 13 12:00 08-08-13-Wednesday
-rw-r--r--  1 mrc  mrc    0B Aug 14 12:00 08-08-14-Thursday
-rw-r--r--  1 mrc  mrc    0B Aug 15 12:00 08-08-15-Friday
-rw-r--r--  1 mrc  mrc    0B Aug 16 12:00 08-08-16-Saturday
-rw-r--r--  1 mrc  mrc    0B Aug 17 12:00 08-08-17-Sunday
-rw-r--r--  1 mrc  mrc    0B Aug 18 12:00 08-08-18-Monday
-rw-r--r--  1 mrc  mrc    0B Aug 19 12:00 08-08-19-Tuesday
-rw-r--r--  1 mrc  mrc    0B Aug 20 12:00 08-08-20-Wednesday
-rw-r--r--  1 mrc  mrc    0B Aug 21 12:00 08-08-21-Thursday
-rw-r--r--  1 mrc  mrc    0B Aug 22 12:00 08-08-22-Friday
-rw-r--r--  1 mrc  mrc    0B Aug 23 12:00 08-08-23-Saturday
-rw-r--r--  1 mrc  mrc    0B Aug 24 12:00 08-08-24-Sunday
-rw-r--r--  1 mrc  mrc    0B Aug 25 12:00 08-08-25-Monday
-rw-r--r--  1 mrc  mrc    0B Aug 26 12:00 08-08-26-Tuesday
-rw-r--r--  1 mrc  mrc    0B Aug 27 12:00 08-08-27-Wednesday
-rw-r--r--  1 mrc  mrc    0B Aug 28 12:00 08-08-28-Thursday
-rw-r--r--  1 mrc  mrc    0B Aug 29 12:00 08-08-29-Friday
-rw-r--r--  1 mrc  mrc    0B Aug 30 12:00 08-08-30-Saturday


Now, let's find the correct range of files to delete:
Code:

$ find . -type f \( -mtime +14 -o -mtime +60 \) -printf "%Tw %p\n"
0 ./08-06-01-Sunday
1 ./08-06-02-Monday
2 ./08-06-03-Tuesday
3 ./08-06-04-Wednesday
4 ./08-06-05-Thursday
5 ./08-06-06-Friday
6 ./08-06-07-Saturday
0 ./08-06-08-Sunday
1 ./08-06-09-Monday
2 ./08-06-10-Tuesday
3 ./08-06-11-Wednesday
4 ./08-06-12-Thursday
5 ./08-06-13-Friday
6 ./08-06-14-Saturday
0 ./08-06-15-Sunday
1 ./08-06-16-Monday
2 ./08-06-17-Tuesday
3 ./08-06-18-Wednesday
4 ./08-06-19-Thursday
5 ./08-06-20-Friday
6 ./08-06-21-Saturday
0 ./08-06-22-Sunday
1 ./08-06-23-Monday
2 ./08-06-24-Tuesday
3 ./08-06-25-Wednesday
4 ./08-06-26-Thursday
5 ./08-06-27-Friday
6 ./08-06-28-Saturday
0 ./08-06-29-Sunday
1 ./08-06-30-Monday
2 ./08-07-01-Tuesday
3 ./08-07-02-Wednesday
4 ./08-07-03-Thursday
5 ./08-07-04-Friday
6 ./08-07-05-Saturday
0 ./08-07-06-Sunday
1 ./08-07-07-Monday
2 ./08-07-08-Tuesday
3 ./08-07-09-Wednesday
4 ./08-07-10-Thursday
5 ./08-07-11-Friday
6 ./08-07-12-Saturday
0 ./08-07-13-Sunday
1 ./08-07-14-Monday
2 ./08-07-15-Tuesday
3 ./08-07-16-Wednesday
4 ./08-07-17-Thursday
5 ./08-07-18-Friday
6 ./08-07-19-Saturday
0 ./08-07-20-Sunday
1 ./08-07-21-Monday
2 ./08-07-22-Tuesday
3 ./08-07-23-Wednesday
4 ./08-07-24-Thursday
5 ./08-07-25-Friday
6 ./08-07-26-Saturday
0 ./08-07-27-Sunday
1 ./08-07-28-Monday
2 ./08-07-29-Tuesday
3 ./08-07-30-Wednesday
5 ./08-08-01-Friday
6 ./08-08-02-Saturday
0 ./08-08-03-Sunday
1 ./08-08-04-Monday
2 ./08-08-05-Tuesday
3 ./08-08-06-Wednesday
4 ./08-08-07-Thursday
5 ./08-08-08-Friday
6 ./08-08-09-Saturday
0 ./08-08-10-Sunday

Now, let's exclude Sundays, and delete the remainding files:
Code:

$ find . -type f \( -mtime +14 -o -mtime +60 \) -printf "%Tw %p\n" | \
  grep -v '^0' | cut -c3- | xargs rm -f
$ ll
total 0B
-rw-r--r--  1 mrc  mrc    0B Jun  1 12:00 08-06-01-Sunday
-rw-r--r--  1 mrc  mrc    0B Jun  8 12:00 08-06-08-Sunday
-rw-r--r--  1 mrc  mrc    0B Jun 15 12:00 08-06-15-Sunday
-rw-r--r--  1 mrc  mrc    0B Jun 22 12:00 08-06-22-Sunday
-rw-r--r--  1 mrc  mrc    0B Jun 29 12:00 08-06-29-Sunday
-rw-r--r--  1 mrc  mrc    0B Jul  6 12:00 08-07-06-Sunday
-rw-r--r--  1 mrc  mrc    0B Jul 13 12:00 08-07-13-Sunday
-rw-r--r--  1 mrc  mrc    0B Jul 20 12:00 08-07-20-Sunday
-rw-r--r--  1 mrc  mrc    0B Jul 27 12:00 08-07-27-Sunday
-rw-r--r--  1 mrc  mrc    0B Aug  3 12:00 08-08-03-Sunday
-rw-r--r--  1 mrc  mrc    0B Aug 10 12:00 08-08-10-Sunday
-rw-r--r--  1 mrc  mrc    0B Aug 11 12:00 08-08-11-Monday
-rw-r--r--  1 mrc  mrc    0B Aug 12 12:00 08-08-12-Tuesday
-rw-r--r--  1 mrc  mrc    0B Aug 13 12:00 08-08-13-Wednesday
-rw-r--r--  1 mrc  mrc    0B Aug 14 12:00 08-08-14-Thursday
-rw-r--r--  1 mrc  mrc    0B Aug 15 12:00 08-08-15-Friday
-rw-r--r--  1 mrc  mrc    0B Aug 16 12:00 08-08-16-Saturday
-rw-r--r--  1 mrc  mrc    0B Aug 17 12:00 08-08-17-Sunday
-rw-r--r--  1 mrc  mrc    0B Aug 18 12:00 08-08-18-Monday
-rw-r--r--  1 mrc  mrc    0B Aug 19 12:00 08-08-19-Tuesday
-rw-r--r--  1 mrc  mrc    0B Aug 20 12:00 08-08-20-Wednesday
-rw-r--r--  1 mrc  mrc    0B Aug 21 12:00 08-08-21-Thursday
-rw-r--r--  1 mrc  mrc    0B Aug 22 12:00 08-08-22-Friday
-rw-r--r--  1 mrc  mrc    0B Aug 23 12:00 08-08-23-Saturday
-rw-r--r--  1 mrc  mrc    0B Aug 24 12:00 08-08-24-Sunday
-rw-r--r--  1 mrc  mrc    0B Aug 25 12:00 08-08-25-Monday
-rw-r--r--  1 mrc  mrc    0B Aug 26 12:00 08-08-26-Tuesday
-rw-r--r--  1 mrc  mrc    0B Aug 27 12:00 08-08-27-Wednesday
-rw-r--r--  1 mrc  mrc    0B Aug 28 12:00 08-08-28-Thursday
-rw-r--r--  1 mrc  mrc    0B Aug 29 12:00 08-08-29-Friday
-rw-r--r--  1 mrc  mrc    0B Aug 30 12:00 08-08-30-Saturday


investmentbnker75 08-26-2008 07:52 AM

Perfect! Thanks a million Mr.C, I created the test env you listed in your post and ran it and it ran perfectly! I also then ran it in the test env i created, just to double check and it was perfect again. If you can, will you give a break down of your final find command that did the actual delete and how it determined what days were what and how removed exactly what was requested?

I could use it to understand Linux better and to write my own script next time. If not, thank you for your time.

Also, thanks to Matthew and rab for trying to help me out with this! Hopefully Mr.C post helped you too!


All times are GMT -5. The time now is 06:52 AM.