LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   can we exclude single file while doing rm -rf /opt/* (https://www.linuxquestions.org/questions/linux-newbie-8/can-we-exclude-single-file-while-doing-rm-rf-opt-%2A-4175635409/)

uidname 08-01-2018 03:43 AM

can we exclude single file while doing rm -rf /opt/*
 
I want to exclude a file abc under /opt/ while doing rm -rf /opt/*

syg00 08-01-2018 03:53 AM

If you insist on running "rm -rf" on system directories you will destroy your system.

Find a better way. The "find" command would be a good start.

uidname 08-01-2018 05:33 AM

Quote:

Originally Posted by syg00 (Post 5886354)
If you insist on running "rm -rf" on system directories you will destroy your system.

Find a better way. The "find" command would be a good start.


Can I exclude a single file from /test/* while doing rm -rf on it. where /test is not a system directory.

rtmistler 08-01-2018 08:42 AM

Quote:

Originally Posted by uidname (Post 5886384)
Can I exclude a single file from /test/* while doing rm -rf on it. where /test is not a system directory.

  1. Go to /test
  2. Move the one file you wish to preserve somewhere else, suggest the mv command
  3. Make sure there's nothing remaining in /test that you wish to retain, including checking for hidden files (those beginning with . )
  4. Make backup of /test anyways
  5. Get out of the directory and perform your rm -rf command

BW-userx 08-01-2018 09:09 AM

something like this.
Code:

while read f ; do

if [[ "$f" != 'fileIwantToSave' ]]
  rm -rvf "$f"
fi

done <<< "$(find /opt -type f)"

##lots to write for one file save

like rtmistler suggested, I'd move the one file you want to save, then delete the rest.

michaelk 08-01-2018 09:43 AM

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.
Code:

find /directory_name ! -name 'file.txt' -type f -exec rm -f {} +
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..

JeremyBoden 08-01-2018 03:13 PM

Actually, if you want to delete a directory and all its sub-directories and all files including the hidden ones, a GUI file-manager works well.

It might stop you accidentally deleting the wrong directory tree...

scasey 08-01-2018 04:25 PM

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.

keefaz 08-01-2018 08:19 PM

Maybe things in /opt were installed by package manager, if yes I would rather use the package manager and uninstall things I don't want anymore

chrism01 08-02-2018 11:47 PM

@scasey : I sympathize, but when you have 1000s of files to remove .... & I do on servers occasionally

uidname 08-03-2018 05:56 AM

Quote:

Originally Posted by rtmistler (Post 5886478)
  1. Go to /test
  2. Move the one file you wish to preserve somewhere else, suggest the mv command
  3. Make sure there's nothing remaining in /test that you wish to retain, including checking for hidden files (those beginning with . )
  4. Make backup of /test anyways
  5. Get out of the directory and perform your rm -rf command

Thanks for your support. My doubt got clears....

scasey 08-03-2018 11:26 AM

Quote:

Originally Posted by chrism01 (Post 5887141)
@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...

JeremyBoden 08-03-2018 11:58 AM

1. Construct a list of all sub-directories & files that rm -rf would destroy.

2. Check it carefully.

3. delete the line(s) containing vital files etc

4. Feed your list into rm.

MadeInGermany 08-03-2018 02:24 PM

Quote:

Originally Posted by scasey (Post 5886631)
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!

JeremyBoden 08-03-2018 05:10 PM

Take a backup first. :D


All times are GMT -5. The time now is 09:30 PM.