Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

rm all except zip files

1 view
Skip to first unread message

Eric Gorely

unread,
Mar 4, 1999, 3:00:00 AM3/4/99
to
Is there a way with a simple rm command to delete all files in the current
directory except those that end with zip?

I have tried the following:
rm *.!(zip) #will not get files with no extension or with multiple dots in
them
rm *!(zip) #deletes all files, including zip files
rm *.[!zip] #appears to not get files ending with .z, more than one dot,
and no extensions
rm *[!zip] #same as above, except will get files with no extension
rm *![zip] #rm: *![zip]: No such file or directory

Am I close? Is there even a way? I think I understand the behavior of all of
the above except for the second one. Can someone explain? Thanks for your
time. System info:
uname -a
OSF1 machinename V4.0 878 alpha
(using ksh)

Barry Margolin

unread,
Mar 4, 1999, 3:00:00 AM3/4/99
to
In article <P%DD2.321$oe3.36728@PM01NEWS>,

Eric Gorely <Eric....@mci.com> wrote:
>Is there a way with a simple rm command to delete all files in the current
>directory except those that end with zip?

rm `find * -type d -prune -o -type f ! -name '*.zip'`

If you don't have any subdirectories you can leave out the '-type d -prune
-o' part of the command.

--
Barry Margolin, bar...@bbnplanet.com
GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

Yaning Wang

unread,
Mar 4, 1999, 3:00:00 AM3/4/99
to

Eric Gorely wrote in message ...

>Is there a way with a simple rm command to delete all files in the current
>directory except those that end with zip?
>

Since you want to do this in CURRENT directory, you can simply use grep,
something like:

rm `ls | grep -v '.zip'`
(or rm `ls | grep -v '.zip$'`)

Dale Hagglund

unread,
Mar 4, 1999, 3:00:00 AM3/4/99
to
"Eric Gorely" <Eric....@mci.com> writes:

> Is there a way with a simple rm command to delete all files in the current
> directory except those that end with zip?

ls | grep -v '\.zip$' | xargs rm

Dale.

John DuBois

unread,
Mar 5, 1999, 3:00:00 AM3/5/99
to
In article <P%DD2.321$oe3.36728@PM01NEWS>,
Eric Gorely <Eric....@mci.com> wrote:
>Is there a way with a simple rm command to delete all files in the current
>directory except those that end with zip?
>
>I have tried the following:
>rm *.!(zip) #will not get files with no extension or with multiple dots in them
>rm *!(zip) #deletes all files, including zip files
>
>Am I close?

Yes.

rm !(*.zip)

or if you want to include dotfiles,

rm !(*.zip) .!(*.zip)


John
--
John DuBois spc...@armory.com. KC6QKZ http://www.armory.com./~spcecdt/

Greg Hunter

unread,
Mar 7, 1999, 3:00:00 AM3/7/99
to
Something like

/bin/rm -f *.[^zip]*
 

Eric Gorely wrote:

Is there a way with a simple rm command to delete all files in the current
directory except those that end with zip?

I have tried the following:
rm *.!(zip)   #will not get files with no extension or with multiple dots in
them
rm *!(zip)    #deletes all files, including zip files

rm *.[!zip]   #appears to not get files ending with .z, more than one dot,
and no extensions

rm *[!zip]    #same as above, except will get files with no extension

rm *![zip]    #rm: *![zip]: No such file or directory

Am I close? Is there even a way? I think I understand the behavior of all of
the above except for the second one. Can someone explain? Thanks for your
time. System info:
uname -a
OSF1 machinename V4.0 878 alpha
(using ksh)

 
-- 
_____________________________________________________________________________
|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|
|_|___|___|___|___|___|___|Greg Hunter            |___|___|___|___|___|___|_|
|_|___|___|___|___|___|___|jghu...@ix.netcom.com |___|___|___|___|___|___|_|
|___|___|___|___|___|___|_|_______________________|_|___|___|___|___|___|___|
|_|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|_|
|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|
 

Al Aab

unread,
Mar 8, 1999, 3:00:00 AM3/8/99
to
sorry
i have no unix
but try

ls *.[!z][!i][!p]

if it works
replace ls with rm


--
=-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
al aab, seders moderator sed u soon
it is not zat we do not see the s o l u t i o n
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+

