Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

gogreen1

macrumors 6502
Original poster
Nov 20, 2017
262
14
I’ve created two copy commands for use in Terminal. I’m just saving and backing up two files from two different directories. I’d like to place them in a batch file and run them from a desktop icon—I used to do this in Windows. I’m new to MacOS, and I’m a bit bewildered by the different instructions on doing this from Google searches. Can someone please tell me how to create a batch file with these two copy commands for use in MacOS High Sierra? Thanks.

cp -av /Users/[UserName]/Library/Safari/Bookmarks.plist /Users/[UserName]/Documents/Backups

cp -av /Users/[UserName]/Library/Group\ Containers/UBF8T346G9.Office/User\ Content.localized/Templates.localized/Normal.dotm /Users/[UserName]/Documents/Backups
 
You can use Automator as suggested. Another thing you could do is create a script file, which is the macOS batch equivalent. Using Bash (the default) you'd just create a file with a '.sh' extension and add '#!/bin/bash' as the first line. This is telling the command line that this is a Bash script and use Bash to interpret it. You'd then put your copy commands on lines below this. You then have to make the .sh file executable. Then you can run the script from Terminal. I think you can have Automator run the script. That way you just change the script as your needs change.

I'm thinking you want some sort of regular backup. You can register your Bash script to run automatically using a plist (XML file) you create and load with launchctl.
 
  • Like
Reactions: gogreen1
The quick way to to join them with && and copy/paste them into terminal, hitting [enter or return] afterwards:
Code:
cp -av /Users/[UserName]/Library/Safari/Bookmarks.plist /Users/[UserName]/Documents/Backups && cp -av /Users/[UserName]/Library/Group\ Containers/UBF8T346G9.Office/User\ Content.localized/Templates.localized/Normal.dotm /Users/[UserName]/Documents/Backups
To make a equivalent .bat file you need to make a .command file with these contents:
Code:
#!/bin/bash
cp -av /Users/[UserName]/Library/Safari/Bookmarks.plist /Users/[UserName]/Documents/Backups
cp -av /Users/[UserName]/Library/Group\ Containers/UBF8T346G9.Office/User\ Content.localized/Templates.localized/Normal.dotm /Users/[UserName]/Documents/Backups
The important step is that .command files are not executable by default (for security). So you need to open terminal, go to the to the directory with the .command file and make it executable. Type cd, a space, then the directory of the file (you can just drag the folder that contains your file into terminal, it will add the correct directory)
Code:
cd [path to the file]
Then make it executable:
Code:
chmod +x [filename.command]
If you want the terminal window to close after running, you need to add two lines to the end of the .command file:
Code:
osascript -e 'tell application "Terminal" to close (every window whose name contains ".command")' &
exit
You can also schedule it to run occasionally.
 
Wow! You guys must have advanced black belts in MacOS. Thanks!
One more question, if I may: Does this file need further tinkering to place it on my desktop and execute it by double-clicking?
Thanks again (deep bow)!
 
Wow! You guys must have advanced black belts in MacOS. Thanks!
One more question, if I may: Does this file need further tinkering to place it on my desktop and execute it by double-clicking?
Thanks again (deep bow)!

If you follow what @cruisin said to do (make the .command file) you can double click on the file to execute it. The key step is the ‘chmod’ command. Once the file is executable then wherever you place it you should be able to execute it. The permission to execute stays with the file.

I didn’t know about .command files and learned something myself.
 
  • Like
Reactions: gogreen1
I've run into a snag on this. I think I created the .command file correctly (I called it "Batchsaves"). It's trying to execute the commands. I tried copying items as text only, but that didn't fix the errors. I also tried re-entering the text manually. How do I correct the errors in the attached? :confused: Thanks. command error.png
 
Last edited:
Can you post the complete contents of your .command file or a screenshot of it?

Did it copy anything?
 
neliason: Here's a screenshot. It didn't copy anything. Thanks

command file text.png
 
Is the 3rd line ‘Templates’ just wrapped and really part of the 2nd line?

It looks like you are missing the ampersand ‘&’ at the end of the osascript line. I believe this is necessary to get that to work properly. There should be a space and then the ampersand.

I’d fix the missing ampersand. If that doesn’t work then I’d start commenting out all the lines but the first ‘cp’. You comment with a pound symbol ‘#’ at the beginning of the line. If the first command works and copies data then uncomment the next line. Keep doing that until you find a line that causes an error.
 
Thanks, neliason--yup, I missed the ampersand at the end of the osascript.

I get errors in the two save commands no matter what. But when I enter them individually in Terminal, they work. What gives with that? I think it's odd that capturing the path to these files produces such a strange-looking path with unexpected spaces and slashes. Still, by themselves, they work in Terminal but not in the .command file.
 
The slash escapes the space in a file or directory name. If the commands work when directly entered in a Terminal they should work in a script. The error message should point you to the problem. I’d comment out everything but the cp lines and see what the error is.
 
You'll get that script running, I'm sure, but you might be better off (like me) doing this sort of thing with Automator.

Thanks, BrianBaughn. I'm totally lost when it comes to Automator--never used it before. For my .command desktop file, what type of document would I choose, and how would I then enter the copy commands?
 
Thanks, kryten2. For the moment, I've created a sync job in FreeFileSync for this task.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.