Sunday, November 29, 2020

#1 2020-11-18 04:23:26 pm

CAPTURE-A
Member
From:: Brooklyn
Registered: 2017-01-15
Posts: 15

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




Applescript:



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

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

Last edited by CAPTURE-A (2020-11-18 09:42:50 pm)

Offline

 

#2 2020-11-18 05:30:16 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 673

Re: Shell Script to Delete all Contents of a Specified Folder

Post deleted by peavine

Last edited by peavine (2020-11-18 06:01:27 pm)


2018 Mac mini - macOS Catalina

Offline

 

#3 2020-11-18 05:34:50 pm

CAPTURE-A
Member
From:: Brooklyn
Registered: 2017-01-15
Posts: 15

Re: Shell Script to Delete all Contents of a Specified Folder

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

Offline

 

#4 2020-11-18 05:51:45 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 673

Re: Shell Script to Delete all Contents of a Specified Folder

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.

Applescript:

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:

Applescript:

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:

Applescript:

set theFolder to (choose folder) as text

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

Last edited by peavine (2020-11-18 07:30:14 pm)


2018 Mac mini - macOS Catalina

Offline

 

#5 2020-11-18 06:00:25 pm

Marc Anthony
Member
From:: Dallas, TX
Registered: 2006-04-27
Posts: 981

Re: Shell Script to Delete all Contents of a Specified Folder

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.

Applescript:

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

Offline

 

#6 2020-11-18 06:17:26 pm

CAPTURE-A
Member
From:: Brooklyn
Registered: 2017-01-15
Posts: 15

Re: Shell Script to Delete all Contents of a Specified Folder

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

Last edited by CAPTURE-A (2020-11-18 07:16:44 pm)

Offline

 

#7 2020-11-18 07:19:33 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 673

Re: Shell Script to Delete all Contents of a Specified Folder

CAPTURE-A wrote:

The only question I have is...would this process be faster if it was in shell script?



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,

Applescript:

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.

Last edited by peavine (2020-11-18 08:00:51 pm)


2018 Mac mini - macOS Catalina

Offline

 

#8 2020-11-18 08:03:13 pm

CAPTURE-A
Member
From:: Brooklyn
Registered: 2017-01-15
Posts: 15

Re: Shell Script to Delete all Contents of a Specified Folder

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


Applescript:



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"

Last edited by CAPTURE-A (2020-11-18 08:04:24 pm)

Offline

 

#9 2020-11-19 06:59:59 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 673

Re: Shell Script to Delete all Contents of a Specified Folder

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:

Applescript:

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

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

Last edited by peavine (2020-11-20 07:17:46 am)


2018 Mac mini - macOS Catalina

Offline

 

#10 2020-11-19 09:09:37 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 469

Re: Shell Script to Delete all Contents of a Specified Folder

I have read many posts about content of Finder items or System Events and its very slow.

Here is a post from Shane
https://macscripter.net/viewtopic.php?p … 98#p184498


The purpose to study someone else art is not to add, its to make less more.

Offline

 

#11 2020-11-20 09:34:38 am

CAPTURE-A
Member
From:: Brooklyn
Registered: 2017-01-15
Posts: 15

Re: Shell Script to Delete all Contents of a Specified Folder

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

-Aaron

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)