Charles Demas

unread,
Mar 8, 1999, 3:00:00 AM3/8/99
to Al Aab
In article <F8A1vJ.GI4...@torfree.net>,

Al Aab <af...@torfree.net> wrote:
>sorry
>i have no unix
>but try
>
>ls *.[!z][!i][!p]
>
>if it works
>replace ls with rm

I have not tested the above, but I believe that this will not work
if there is no period or if there is a zero, one, two, or a 4+
character extension.

These filenames wouldn't get found:
myfile
myfile.
myfile.z
myfile.zi
myfile.zip1
myfile.more2
.
.
.
etc.

Others have posted good solutions using find.


Chuck Demas
Needham, Mass.
[posted and emailed]

--
Eat Healthy | _ _ | Nothing would be done at all,
Stay Fit | @ @ | If a man waited to do it so well,
Die Anyway | v | That no one could find fault with it.
de...@tiac.net | \___/ | http://www.tiac.net/users/demas

Barry Margolin

unread,
Mar 8, 1999, 3:00:00 AM3/8/99
to
In article <7c0pr5$i...@news-central.tiac.net>,

Charles Demas <de...@sunspot.tiac.net> wrote:
>In article <F8A1vJ.GI4...@torfree.net>,
>Al Aab <af...@torfree.net> wrote:
>>sorry
>>i have no unix
>>but try
>>
>>ls *.[!z][!i][!p]
>>
>>if it works
>>replace ls with rm
>
>I have not tested the above, but I believe that this will not work
>if there is no period or if there is a zero, one, two, or a 4+
>character extension.
>
>These filenames wouldn't get found:
> myfile
> myfile.
> myfile.z
> myfile.zi
> myfile.zip1
> myfile.more2

It will also leave things like *.zig, *.zap, and *.pip.

sri...@eng.ua.edu

unread,
Mar 9, 1999, 3:00:00 AM3/9/99
to
try this easy ksh script

#!/bin/ksh

for each in `ls -1 | grep -v "zip file extension eg .Z"`
do echo $each
done
-----

if the above script lists all the file but your zipped files, replace the echo
with \rm -f.

Sriram

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own

John DuBois

unread,
Mar 9, 1999, 3:00:00 AM3/9/99
to
Again:

s_di...@my-dejanews.com

unread,
Mar 9, 1999, 3:00:00 AM3/9/99
to
The answer to this was posted only a few days/weeks ago.

ls -l !(*zip)

or

rm !(*zip)

Regards,
Steve.


In article <S3TE2.83$p4.6536@burlma1-snr2>

-----------== Posted via Deja News, The Discussion Network ==----------

ma...@my-dejanews.com

unread,
Mar 9, 1999, 3:00:00 AM3/9/99
to
Realize this will might recurse through directories .

sriram


In article <7c2j5j$3c$1...@remarQ.com>,

-----------== Posted via Deja News, The Discussion Network ==----------

John DuBois

unread,
Mar 10, 1999, 3:00:00 AM3/10/99
to
In article <7c40bl$cd$1...@nnrp1.dejanews.com>, <ma...@my-dejanews.com> wrote:
>Realize this will might recurse through directories .

Hmm? No, this won't recurse through any directories.

John

sri...@eng.ua.edu

unread,
Mar 11, 1999, 3:00:00 AM3/11/99
to
a ls command of the same syntax recursed thorugh my dir.
and thanks for the help on the ksh prompt stuff.

Sriram


In article <7c5g2o$5u2$1...@remarQ.com>,


spc...@deeptht.armory.com. (John DuBois) wrote:
> In article <7c40bl$cd$1...@nnrp1.dejanews.com>, <ma...@my-dejanews.com> wrote:
> >Realize this will might recurse through directories .
>
> Hmm? No, this won't recurse through any directories.
>
> John
>
> >sriram
> >
> >
> >In article <7c2j5j$3c$1...@remarQ.com>,
> > spc...@deeptht.armory.com. (John DuBois) wrote:
> >> Again:
> >>
> >> rm !(*.zip)
> >>
> >> or if you want to include dotfiles,
> >>
> >> rm !(*.zip) .!(*.zip)
> >>
> >> John
> --
> John DuBois spc...@armory.com. KC6QKZ
http://www.armory.com./~spcecdt/
>

