Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Create a script to delete certain files inside a folder

Hi guys,


I was wondering if anyone could help me...


My objective is as follows; sometimes when i download stuff off the internet, movies/tv shows they com bundled with NFO's, JPEG's, TXT's and so on.... these are all downloaded to a dedicated followed called 'TVDOWNLOADS' .... I'd like to create a script that when I launch it would delete all files inside that folder and leave me with just the .mp4 file?


IF anyone can help it would be much appreciated!


Thanks.

Posted on Feb 7, 2016 9:31 AM

Reply
7 replies

Feb 7, 2016 11:23 AM in response to peds1988

The following Bash script hardcodes your TVDOWNLOADS folder as an assumption that it is in your home directory. If it is not, modify the path accordingly. It changes directory into the TVDOWNLOADS folder, and removes everything but .mp4 files. I set it up to interactively (-i) prompt you to delete the non-mp4 files. I tested this script on OS X 10.11.3 in the default Bash shell.


#!/bin/bash

#

TVD="$HOME/TVDOWNLOADS"



cd "${TVD}"

shopt -s extglob

# remove every file that is not an .mp4

rm -i !(*.mp4)

shopt -u extglob

exit 0


Make the script executable, and run it.

$ chmod +x tvd.sh

$ ./tvd.sh

Feb 8, 2016 8:36 AM in response to Pierre L.

Pierre,


The Finder's name extension does not include the dot in its returned value, so your is not condition is never met, and all files, including the mp4 files are in the selection set, and removed. When I tested your one-liner with the ".mp4", all mp4 files were included in the result.


Otherwise, I agree that it is a good alternative to what I provided.


[Edit: I see that you have fixed it while I was writing this. 😉 . Perhaps time to upgrade your coffee?]

Create a script to delete certain files inside a folder

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.