LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
LinkBack Search this Thread
Old 03-14-2007, 10:45 AM   #1
gothicbob
Member
 
Registered: Sep 2005
Location: Birmingham / Bath Uk
Distribution: Kubuntu 8.10, Debain 4.0
Posts: 86

Rep: Reputation: 16
find and delte commands


Ive had a look around and I'm having a little bit of trouble with the find and delte command, I dont know how to make the recurrsive (if thats the right word, I mean go into every folder and execute)

like the:

delete /s command in windows.



and also I dont know how to use the find command to find a file recurrsivly, and how to excluse certain extentions.

eg: I want to find all files in a folder + subfolders that dont end with .mp3, .wma, or .m4a

would this be:
find /Music/*.* !name-*.mp3 !name-*.wma !name-*.m4a

??

Thanks for your help in advance
 
Old 03-14-2007, 12:43 PM   #2
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian Squeeze
Posts: 5,697

Rep: Reputation: 286Reputation: 286Reputation: 286
This might work:
find /Music -r -type f -name "<filename>.*"--exclude .mp3 --exclude .wma -exec rm -f {} \;

where:
/Music is the top level directory to begin.
-r is recursive
-type f means regular file, as opposed to binary, block, etc.
-name "<filename>" is the basename of the file in quotes
exclude selected by file extension
-exec rm -f {} \; to remove the matches to the search pattern after the excluded files are de-selected.

More experienced heads will probably give you a more elegant solution.

Before trying this on your system, I'd suggest making a test directory with test files (same name, different extensions) and run the find command given above on the test directory to be certain it will work as expected.

Last edited by bigrigdriver; 03-14-2007 at 12:55 PM.
 
Old 03-14-2007, 12:51 PM   #3
gothicbob
Member
 
Registered: Sep 2005
Location: Birmingham / Bath Uk
Distribution: Kubuntu 8.10, Debain 4.0
Posts: 86

Original Poster
Rep: Reputation: 16
when i tried:
find /Music -r -type f --exclude .mp3 --exclude .wma | grep '*.*'
i got: find: invalid predicate `-r'

when i got rid of -r
i got: find: invalid predicate `--exclude'


when i tried:
find Music/ -type f | grep '*.*'

it seemed to run for a second and then finish, printing no results
 
Old 03-14-2007, 01:09 PM   #4
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian Squeeze
Posts: 5,697

Rep: Reputation: 286Reputation: 286Reputation: 286
My bad. Mixed parts of two commands. I apologize.

find /Music -type f -maxdepth 3 | grep 'filename' --exclude .mp3 --exclude .wma | -exec rm -f {} \;

maxdepth 3 (search three levels down)
You may have to enclose the excluded pattern in quotes. Try it both ways.

Make a test directory with test files (same name; different extensions) to test this on to verify that it will work.

Last edited by bigrigdriver; 03-14-2007 at 01:10 PM.
 
Old 03-14-2007, 01:12 PM   #5
gothicbob
Member
 
Registered: Sep 2005
Location: Birmingham / Bath Uk
Distribution: Kubuntu 8.10, Debain 4.0
Posts: 86

Original Poster
Rep: Reputation: 16
ok i worked it out:

find -not -name *.mp3 -a -not -name *.m4a -a -not -name *.wma

would get what I wanted, and this could also be used to delte the files by using the find commands ability to execute stuff to the files it finds.

Thanks
 
Old 03-14-2007, 01:19 PM   #6
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian Squeeze
Posts: 5,697

Rep: Reputation: 286Reputation: 286Reputation: 286
And I've learned yet another variation on the theme of 'find and delete'. Thank you.
 
Old 03-14-2007, 01:43 PM   #7
gothicbob
Member
 
Registered: Sep 2005
Location: Birmingham / Bath Uk
Distribution: Kubuntu 8.10, Debain 4.0
Posts: 86

Original Poster
Rep: Reputation: 16
I think I might have to make an alias for it just so i can type "searchanddestroy" in at command line for it :P
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how can I execute two commands on exec of a find? eantoranz Programming 3 08-03-2010 04:51 PM
commands to find the proprties of my hardware micro_xii Linux - Newbie 5 11-29-2006 08:12 PM
find & rm commands O(V)eGA_l2el) Linux - Newbie 6 08-26-2006 08:16 AM
What is the command to delte EVERYTHING when you are logged into root? Ricapar Mandriva 5 05-02-2004 06:53 PM
Cannot find reliable information on shell commands Rakin Linux - Newbie 7 02-21-2003 10:55 AM


All times are GMT -5. The time now is 12:28 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration