Hans Lindahl
Hans Lindahl

Reputation: 23

Delete all files and directories - except specified

Been looking for this for a while. We have a common directory where everyone has rwx rights. To keep this from growing out of proportions, I need to clear this once a month. No problem, except I'm supposed to keep 2 directories - one of which has spaces in its name.

I have this find command that lists what should be deleted;

find /COMMON/* | grep -v 'keepthis' | grep -v 'keep this too'

However, I'm at at total loss as to how I could rm or delete the output

I've googled an found millions of suggestions but they don't fit - I believe it's because of the spaces in the directory name.
So: how should the command end in order to work?

Upvotes: 2

Views: 306

Answers (1)

mirkobrankovic
mirkobrankovic

Reputation: 2347

Send to xargs and then rm:

find /COMMON/* -maxdepth 0 | grep -v 'keepthis' | grep -v 'keep this too' | xargs -rd '\n' rm -r --

edited after your suggestion :)

Upvotes: 1

Related Questions