LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-23-2005, 08:54 AM   #1
Melsync
Member
 
Registered: Sep 2005
Posts: 75

Rep: Reputation: 15
remove files excluding those with specified extensions


I want to remove almost all files in a directory. It contains a number of them with many different extensions. I need to leave in the directory those with the extensions .xml and .mpeg, for instance.
I don't see that rm has an --exclude option.
I haven't tried a combination like
$ ls -l | grep -v ".xml" | grep -v ".mpeg"
and then remove the output of the above,
but there must be a shorter/elegant way of doing it.
Thanks.

Last edited by Melsync; 09-26-2005 at 06:19 PM.
 
Old 09-23-2005, 09:16 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,830
Blog Entries: 15

Rep: Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668
There is:

First you wouldn't want "ls -l" because it gives you more than the file name.

You could do your ls and pipe it to "xargs rm" (of course leaving out the -l as noted above). xargs says to execute the command on each item piped into it.

However more elegantly:

ls !(*.mpeg|*.xml) will do a list of all file other than the ones specified. You could pipe this to xargs rm as mentioned above but since rm will use the same syntax you can simply type:

rm !(*.mpeg|*.xml)


CAUTION: Do the ls first just to make sure the list it gives you is the list you want to remove. The dot (.) in many commands is seen as "any string of characters". I "think" it is litteral in ls and rm but wouldn't swear to it without testing.
 
Old 09-23-2005, 10:24 AM   #3
sirclif
Member
 
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192

Rep: Reputation: 30
what shell will that work in?

it fails in my bash, csh and zsh.

here is the unelegant solution you where talking about.
[CODE]
$ ls | grep -v -e '.*\.mpeg|.*\..xml' | xargs rm
[\CODE]
-v inverts the matching, so non-matching lines are printed and you escape the period in the extension, otherwise files named 'somenamempeg' would match because . will match anything.

another, but possibly much less elegant, technique is to use find:
[CODE]
$ find ./ -type f \! -name '*.mpeg' \! -name '*.xml' -maxdepth 1 -exec rm {} \;
[\CODE]

somebody more familiar with find may be able to shorten this a bit, but it works. if you want an explanation of all the options as an academic exercise, just ask.
 
Old 09-23-2005, 10:25 AM   #4
sirclif
Member
 
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192

Rep: Reputation: 30
oh yea, as jlighter suggested, you should run these without the rm part first to make sure its listing all the files your expecting to remove.
 
