[SOLVED] How "to rm -rf *", but excluding (leaving behind) symlinks ?
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 !
Last edited by Sabinou; 01-13-2012 at 01:48 AM.
Reason: addition of the [Solved] prefix
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.
Last edited by Dark_Helmet; 01-12-2012 at 03:32 PM.
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...
this will search for all object that are not symbolic links and will delete them recursively. Obviously the directories containing symbolic links will not be removed.
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
drwxr-xr-x 2 user user 4096 2012-01-12 15:48 another_dir
lrwxrwxrwx 1 user user 16 2012-01-12 15:48 download_link -> ../../Downloads/
drwxr-xr-x 2 user user 4096 2012-01-12 15:47 test_dir
lrwxrwxrwx 1 user user 11 2012-01-12 15:47 test_link -> ../../temp/
user@localhost$ # Display symbolic links
user@localhost$ find . -maxdepth 1 -type l
./test_link
./download_link
user@localhost$ # Display non-symbolic links
user@localhost$ find . -maxdepth 1 ! -type l
.
./test_dir
./another_dir
The problem with the non-symbolic link command is that you get the current directory reference: .
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 \)
./test_dir
./another_dir
With that, you can safely add the -exec portion:
Code:
user@localhost$ find . -maxdepth 1 \( ! -type l \) -a \( -regex "\./.*" \) -exec rm -R "{}" \;
And lastly, if that's too much to type, you can always create an alias for it. You just have to remember to execute it from the directory you want it to work on. You could modify the alias to work with an argument, but anyway...
EDIT:
I type too much...
EDIT2:
And I'm dumb... instead of "! -type l" the simplest substitute would be "-type d"
I think Helmet's edit suggestion is what I would do. Something along the lines of:
Code:
find . ! -type l -exec rm -f {} \;
You should obviously run that without the -exec rm -f to make sure it matches what you want and doesn't match what you don't want before running the whole shebang.
Last edited by suicidaleggroll; 01-12-2012 at 04:12 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.