Register
It is currently Fri Nov 28, 2014 4:51 pm

Script to delete media files


All times are UTC - 6 hours


Post new topic Reply to topic  [ 5 posts ] 
Author Message
 PostPosted: Mon Apr 27, 2009 11:44 pm   

Joined: Mon Apr 27, 2009 11:30 pm
Posts: 3
Hi Everyone,

I'm a newbie.

I usually issue this command to find and delete mp3 files in home directory.

find /home -iname '*.mp3' -exec rm -f {} \;

How can I make a bash script to find and delete all kinds of media files such as mp3, wma, mpg, etc.

Thanks a lot! :)


Top
 Profile  
 PostPosted: Tue Apr 28, 2009 1:56 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
Code:
#!/bin/bash
array=$*

for ext in $array; do
   find /home -iname '*.$ext' -exec rm -f {} \;
done


This is a really simplistic solution.
Save it as script.sh and make it executeable, chmod +x script.sh.
To use it do something like this:
./script.sh mp3 mpg wma

This will execute 3 finds that can be translated like this.
find /home -iname '*.mp3' -exec rm -f {} \;
find /home -iname '*.mpg' -exec rm -f {} \;
find /home -iname '*.wma' -exec rm -f {} \;

This happens because 'for' shifts each word seperated by a whitespace character (in this case it's a space) and sets $ext to the current itteration.
$* is just a way to print all arguments passed from the command line.
The $array variable will look like this:
Code:
array="mp3 mpg wma"


Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Wed Apr 29, 2009 1:09 am   

Joined: Mon Apr 27, 2009 11:30 pm
Posts: 3
Hi,

I made some modifications.

I created a flat text file named file.txt and I put inside that text file all the file extensions of the media files that I wanted to delete.

Here is the script:
Code:
#!/bin/bash
for ext in $(cat file.txt)
do
  find /path-to-folder -iname "*.$ext" -exec rm -f {} \;
done


What I want to add next is how to make the script ignore some specific files / folders inside the /path-to-folder folder

Thanks! :)


Top
 Profile  
 PostPosted: Wed Apr 29, 2009 1:30 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
That is abit trickier. AFAIK find doesn't support excludes right out of the box.

This is a method google proposed to use.
Code:
find . -path './media' -prune -o -path './images' -prune -o -path './backups' -prune -o -print


You can find more info at this url:
http://www.liamdelahunty.com/tips/linux ... tories.php

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Wed Apr 29, 2009 4:32 am   

Joined: Mon Apr 27, 2009 11:30 pm
Posts: 3
Hi fredrik,

Thank you for your help.

Just wanted to share the final script I used for the benefit of others who are also encountering the same problem:

Code:
#!/bin/bash
for ext in $(cat file.txt)
do
  find /path-to-folder -type d \( -name media -o -name images -o -name backups \) -prune -o -iname "*.$ext" -exec rm -f {} \;
done


Basically, the script will find and delete all the files with the same file extensions listed in the file.txt located inside the "/path-to-folder" folder excluding media, images, and backups folder.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP