LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   rm *.foo through recursive directories (https://www.linuxquestions.org/questions/linux-general-1/rm-%2A-foo-through-recursive-directories-548089/)

RevenantSeraph 04-22-2007 12:46 PM

rm *.foo through recursive directories
 
So I have a bunch of mp3's in my flac folders and I want to delete them, but I'm more interested in finding a way to remove them through the commandline folder-recursively rather than having to cd to each directory and type rm .*mp3 each time.

I have tried rm -r *.mp3 but this doesn't work.

Any ideas?

rocket357 04-22-2007 12:50 PM

man rm...

Mine shows rm -r and rm -R as recursive removes. Try -R if -r isn't working (for whatever reason), and try specifying the directory explicitly, i.e.:

rm -R ./*.mp3

Panagiotis_IOA 04-22-2007 12:50 PM

If you are in the directory then type:

Code:

rm -R *
or from anywhere

Code:

rm -R /this/is/my/directory/*
These commands will remove any folder and file from your directory.

EDIT: Sorry, I don't think my reply is what you need. I misunderstood what you were asking for.

cseanburns 04-22-2007 03:02 PM

You can try using the find command, but be careful. Do a test run first.

> find -name "*.mp3"

This will simply print the list of files "find" finds to standard output. If you have a lot of files to delete and you want to inspect them closely, redirect the above command into a text file for closer inspection:

> find -name "*.mp3" > output.txt

To delete the files "find" finds, run this command:

> find -name "*.mp3" -delete

If you have several subdirectories of files and you want find to delete mp3s in some subdirectories and not others, the easiest thing to do is to switch to the base directory. E.g., If you have mp3s in these areas:

/home/user/music/
/home/user/music/bandname/
/home/user/audio/
/home/user/audio/personal/

If you want to delete mp3s in the music directory and its subdirectories but not in the audio directory or its subdirectories, simply change to the music directory. Do a test run first. Be very careful about find + delete usage because you could easily wipe out what you want to keep. Make backups before if you want.

Hope that helps,
sean

Junior Hacker 04-23-2007 01:58 AM

So far, cseanburns has the best suggestion. Using rm will not just remove .mp3 but any file extension like .jpg .doc etc., not sure why but I know from experience. Find is the better command to use but the suggested command above needs refinement. Change into the parent directory.
Code:

find . -name "*.mp3" -delete
The (.) says to look in the current directory and all sub-directories.
EDIT: Where are all these "flac" folders?

cseanburns 04-23-2007 05:19 PM

Thanks Junior Hacker for catching the missing period.

Tinkster 04-24-2007 07:50 PM

The GNU version of find doesn't need the period (unlike the
one that ships with Solaris), starting from the current working
directory is the default behaviour.



Cheers,
Tink

Junior Hacker 04-24-2007 08:54 PM

Hmmm
Good to know.

RevenantSeraph 05-05-2007 04:03 AM

That is so powerful!
It took me a while to get some time to do upkeeping on my computer, but I'm so glad you showed me how to do that. It works perfectly, as outlined by cseanburns & Junior Hacker.

Ah, the glory of Linux.

Thanks again!

RevenantSeraph 05-05-2007 04:03 AM

Quote:

Originally Posted by Tinkster
The GNU version of find doesn't need the period (unlike the
one that ships with Solaris), starting from the current working
directory is the default behaviour.



Cheers,
Tink

And this is correct.


All times are GMT -5. The time now is 01:51 PM.