I'm trying to do some cleanup similar to this question. In a UNIX OS, I want to delete the directories that aren't being symbolic linked to in a given directory.
e.g. I have a deployment script that creates a directory structure for an app like this:
1.0-201103071711/
1.0-201103071718/
1.0-201103071729/
current -> /opt/myapps/fooapp/1.0-201103071729/
I want to create a script that will remove the directories in that directory that aren't the "current" directory. Thanks!
This Bash snippet will find directories that have no directories linked to them. It will also find broken links.
It does not look outside the current directory or in subdirectories.
Be sure to test it thoroughly.
As the referenced question mentioned, there isn't an official way to do this. As an alternative to Dennis' option, I present the following horrible hack. Please be sure to test thoroughly as well. I'd recommend replacing the
-exec
portion with anls
until you're sure it works.touch current;find . -maxdepth 1 -type d -mmin +2 -exec rm -rf {} \;
It will check the current directory only. It essentially touches the directory linked by 'current', and then removes anything with a modify time older than two minutes. If the other directories haven't been touched in a while, you can increase the timer on
-mmin
, so you don't accidentally nuke the directory you want. This is obviously only viable if you don't mind changing the timestamp on the current directory.--Christopher Karel