-----------== Posted via Deja News, The Discussion Network ==----------

John DuBois

unread,
Mar 11, 1999, 3:00:00 AM3/11/99
to
In article <7c71im$n0s$1...@nnrp1.dejanews.com>, <sri...@eng.ua.edu> wrote:
>a ls command of the same syntax recursed thorugh my dir.

Yes, you need to give ls the '-d' option to make it act on directories the same
as it does on files.

>and thanks for the help on the ksh prompt stuff.

You're welcome.

John

ray

unread,
Mar 15, 1999, 3:00:00 AM3/15/99
to
I would use this...
#### granted you can do as you wish
## move files as you see them... Incase you change your mind....
## then the directories.
# this is for a idea only.
for name in `find ./ -name "*" -print |grep -v '\.zip'` ### find everything excpt
the .zip files and
do ##
assign it to the variable name.
echo "about to remove:"
ls -ld $name
echo "press y to remove"
read ans
if [[ ${ans} = y ]]
then
if [[ -f $name ]]
then
rm $name
elif [[ -d $name ]]
then
echo "Will remove dir later."
fi
done

sri...@eng.ua.edu wrote:

> a ls command of the same syntax recursed thorugh my dir.

> and thanks for the help on the ksh prompt stuff.
>

Bob McGowan

unread,
Mar 15, 1999, 3:00:00 AM3/15/99
to
ray wrote:
>
> I would use this...
> #### granted you can do as you wish
> ## move files as you see them... Incase you change your mind....
> ## then the directories.
> # this is for a idea only.
> for name in `find ./ -name "*" -print |grep -v '\.zip'` ### find everything excpt
> the .zip files and
> do ##
> assign it to the variable name.
> echo "about to remove:"
> ls -ld $name
> echo "press y to remove"
> read ans
> if [[ ${ans} = y ]]
> then
> if [[ -f $name ]]
> then
> rm $name
> elif [[ -d $name ]]
> then
> echo "Will remove dir later."
> fi
> done
>
---removed bits and pieces of previous posts

I got into this late and didn't see the original posting, but I would
like to respond to the above code specifically, without reference to the
original stuff other than to say, I assume we want to keep *zip files
and delete ALL other regular files, from the current directory down. We
will not remove any directories, even if they are empty.

First, simplify: rm has an option, -i that makes it interactive. It
will ask, for each file it is given, whether you want to remove it. An
answer beginning with y is required, anything else skips.

Second, understand find: -name '*' will limit to all names not
beginning with a dot. But we could also limit the find to reporting
about regular files only with '-type f', since we want to skip
directories.

There are possibly a lot of files to find doing this, so 'xargs' is
added to the needed tools:

find . -name '*' -type f -print | grep '\.zip$' | xargs rm -i

Coupla other notes: directory names dot and dot_slash are the same so
go with the simpler; the grep pattern without the dollar sign would save
files like 'the.zipper'.

Cheers,

--
Bob McGowan
bob dot mcgowan at usa dot net

Martin Ramsch

unread,
Mar 16, 1999, 3:00:00 AM3/16/99
to
On Mon, 15 Mar 1999 22:07:06 -0800, Bob McGowan <rmcg...@jps.net> wrote:
> find . -name '*' -type f -print | grep '\.zip$' | xargs rm -i
>
> Coupla other notes: directory names dot and dot_slash are the same so
> go with the simpler; the grep pattern without the dollar sign would save
> files like 'the.zipper'.

Even better, because the '-name' parameter takes file patterns we can
drop the grep entirely:

find . -name '*.zip' -type f -print | xargs rm -i

Or interactively delete everything except "*.zip" files:

find . \! -name '*.zip' -type f -print | xargs rm -i

Because this includes ".*" files now, to avoid these we could add:

find . -name '*' \! -name '*.zip' -type f -print | xargs rm -i
or
find . \! -name '.*' \! -name '*.zip' -type f -print | xargs rm -i

