LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Red Hat
User Name
Password
Red Hat This forum is for the discussion of Red Hat Linux.

Notices

Reply
 
Search this Thread
Old 02-22-2008, 12:17 PM   #1
jimmyjiang
Member
 
Registered: Jun 2006
Posts: 132

Rep: Reputation: 15
Does "rm" command has a option "--exclude"?


hi,
Does "rm" command has a option "--exclude"? I need to delete a butch of files exclude some files with .txt extension.
thanks!
jimmy
 
Old 02-22-2008, 12:20 PM   #2
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
there's no --exclude option (see man page), though rm takes a list of arguments, so you can so something like:

rm `ls | grep -v 'txt$'`

-or-

rm `find . -type f | grep -v 'txt$'`

.. if you want to do it recursively.

... as with anything that deletes files permanently, use with caution.
 
Old 02-22-2008, 12:41 PM   #3
jimmyjiang
Member
 
Registered: Jun 2006
Posts: 132

Original Poster
Rep: Reputation: 15
great, thanks!
 
Old 02-22-2008, 01:09 PM   #4
b0uncer
Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
Another way of selecting files is using find with exec rm, something like
Code:
find /home/jimyjiang -name "IMG_*.JPG" -exec rm {} +
or close to that (refer to man page of find, if that doesn't work). Of course you can use any parameters find supports, to create the list of files to delete. Before actually removing, it's not a bad idea to run 'ls' instead of 'rm', for example, to have find print out the list it's going to work on, so you know what you're doing. Find is a nice tool for things like that, in addition to grep, xargs and such.
 
Old 02-22-2008, 01:52 PM   #5
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Yet another way, if your shell is bash and extglob is enabled (I'm sure other shells have things like it and more) is to do something like

Code:
rm PATH/!(*.txt)
I'm not very comfortable with that and it will try and fail to remove subdirectories, if any, but it's a possibility.

On the find, to follow along with b0uncer and to get closer to what you were asking, you could do

Code:
find . -type f ! -name '*.txt' -execdir rm {} +
I'd leave in BrianK's '-type f' to avoid just that subdir problem and find can find negated expressions. (-execdir is just a bit of paranoia.) If recursing subdirs is a problem, there's '-maxdepth 1'. (A lot of these are GNUisms.)

Doesn't much matter, as you have your problem solved but it might be useful to others with similar issues.
 
Old 02-22-2008, 01:59 PM   #6
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
Just wanted to add that I never knew you could throw a 'not' into the find command. Cool!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 6 03-21-2013 10:35 AM
"latest threads" - exclude option ??? syg00 LQ Suggestions & Feedback 2 01-01-2008 08:11 PM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 10:18 PM
LXer: Displaying "MyComputer", "Trash", "Network Servers" Icons On A GNOME Desktop LXer Syndicated Linux News 0 04-02-2007 08:31 AM
anging "Protocol" option to "IMPS/2" in XF86Config-4 causes problems zstingx Linux - General 2 10-27-2003 09:47 AM


All times are GMT -5. The time now is 01:22 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration