![]() |
|
|||||||
| Notices |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
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 |
|
|
|
#2 |
|
Guest
Posts: n/a
|
Re: Delete Files Except Certain Ones
"Will" <.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. |
|
![]() |
| 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 | 3 | 26-09-2009 07:26 PM |
| Files won't delete | Bobby28 | Windows XP | 1 | 11-09-2007 01:29 PM |
| Can't delete over 200,000 files | Bob S. | Windows XP | 13 | 07-09-2007 04:29 AM |
| Cant delete files | Lilllis | Windows XP | 3 | 16-08-2007 02:16 PM |
| Can't delete two files...not sure why | genericaudioperson@hotmail.com | Windows XP | 4 | 16-08-2007 12:41 PM |
< Windows Help - MS Office Help - Hardware Support >
| New To Site? | Need Help? |