TechTalkz.com Logo Ask the Experts!

Go Back   TechTalkz.com Technology & Computer Troubleshooting Forums > Tech Support Archives > Linux & Opensource > Unix

Delete Files Except Certain Ones

Unix

 
 
Thread Tools Display Modes
Unread 15-09-2007, 05:35 AM   #1
Will
Guest
 
Posts: n/a
Delete Files Except Certain Ones

Hi friends,

Suppose I have the following files in a directory:

abc
xyz.c
xyz.h
.......
totally 1000 files

1. How could I remove all files except those with .c at the end?

2. How about only except xyz.c and xyz.h??
(I don't want to move them to somewhere and put them back after
deleting all other files.)

Any smart command regarding rm ??

Thanks for your guidance in advance.

Will

 
Unread 15-09-2007, 05:35 AM   #2
Todd H.
Guest
 
Posts: n/a
Re: Delete Files Except Certain Ones

"Will" <chickenwing2010***********.hk> writes:

> Hi friends,
>
> Suppose I have the following files in a directory:
>
> abc
> xyz.c
> xyz.h
> ......
> totally 1000 files
>
> 1. How could I remove all files except those with .c at the end?
>
> 2. How about only except xyz.c and xyz.h??
> (I don't want to move them to somewhere and put them back after
> deleting all other files.)
>
> Any smart command regarding rm ??
>
> Thanks for your guidance in advance.


As in most things in unix, there is nearly nothing a properly crafted
application of the "find" command won't do.

Have a look at this:

$ find . \( \! -name "*\.c" \! -name "*\.h" -type f \) -print

Now, if that looks like the stuff you wanna kill, then replace print
with print0 to get it all on one neat line.

$ find . \( \! -name "*\.c" \! -name "*\.h" -type f \) -print0

and verify that these ARE FILES YOU NEVER WANT TO SEE AGAIN!

What that's doing is "i wana find files from teh current directory . ,
and I'm gonna built up an expression (it's not ending in .c and it's
not ending in .h and it's a file (f) rather than a directory or other
stuff). The and is implicit. And whatever you find that meets all my
picky criteria, print it out." The next command just says "print it
all out on one line. The backslashes are needed in most cases to
keep the stupid shell from interpreting the characters and sends them
on to find unmolested.

Next test it with xargs and a benign command, like friendly ole echo:

$ find . \( \! -name "*\.c" \! -name "*\.h" -type f \) -print0 | xargs -0 echo

Now and if that still looks like a list of stuff you don't wanna see
ever again, replace echo with /bin/rm -f:

$ find . \( \! -name "*\.c" \! -name "*\.h" -type f \) -print0 | xargs -0 /bin/rm -f


Use at your own risk. Understand it. You can do bad things in a
hurry with rm and find together, but you can also do wonderful things
such as ... exactly what you want.

Good luck!

Best Regards,
--
Todd H.
http://www.toddh.net/
 
 

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete Locked files in Windows XP - Cannot delete file: Access is denied pixelpainter TIPS 'n' TRICKS 27 13-07-2012 12:25 AM
Files won't delete Bobby28 Windows XP 1 11-09-2007 06:59 AM
Can't delete over 200,000 files Bob S. Windows XP 13 06-09-2007 09:59 PM
Cant delete files Lilllis Windows XP 3 16-08-2007 07:46 AM
Can't delete two files...not sure why genericaudioperson@hotmail.com Windows XP 4 16-08-2007 06:11 AM


< Home - Windows Help - MS Office Help - Hardware Support >


New To Site? Need Help?

All times are GMT. The time now is 02:23 AM.


vBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO
Copyright © 2005-2013, TechTalkz.com. All Rights Reserved - Privacy Policy
Valid XHTML 1.0 Transitional