Shell Script to Delete all Contents of a Specified Folder

I created this script to delete all contents of a specified folder. It works great but now I would like to bypass the finder and use a shell script to remove all the folder contents instead of them going to the trash. I know a “rm” command needs to be used. I’m a little stuck on how the shell script needs to be coded. Any help would be appreciated!

-Aaron



set theFolder to choose folder with prompt "Choose folder to delete all contents"

tell application "Finder"
	
	delete (every item of theFolder)
	
end tell


Post deleted by peavine

Thanks for your reply peavine!

I actually want to keep the folder and only delete the contents using shell scripting.

I appreciate your input.

-Aaron

Apologies. Your post was clear and I didn’t read it carefully.

You can bypass the trash with System Events as follows. I tried “every item” but it wouldn’t work for some reason and instead used every folder then every file.

set theFolder to (choose folder) as text

tell application "System Events"
	delete (every folder in folder theFolder)
	delete (every file in folder theFolder)
end tell

A script with rm shell command, which deletes all files and folder in the target folder:

set theFolder to POSIX path of (choose folder)

do shell script "rm -R " & quoted form of theFolder & "*"

EDIT:

After reading Marc Anthony’s post, I recalled that System Events deletes hidden files, which may not be desirable. To avoid that:

set theFolder to (choose folder) as text

tell application "System Events"
	delete (every file in folder theFolder whose visible is true)
end tell

Hi. As a caveat, you can easily suffer unintentional data loss when using the shell for direct deletion. Your question lacks specificity as to how delete happy you want to get. This avoids invisible DS store files, files in subfolders, and packages, but I’d still take care in where it’s being pointed.

do shell script "find -E " & my (choose folder)'s POSIX path's quoted form & " -name '*.*' -prune ! -regex '.*/[.].*' -maxdepth 1 -delete"

Peavine, no need for apologies! This script works great! Thank you!! The only question I have is…would this process be faster if it was in shell script? I just noticed Marc Anthony chimed in too. Let me explain what I’m doing. I work on photoshoots and the photographer(s) are able to shoot thousands of photos to cards (SD,CF, CFast, etc). What I would like to do is…Once I’m certain I have safely backed up the images in at least 3 locations at the time of importing, I would like to run a script to remove all the images in a specified folder on the card so I can hand it back to the photographer and he/she can begin shooting again without formatting the card in camera. Sorry for the long winded explanation. I’ve been doing a little shell scripting and have noticed how fast it processes. I understand there can be irrevocable data loss using the “rm” in shell. I appreciate your input.

-Aaron

Aaron. The only way to know for sure is to run some tests, but I would certainly expect the rm shell command to be faster than System Events when removing thousands of files. The script that utilizes the rm command in my above post deletes all files and folders in the target folder, but this could be modified to delete just what you want. For example,

set theFolder to POSIX path of (choose folder)

do shell script "rm -f " & quoted form of theFolder & "*.jpg"

BTW, since this is an AppleScript forum, I assume you don’t mean an actual shell script. If you do, I wouldn’t expect it to be significantly faster, although I can’t say I’ve ever run any timing tests on this.

Brilliant!!

These lines of code do exactly what I want them to do! I modified the script to remove .JPG and .CR2 files instantaneously from the card’s capture folder. What’s great is I can change the extension from “.CR2” to “.NEF” …etc when using other camera’s propriety image files.

I appreciate your help peavine and Marc Anthony and getting me to wrap my head around the problem. Cheers!

-Aaron

Below is the code that works for me when using a Canon 5D IV with RAW+JPEG



set theFolder to POSIX path of (choose folder)

do shell script "rm -f " & quoted form of theFolder & "*.JPG"
do shell script "rm -f " & quoted form of theFolder & "*.CR2"


Aaron. I’m glad that does the job.

Just as a point of information, the separate do-shell-script lines can be combined as shown below and this may be marginally faster:

set theFolder to quoted form of POSIX path of (choose folder)

do shell script "rm -f " & theFolder & "*.JPG" & space & theFolder & "*.CR2"

Thanks peavine! I’ll give that code a go! I’ll check out Shane’s post! Thanks Fredrik71!

-Aaron