Old 09-23-2005, 11:26 AM   #5
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Don't forget the simple solutions either:
Code:
$ mv *.xml *.mpeg /tmp/save
$ rm *
$ mv /tmp/save/* ./
I've got nothing against "elegant" commands, it's just you might be able to get your work done with simple approaches than researching a clever shell syntax. Do whatever makes your life simple...

Last edited by Dark_Helmet; 09-23-2005 at 11:31 AM.
 
Old 09-23-2005, 02:05 PM   #6
sirclif
Member
 
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192

Rep: Reputation: 30
haha, yes. thats probably the method i would choose. but there is nothing wrong with learning the ins and outs of your shell.
 
Old 09-26-2005, 05:46 PM   #7
Melsync
Member
 
Registered: Sep 2005
Posts: 75

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by jlightner


rm !(*.mpeg|*.xml)

Hi, when I tried this I got

bash: !: event not found

Why is that?

Regarding
$ ls | grep -v -e '.*\.mpeg|.*\..xml' | xargs rm
maybe it is the operator | or the quotes that don't work,
but
$ ls | grep -v -e ".*\.mpeg" | grep -v -e ".\*.head" | xargs rm
did all right.

Moving the files to /tmp worked as well but only if I created
the directory /tmp/save before, otherwise it would say that
$ mv *.xml *.mpeg /tmp/save
mv: when moving multiple files, last argument must be a directory.

And thanks a lot for the answers.

Last edited by Melsync; 09-26-2005 at 06:07 PM.
 
Old 09-29-2005, 09:54 AM   #8
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,830
Blog Entries: 15

Rep: Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668
D'oh! Guess I should have tested bash. I've been doing Unix for so long that I always install ksh on Linux because bash annoys me.

The commands I gave work for ksh (Korn Shell) but on testing Sircliff is right - they don't work for bash which is the default shell used in Linux.
 
Old 09-29-2005, 01:39 PM   #9
sirclif
Member
 
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192

Rep: Reputation: 30
thats an interesting capability of the Korn Shell, much simpler and shorter. I'll have to keep that in mind. I wonder, do you know if Korn supports vi style command line editing? I imagine it does. I find that to be the most usefull feature of bash for me.
 
Old 09-30-2005, 08:02 AM   #10
Melsync
Member
 
Registered: Sep 2005
Posts: 75

Original Poster
Rep: Reputation: 15
OK,
$ mv {*.xml,*.mpeg} /tmp/save
// instead of $ mv *.xml *.mpeg /tmp/save
$ rm *
$ mv /tmp/save/* ./
does the trick.
 
Old 09-30-2005, 08:25 AM   #11
Thakowbbery
Member
 
Registered: Mar 2005
Posts: 138

Rep: Reputation: 17
rm `ls | grep -v xml | grep -v mpeg`

that sould do it
 
Old 09-30-2005, 12:05 PM   #12
Melsync
Member
 
Registered: Sep 2005
Posts: 75

Original Poster
Rep: Reputation: 15
grep power

And it does

rm `ls | grep -v "xml\|mpeg"`

as well!
 
Old 10-03-2005, 12:55 PM   #13
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,830
Blog Entries: 15

Rep: Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668
Quote:
Originally posted by sirclif
thats an interesting capability of the Korn Shell, much simpler and shorter. I'll have to keep that in mind. I wonder, do you know if Korn supports vi style command line editing? I imagine it does. I find that to be the most usefull feature of bash for me.
Yes it does. Or you can use emacs or another editor if you prefer. You can set which one to use by either setting your EDITOR=vi (or =emacs etc...) or by invoking ksh with "ksh -o vi".
By the way ksh lets you set variables by saying "export VARIABLE=value" instead of having to do the "VARIABLE=value;export VARIABLE" thing.

Another thing I like about ksh is you can use vi's search (/) to seach back through shell history instead of having to step back through (though you can do the stepping just by hitting esc-k then doing k for each step). It also allows command line completion like bash.

Since I've never really taken the time to discover all the intricacies of bash I'm sure its a good shell. Just never found the need to go to it since most commercial Unix flavors have ksh (or posix shell which incorporates most of its features) by default and don't have bash though there are of course ported version for them.
 
Old 10-03-2005, 07:50 PM   #14
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760
Blog Entries: 4

Rep: Reputation: 78
Quote:
By the way ksh lets you set variables by saying "export VARIABLE=value" instead of having to do the "VARIABLE=value;export VARIABLE" thing.
So does bash

Quote:
Another thing I like about ksh is you can use vi's search (/) to seach back through shell history instead of having to step back through
So does bash. Try [ctrl] + [r]

Not starting a flamewar, just letting people know that bash has this functionality too.
Besides, everyone knows that real hackers use zsh
 
Old 10-04-2005, 11:22 AM   #15
sirclif
Member
 
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192

Rep: Reputation: 30
haha, perhaps real hackers use xterm...

thanks for the ctr-r tip, rather handy.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Complex cp : only *.jpg files while excluding DIRs LancerNZ Linux - General 3 02-16-2005 03:12 AM
files with extensions ;type=i santoshraju Linux - General 2 12-11-2004 10:31 AM
Excluding directories from zip files jonathanztaub Linux - General 1 05-17-2004 02:17 AM
file extensions ? types ? executable files ?? abbasakhtar Linux - Newbie 13 10-06-2003 05:12 PM
tar and excluding files murshed Linux - Newbie 7 03-15-2003 02:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:05 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