Creating and removing directories via command line

  1. is there a more elegant way to delete all the sub contents of a dir and not the top dir itself ( currently we are doing a rm -r and then a mkdir)

  2. one directory we want to delete and create has a s space in the title ( server suitcase fonts) but the mkdir only reads till the first space.

is there anyway to make mkdir understnad the spaces or put a new char in the place of it?

“Two easy questions” is not helpful when searching; I’ve change the subject to “Creating and removing directories via command line.”

thanks

any chance you might also know the answer?

Hi,

if you have space(s) in the path, you must either escape the space(s) or put the whole path into quotes.
Oddly the rm command with a wildcard (e.g. /Users/Desktop/Test/*) doesn’t work on my machine,
if the path is quoted, so the following script uses a repeat loop:

Caution: executing the script deletes permanently the contents of the chosen folder

set theFolder to (choose folder)
tell application "Finder" to set theItems to (get items of theFolder)
repeat with i in theItems
	do shell script "rm -rf " & quoted form of POSIX path of (i as alias)
end repeat

The quotes protect the asterik from “shell globbing”:

You can add the asterik on to the end, after the rest of the path has been quoted.

Caution: This script deletes files!

choose folder with prompt "Delete contents of this folder:"
set targetFolder to POSIX path of result

do shell script "/bin/rm -r " & quoted form of targetFolder & "*"

Thanks again, Bruce, you taught me a few important things today :slight_smile: