LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-26-2007, 04:54 PM   #1
jchambers
Member
 
Registered: Aug 2007
Location: California
Distribution: Debian
Posts: 127

Rep: Reputation: 15
Keep specific file types, delete the rest ?


Hello, I bet someone has asked this before but I can't find the answer...


I am trying to keep only certain file types and delete the rest.

Code:
-rw-r--r-- 1 root   root        0 2007-11-26 06:41 file_a.wmv
-rw-r--r-- 1 root   root        0 2007-11-26 06:41 file_b.pdf
-rw-r--r-- 1 root   root        0 2007-11-26 06:41 file_c.sh
-rw-r--r-- 1 root   root        0 2007-11-26 06:41 file_d.mov
-rw-r--r-- 1 root   root        0 2007-11-26 06:42 file_e.whatever
My goal is to delete b,c,e and keep wmv & mov

rm -f /path/to/folder/*^[wmv|mov]

Is there anything like this that will work?

My other alternative is to temporarily move all the wanted files out of that folder delete all then move them back. But I would imagine there is a simpler way.
 
Old 11-26-2007, 05:19 PM   #2
rsashok
Member
 
Registered: Nov 2006
Location: USA, CA
Distribution: RedHat, Debian
Posts: 202

Rep: Reputation: 31
Code:
for file in `ls | grep -v -E 'wmv|mov'`;do echo rm -f $file;done
If this displays what you want, then remove 'echo'.
 
Old 11-26-2007, 05:25 PM   #3
jchambers
Member
 
Registered: Aug 2007
Location: California
Distribution: Debian
Posts: 127

Original Poster
Rep: Reputation: 15
Wow, that is nice an simple.
( I need to learn more about grep )

Perfect !


Thank you for the help rsashok.
 
Old 11-26-2007, 05:27 PM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
The find syntax to list the files whose names do not match *.mov or *.wmv is a little ugly, but it'll work:
Code:
find . -type f \! \( -iname \*.wmv -o -iname \*.mov \)
You can also use regexps with find, although the GNU find uses the emacs-style regexp's which require even more than normal escaping with the \ character:
Code:
find . -type f \! -iregex '.*\(wmv\|mov\)'
You can append "-exec rm {} \;" to remove them, or use xargs if you prefer. xargs will be faster if you have a very large number of files. It is probably prudent to add the -print0 / -0 options to prevent file names with spaces causing problems:
Code:
find . -type f \! -iregex '.*\(wmv\|mov\)' -print0 |xargs -0 rm

Last edited by matthewg42; 11-26-2007 at 05:38 PM.
 
Old 11-26-2007, 05:38 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
AND--just for variety:

ls|egrep -v 'f1|f2'|xargs rm -f

removes all files except f1 and f2

Note that "egrep" and "grep -E" are synonymous
 
Old 11-26-2007, 06:25 PM   #6
jchambers
Member
 
Registered: Aug 2007
Location: California
Distribution: Debian
Posts: 127

Original Poster
Rep: Reputation: 15
Bonus round... thanks for the extra tips.
I am trying those as well.

pixellany -- funny signature
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
ow do I delete all files which were created on a specific date marsguy Linux - General 9 08-21-2007 08:15 AM
Delete Windows partition, save rest tommytwo Linux - General 5 02-11-2007 10:56 AM
vsftpd disallow specific file types from being uploaded GUIPenguin Linux - General 2 04-08-2005 10:04 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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