0

I'm trying to scan a backup drive and remove files older than about two years. However, there is one folder I want to be skipped, as it is where the needed older files go.

Here's the command I tried to use: find /mnt/backup/ -path '/mnt/backup/home/shares/Projects/Projects Archive' -prune -o -atime +731 -type f -delete*

I get the result below:

find: The -delete action automatically turns on -depth, but -prune does nothing when -depth is in effect. If you want to carry on anyway, just explicitly use the -depth option.

It seems find may be a dead end for this task, but am hoping someone might have some insight into how to work around this.

Thanks in advance. B

bkeahl
  • 19

2 Answers2

1

I don't understand find well enough to know if there's a more elegant solution, but you could do something like this:

find /mnt/backup -atime +731 | \ 
grep --null --invert-match '/mnt/backup/home/shares/Projects/Projects Archive' | \ 
xargs -0 -n 10 rm -r

This runs find to find the old files, removes files in the pruned path using grep, then uses xargs and rm -r to delete them. It's not ideal because depending on depth vs breadth behavior of find, it might delete a folder recursively, then try to delete a file in that folder which was already deleted. I like this style of compound command because instead of running the rm -r command you can run echo, to debug the script and make sure it's running as expecting before committing.

0

different approach to exclude the specific folder while deleting files older than two years

find /mnt/backup/ -path '/mnt/backup/home/shares/Projects/Projects Archive' -prune -o -atime +731 -type f -exec rm {} \;

https://linuxconfig.org/how-to-explicitly-exclude-directory-from-find-command-s-search

Toto
  • 19,304