Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am creating a bash script that is suppose to copy all files in ~/Development/ to another computer on my network. I have enabled it to mount the computer if it is not there, but my real problem is creating folders.

I want to save the files in /Volumes/Users/Public/InfiniteBackups/Development/, therefore I run some if statements to check if they exist.

#! /bin/bash

echo "Starting Backup of Development Files"
say "Starting Backup of Development Files"

if [ -d "/Volumes/Users/Public/" ]; then
    # Control will enter here if $DIRECTORY exists.
echo "Location is there..."

    # Start 
echo "Copying Files"

if [ ! -d "/Volumes/Users/Public/InfiniteBackups/Development" ] ; then
  echo "/Volumes/Users/Public/InfiniteBackups/Development did not exist."
  say "Location did not exist, creating it."
  cd /Volumes/Users/Public/
  if [ ! -d "/Volumes/Users/Public/InfiniteBackups"]; then
  say "InfiniteBackups did not exist, creating it…"
  mkdir InfiniteBackups


  if [! -d "/Volumes/Users/Public/InfiniteBackups/Development"]; then
  cd InfiniteBackups
  mkdir Development


cp -v -rip ~/Desktop/ /Volumes/Users/Public/InfiniteBackups/Development
echo "Files have finished transferring"
say "Files have finished transferring"


It will say /Volumes/Users/Public/ exists, and it will say /Volumes/Users/Public/InfiniteBackups/Development does not exist. It then checks to see if /Volumes/Users/Public/InfiniteBackups/ exists, which it shouldn't. Then it is suppose to make the directory. However, it instead acts like the volume did exist even though it clearly doesn't.

Why doesn't it work. Thanks.

share|improve this question
Why not use mkdir -p and cp -r? – KAction Oct 18 '12 at 11:36
What does mkdir -p do. I am using -r for cp. – Josiah Oct 18 '12 at 13:18
You check if some directory exist, if not, create it. mkdir -p create path, with all required parent dirs. It do not consider mistake directory existance, so you can just mkdir -p /Volumes/Users/Public/InfiniteBackups without conditional. – KAction Oct 18 '12 at 14:39
Ah, thanks for the tip. That is quite useful. Oh, but what argument do I use so that cp will automatically overwrite my previous files? After the first backup I have to say yes to each overwrite. – Josiah Oct 18 '12 at 15:18
Look in manual. There is a lot there. To force overwrite, use -f. – KAction Oct 18 '12 at 15:21
up vote 1 down vote accepted

Try adding a space between " and ]. You are missing that in two places. The whole test returns a non-zero number all the time because it's giving an error.

I tested my theory on Linux, it should be the same for OS X...

enter image description here

share|improve this answer
Thanks, that did it. I haven't really used bash scripts that often. I wasn't aware that white spaces did anything. Well, it's working now. – Josiah Oct 16 '12 at 12:50
Your image seems to be broken – slhck Oct 16 '12 at 13:33
I can see it. Here's the location – Radoo Oct 16 '12 at 13:33

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.