remove files excluding those with specified extensions
Linux - GeneralThis 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
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.
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.
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.
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.
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.