user2487539
user2487539

Reputation: 11

Find and delete old files excluding some subdirectories

I have been searching for a while, but can't seem to get a succinct solution. I am trying to delete old files but excluding some subdirectories (passed via parm) and their child subdirecories.

The issue that I am having is that when the subdirectory_name is itself older than the informed duration (also passed via parm) the find command is including the subdirectory_name on the list of the find. In reality the remove won't be able to delete these subdirectories because the rm command default option is f.

Here is the find commmand generated by the script:

find /directory/ \( -type f -name '*' -o -type d \
    -name subdirectory1 -prune -o -type d -name directory3 \
    -prune -o -type d -name subdirectory2 -prune -o \
    -type d -name subdirectory3 -prune \) -mtime +60 \
    -exec rm {} \; -print 

Here is the list of files (and subdirectories brought by the find command)

/directory/subdirectory1  ==> this is a subdreictory name and I'd like to not be included   
/directory/subdirectory2  ==> this is a subdreictory name and I'd like to not be included      
/directory/subdirectory3  ==> this is a subdreictory name and I'd like to not be included        
/directory/subdirectory51/file51               
/directory/file1 with spaces 

Besides this -- the script works fine not bringing (excluding) the files under these 3 subdirectories: subdirectory1, subdirectory2 and subdirectory3.

Thank you.

Upvotes: 1

Views: 2416

Answers (2)

Amit
Amit

Reputation: 20456

Following command will delete only files older than 1 day. You can exclude the directories as shown in the example below, directories test1 & test2 will be excluded.

find /path/ -mtime +60 -type d \( -path ./test1 -o -path ./test2 \) -prune -o -type f -print0 | xargs -0 rm -f 

Though it would be advisable to see what's going to be deleted using -print

find /path/ -mtime +60 -type d \( -path ./test1 -o -path ./test2 \) -prune -o -type f -print

Upvotes: 2

Stephane Chazelas
Stephane Chazelas

Reputation: 6239

find /directory/ -type d \(
    -name subdirectory1 -o \
    -name subdirectory2 -o \
    -name subdirectory3 \) -prune -o \
    -type f -mtime +60 -print -exec rm -f {} +

Note that the AND operator (-a, implicit between two predicates if not specified) has precedence over the OR one (-o). So the above is like:

find /directory/ \( -type  d -a \(
    -name subdirectory1 -o \
    -name subdirectory2 -o \
    -name subdirectory3 \) -a -prune \) -o \
    \( -type f -a -mtime +60 -a -print -a -exec rm -f {} + \)

Note that every file name matches the * pattern, so -name '*' is like -true and is of no use.

Using + instead of ; runs fewer rm commands (as few as possible, and each is passed several files to remove).

Do not use that code above on directories writeable by others as it's vulnerable to attacks whereby the attacker can change a directory to a symlink to another one in between the time find traverses the directory and calls rm to have you delete any file on the filesystem. Can be alleviated by changing the -exec part with -delete or -execdir rm -f {} \; if your find supports them.

See also the -path predicate if you want to exclude a specific subdirectory1 instead of any directory whose name is subdirectory1.

Upvotes: 0

Related Questions