LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 08-01-2018, 03:43 AM   #1
uidname
LQ Newbie
 
Registered: Jul 2018
Posts: 8

Rep: Reputation: Disabled
can we exclude single file while doing rm -rf /opt/*


I want to exclude a file abc under /opt/ while doing rm -rf /opt/*
 
Old 08-01-2018, 03:53 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
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.
 
2 members found this post helpful.
Old 08-01-2018, 05:33 AM   #3
uidname
LQ Newbie
 
Registered: Jul 2018
Posts: 8

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by syg00 View Post
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.
 
Old 08-01-2018, 08:42 AM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by uidname View Post
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
 
1 members found this post helpful.
Old 08-01-2018, 09:09 AM   #5
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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.

Last edited by BW-userx; 08-01-2018 at 09:10 AM.
 
Old 08-01-2018, 09:43 AM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
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..

Last edited by michaelk; 08-01-2018 at 09:44 AM.
 
Old 08-01-2018, 03:13 PM   #7
JeremyBoden
Senior Member
 
Registered: Nov 2011
Location: London, UK
Distribution: Debian
Posts: 1,947

Rep: Reputation: 511Reputation: 511Reputation: 511Reputation: 511Reputation: 511Reputation: 511
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...
 
Old 08-01-2018, 04:25 PM   #8
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
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.
 
Old 08-01-2018, 08:19 PM   #9
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
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
 
Old 08-02-2018, 11:47 PM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
@scasey : I sympathize, but when you have 1000s of files to remove .... & I do on servers occasionally
 
Old 08-03-2018, 05:56 AM   #11
uidname
LQ Newbie
 
Registered: Jul 2018
Posts: 8

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by rtmistler View Post
  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....
 
Old 08-03-2018, 11:26 AM   #12
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Quote:
Originally Posted by chrism01 View Post
@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...
 
Old 08-03-2018, 11:58 AM   #13
JeremyBoden
Senior Member
 
Registered: Nov 2011
Location: London, UK
Distribution: Debian
Posts: 1,947

Rep: Reputation: 511Reputation: 511Reputation: 511Reputation: 511Reputation: 511Reputation: 511
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.
 
Old 08-03-2018, 02:24 PM   #14
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,791

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Quote:
Originally Posted by scasey View Post
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!
 
Old 08-03-2018, 05:10 PM   #15
JeremyBoden
Senior Member
 
Registered: Nov 2011
Location: London, UK
Distribution: Debian
Posts: 1,947

Rep: Reputation: 511Reputation: 511Reputation: 511Reputation: 511Reputation: 511Reputation: 511
Take a backup first.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] rsync issues with --include-from=FILE and --exclude-from=FILE jgould Linux - Server 8 04-04-2018 12:44 PM
Exclude Single IP from iptables rule networkprosource Linux - Networking 2 03-23-2017 04:18 PM
Using Find with an exclude/exclude file metallica1973 Linux - General 8 11-06-2011 09:39 PM
Extracting archive file to /opt? DaftDave Programming 14 04-01-2008 06:46 AM
Rsync - Exclude single file problem LinuxLuva Linux - Software 3 11-08-2007 09:34 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration