Exclude directories and keep specific ones

charmander

New Member
Joined
Jul 7, 2020
Messages
1
Reaction score
0
Credits
17
Hello friends.

I have some doubts about the find command.

I need to create a command to search and remove directories with more than 30 days without modification, but there are directories with more than 30 days without modification that must remain on the server.

So, I created a command, but it doesn't work and I don't know how to make it work:

find /directory -type d -mtime +30 -exec rm -rf !(directories-which-cannot-be-excluded) \;

I created a shell script to perform a cleanup, but it would delete everything, not just files older than 30 days without modification.

#!/bin/bash
cd / directory
rm -rf !(file-which-cannot-be-excluded)

Is there a way to exclude directories with more than 30 days without modification, keep some directories with more than 30 days without modification and keep those with less than 30 days of creation?
 


You can use the -prune option to exclude directories.
So first try something like this, just to make sure that the correct directories are identified:
Bash:
find /directory -type d -mtime +30 \( -path /directory/to/exclude1 -o -path /directory/to/exclude2 \) -prune -o -print
What the above does is searches /directory for directories that were modified more than 30 days ago.
If any of the directories found match the paths collected in the parentheses - those results are pruned/ignored, otherwise the -print is ran and the path to the directory is printed to the screen.
That way you can see a list of candidates for removal and make sure that your command is correct.

The paths specified between the parentheses /( and /) are the directories that we want to exclude from our search. And between each specified -path, you need to make sure you use an -o (OR switch) if there is more than one path you want to exclude.
So if you had four paths you wanted to exclude, you would specify them like this in the find command:
Code:
\( -path /path/to/dir1 -o -path /path/to/dir2 -o -path /path/to/dir3 -o -path /path/to/dir4 \)

ALSO NOTE: In the first snippet I have used -print, so you can see a list of the directories that are candidates for removal. I recommend running that to ensure that the directories listed are OK for removal.

Once you're happy that the command is showing the correct directories, then you can swap out the -print for an -exec rm -rf {} \; in order to remove the directories.
If we weren't using -prune, you could have simply used -delete. But because -prune is involved, you will have to use an -exec with rm.

So you'd end up with something like this:
Bash:
find /directory -type d -mtime +30 \( -path /directory/to/exclude1 -o -path /directory/to/exclude2 \) -prune -o -exec rm -rf {} \;

As stated previously - have a play with the first snippet I posted using -print until you are happy that you are seeing the correct directories, before swapping out the -print for the -exec rm -rf {} \;
 
Last edited:

Staff online

Members online


Top