How "to rm -rf *", but excluding (leaving behind) symlinks ?
Hello, it's been ages since I came here for a question, good day to everyone :)
On my server, a Debian Squeeze, I need to "empty" a domain's public_html folder, however I want to leave behind the shortcuts added by my hosting panel (virtualmin), all of them being symlinks. In other words, I need to delete everything, except symlinks. rm -rf * would do too much, removing the symlinks alongside the rest. Would you know how it can be achieved ? Thank you VERY much if you can help me, thank you ! :) |
Maybe this thread can be of some help: Exclude a folder from a recursive delete?
Unless you make use of the GLOBIGNORE environment variable, you will not be able to use "rm -rf *" for your command. You'll need to use a script, find, or something similar. EDIT: I forgot to mention, the OP in the linked thread ended up using a script that tested whether the next directory to delete was a symbolic link. That same type of test can be merged into the find command. Perhaps "! -type l"... you'll need to experiment with it before doing a full-fledged rm to make sure you get the right results. |
Thank you Dark_Helmet ! :)
The globignore thing is entirely new to me (well, like so much stuff, I moved my websites to a dedi out of necessity rather than because I felt ready, haha), I'll have to dig into that. The chown trick (suggested later in that thread) is also a great idea, and I believe it WOULD do the trick. I'm still reluctant to doing that, for "purity" reasons I'd like to keep astray from becoming root, ideally I'd like to be able to do all I need as the single user. And there's the problem that if, some day, I have a folder with NUMEROUS symlinks instead of just 4 today, it will be an issue again and, to select all the symlinks, we'll be back to the present post. - To get back to business, if the "rm" command can't be used, then maybe I could search in another direction, as suggested in the first post of your linked thread. Is there a special wildcard that can allow to select only symlinks ? Would you know, please ? - In such a case, I could do that mv "only symlinks" ../ rm -rf * and then bringing the symlinks back. - or find "only symlinks -exec and move them to another folder - or even more simply "find all but symlinks" -exec rm -rf All these ideas would require being able to have a special char to only pick symlinks... |
Following command delete all regular files except symlinks:
find /path/to/del -type f -exec rm -rf {} \; I guess you needn't remove directories, it has no sense, directories can obviously contain symlinks. For more complex of using find, see 'man find' |
The most straightforward method could be:
Code:
find /path/to/dir ! -type l -delete Paranoid warning: this is a very dangerous command: please re-check the path of the top search directory (or use . if it is your current working directory) and the syntax of the command line before pressing enter. You may want to run the command without -delete to check if the list of files/directories matches only the objects you want to remove. |
You might have missed the EDIT that I added while you were typing your response. If you're concerned about the number of symbolic links growing to an unmanageable size, then yes, GLOBIGNORE is probably not a consideration.
A (relatively) short find command will be able to handle any number of symbolic links. For example: Code:
user@localhost$ ls -l That's what half the -prune business was about in that other thread. For instance: Code:
user@localhost$ find . -maxdepth 1 \( ! -type l \) -a \( -regex "\./.*" -print \) Code:
user@localhost$ find . -maxdepth 1 \( ! -type l \) -a \( -regex "\./.*" \) -exec rm -R "{}" \; EDIT: I type too much... EDIT2: And I'm dumb... instead of "! -type l" the simplest substitute would be "-type d" Code:
user@localhost$ find . -maxdepth 1 -type d -regex "\./.*" -exec rm -R "{}" \; |
I think Helmet's edit suggestion is what I would do. Something along the lines of:
Code:
find . ! -type l -exec rm -f {} \; |
(back after a good night's sleep)
Wow, these are more possibilities than I even imagined. Mostly variations around -type l or cunningly using -type d, all them good ideas. Thank you VERY MUCH, guys, I'm really grateful, you helped a lot ! :) |
All times are GMT -5. The time now is 12:03 PM. |