(The first works, because "*" by default skips ".*" files, but
for the sake of clarity I'd prefer the second explicit version.)

Please note, that find's '-name' parameter takes file patterns
in contrast to grep's regular expressions!

Regards,
Martin
--
Martin Ramsch <m.ra...@computer.org> <URL: http://home.pages.de/~ramsch/ >
PGP KeyID=0xE8EF4F75 FiPr=52 44 5E F3 B0 B1 38 26 E4 EC 80 58 7B 31 3A D7

Geoff Clare

unread,
Mar 24, 1999, 3:00:00 AM3/24/99
to
m.ra...@computer.org (Martin Ramsch) writes:

>On Mon, 15 Mar 1999 22:07:06 -0800, Bob McGowan <rmcg...@jps.net> wrote:
>> find . -name '*' -type f -print | grep '\.zip$' | xargs rm -i

> find . -name '*.zip' -type f -print | xargs rm -i

Has anyone bothered to try one of these suggestions that end with
"... | xargs rm -i"? They don't work! When rm prompts for confirmation
it reads the response from stdin - which is the pipe from "find". If
xargs has already read everything from the pipe, rm will get end-of-file.
If not, rm will read part of the list of filenames as if it was a y/n
response.

>Or interactively delete everything except "*.zip" files:

> find . \! -name '*.zip' -type f -print | xargs rm -i

>Because this includes ".*" files now, to avoid these we could add:

> find . -name '*' \! -name '*.zip' -type f -print | xargs rm -i
>or
> find . \! -name '.*' \! -name '*.zip' -type f -print | xargs rm -i

> (The first works, because "*" by default skips ".*" files,

Well, it does on some systems, but only if they have a broken version
of find. The POSIX.2 standard says that -name "*" matches files
beginning with dot. This is also the historical behaviour, but
unfortunately some vendors misinterpreted the POSIX.2 spec.

--
Geoff Clare g...@unisoft.com
UniSoft Limited, London, England. g...@root.co.uk

James Stevenson

unread,
Mar 25, 1999, 3:00:00 AM3/25/99
to
Hi

would this not be easyer?

rm -i `find .|grep -v .zip`


--
Check Out: http://www.users.zetnet.co.uk/james/
E-Mail: mis...@zetnet.co.uk

Martin Ramsch

unread,
Mar 25, 1999, 3:00:00 AM3/25/99
to
On Wed, 24 Mar 1999 20:35:21 GMT,
Geoff Clare <g...@root.co.uk> wrote:
> m.ra...@computer.org (Martin Ramsch) writes:
>
> >On Mon, 15 Mar 1999 22:07:06 -0800, Bob McGowan <rmcg...@jps.net> wrote:
> >> find . -name '*' -type f -print | grep '\.zip$' | xargs rm -i
>
> > find . -name '*.zip' -type f -print | xargs rm -i
>
> Has anyone bothered to try one of these suggestions that end with
> "... | xargs rm -i"? They don't work! When rm prompts for confirmation
> it reads the response from stdin - which is the pipe from "find".

Pff, I should have thought of that - and you're right: in this case I
didn't test the commands myself, but only did cut & paste, as my focus
only was on showing some of the possibilities of "find" ...

Sorry for the confusion I might have caused!

Kind regards,

David Lodge

unread,
Mar 25, 1999, 3:00:00 AM3/25/99
to
On Wed, 24 Mar 1999 20:35:21 GMT, g...@root.co.uk (Geoff Clare) wrote:
>>Because this includes ".*" files now, to avoid these we could add:
>> find . -name '*' \! -name '*.zip' -type f -print | xargs rm -i
>>or
>> find . \! -name '.*' \! -name '*.zip' -type f -print | xargs rm -i
>
>> (The first works, because "*" by default skips ".*" files,
>Well, it does on some systems, but only if they have a broken version
>of find. The POSIX.2 standard says that -name "*" matches files
>beginning with dot. This is also the historical behaviour, but
>unfortunately some vendors misinterpreted the POSIX.2 spec.

Missed the start of this thread; but there is an easier way to delete
all but a certain name of file:

$ ksh
$ rm -i !(*.zip)

dave
--
David 'I just get these headaches' Lodge, da...@muspellheim.demon.co.uk
"Dear God above (if you exist), Hope you see the funny side to this,
Now don't get cross - don't bite your nails,
Oh, Son of Man your mission's failed."
- Skyclad "The Sinful Ensemble"

0 new messages