bash: Delete all files older than 1 month, but lea

2019-03-25 12:54发布

问题:

I have a lot of daily backup archives. To manage disk usage, I need a bash script that will delete all files older than 1 month, but keep all files created on Mondays, even if they are older than 1 month.

For example, this will delete all files last modified more than 30 days ago:

 find /path/to/files* -type f -mtime +30 -delete

But I don't really know how to keep files created on Mondays.

回答1:

Slightly simpler and more cautious version of @JoSo's answer:

find /path/to/files -type f -mtime +30 \
    -exec sh -c 'test $(date +%a -r "$1") = Mon || echo rm "$1"' -- {} \;

The differences:

  • Using date -r to get the last modification date of a file directly
  • Using %a to work with more comprehensible weekday names
  • Just echo the rm "$1" first to review what will be deleted. If looks good, then either stick | sh at the end to really execute, or remove the echo

However, @JoSo is right to point out that date +%a is locale dependent, so these versions would be indeed safer:

find /path/to/files -type f -mtime +30 \
    -exec sh -c 'test $(date +%u -r "$1") = 1 || echo rm "$1"' -- {} \;
find /path/to/files -type f -mtime +30 \
    -exec sh -c 'test $(LC_TIME=C date +%a -r "$1") = Mon || echo rm "$1"' -- {} \;


回答2:

As find to my knowledge has no weekday check, you need to call an external program.

find /path/to/files -type f -mtime +30 \
    -exec sh -c \
        '[ "$(date +%u -d @"$(stat -c %Y "$1")")" != 1 ] && rm "$1"' -- {} \;

Update: Using the -r switch to date (Kudos to Janos) and only testing, not deleting inside the shell command probably yields the cleanest possible version:

find /path/to/files -type f -mtime +30 \
    -exec sh -c 'test "$(date +%u -r "$1")" != 1' -- {} \; \
    -print  # or -delete instead


回答3:

you can use

stat -c %y yourfile

to get the date the file was created.

Then extract the date with cut to have a var like myvar=yyyymmdd

and finally use

date +%u --d $myvar
date +%u --d 20130822

it will return the day of week, if it returns 1 it was monday. In my case it return 4 because 22/08/2013 was a Thursday

Edit : if you can get all these working as a simple command line as Jo So suggest, it's better !



回答4:

What about avoiding subprocesses in loop:

find /path/to/files -type f -mtime +30 -printf '%Ta %p\n' \
    | grep -v ^Mon | cut -c5- | tr "\n" "\0" | xargs -0 rm -v


标签: bash sh