Shell Script to copy directories

I’m trying to do a very very basic script that copies all of the files from one folder to another. I am obviously messing something up with the syntax because it’s telling me the folder doesn’t exist. Can someone enlighten me on what I’m doing incorrectly? Thanks!

set originalfolder to "~/Library/Calendars/"
set destfolder to "~/Sites/phpicalendar/Calendars/"
do shell script "/bin/cp -v '" & originalfolder & "' '" & destfolder & "'"

The error that I get is: cp: directory ~/Sites/phpicalendar/Calendars does not exist

I don’t know if anything’s changed, but last I heard you needed to use ‘ditto’ with its ‘rsrc’ flag to make sure you didn’t obliterate the resource fork on Mac files. I’d suggest the following:


set eachSourceFile to (quoted form of POSIX path of (choose folder)) & "*"
set destFold to quoted form of POSIX path of (choose folder)
set command to "ditto -rsrc "
do shell script command & eachSourceFile & space & destFold

You need the “*” because it’s shell speak for ‘get all the files in this directory’. However, since this is an AppleScript message board, why not go the sweet AS way:


tell application "Finder" to move every file of folder (choose folder) to folder (choose folder)

I agree, you’ll probably want to use ditto to preserve resource forks (or CpMac), but as for why your cp command wasn’t working. You’re trying to copy the contents of a directory, so you need to do a recursive copy which is designated with the -R flag.

But the command is erroring before it even gets to that point. Tilde expansion doesn’t work in a quoted path. Luckily, your paths don’t have any spaces in the folder names so it’s not really a big deal.

do shell script "/bin/cp -Rv " & originalfolder & " " & destfolder

should work. If you want to use quotes, you just can’t start the quote until after “~/”. And don’t forget that AS can automatically quote things for you by getting the “quoted form of”. You could’ve written your original quoted code like this and it would work correctly.

set originalfolder to "Library/Calendars/"
set destfolder to "Sites/phpicalendar/Calendars/"
do shell script "/bin/cp -Rv ~/" & quoted form of originalfolder & " ~/" & quoted form of destfolder

It’s because he was quoting ’ ~ ', a special character in the shell, robbing it of its specialness.

I had tried several variations for the path including without the ~ and none worked. Yours does. Thanks!