[SOLVED] can we exclude single file while doing rm -rf /opt/*
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.
If using a bash or a shell that supports extended globing you can use the following to exclude files. I don't know if it will work the same with -rf options and you might get an Argument list too long error.
Code:
shopt -s extglob
rm !(file.txt)
You might want to disable globing
shopt -u extglob
The find command supports not or ! and a loop is not required.
As always make sure you have a good backup just in case. I also agree with the others and for just a single file would prefer to manually move it versus running the posted commands..
After accidentally removing all of the files in a user's home directory, I have taken to never using the -f option to rm any more. It's much less work to type 'y' and enter a few times than to recover a couple GB of files from backup.
That's just me, tho. YMMV.
Short answer to the OP's question is No, you cannot execute an rm -rf /somedir/* and exclude a file within that tree.
Others have already provided solutions.
@scasey : I sympathize, but when you have 1000s of files to remove .... & I do on servers occasionally
I get that...I'm not saying I never use the -f flag, but I now prefer to build a find command in that case, test it with print, then add a rm when I'm sure all is as I want it.
Still, if I want to just prune a defunct user's home to purge the 2GB of mail files he left behind, I'll definitely use rm -f...
After accidentally removing all of the files in a user's home directory, I have taken to never using the -f option to rm any more. It's much less work to type 'y' and enter a few times than to recover a couple GB of files from backup.
That's just me, tho. YMMV.
Short answer to the OP's question is No, you cannot execute an rm -rf /somedir/* and exclude a file within that tree.
Others have already provided solutions.
rm -f will not report errors. But in interactive use you *want* to see errors if they occur.
Best is rm. If aliased, escape with \rm.
And remember the echo command. First replace or prefix the \rm with echo!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.