Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

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

Why can't I create a directory by full relative path argument? Here is what I try:

# Print the command and run it. Exit the script on failure.
        if $VERBOSE; then
                echo "$@"
                "$@" >& /dev/null
        if [ $result -ne 0 ]; then
                exit $result
                if [ ! -d ${args[0]} ]; then
                        run mkdir -p ${args[0]}

                run cd ./release/
                run cp -rf ./html/ ${args[0]}

but I get:

mkdir -p ./bla/bla/cloud/
cd ./release/
cp -rf ./html/ ./bla/bla/cloud/
cp: cannot create directory `./bla/bla/cloud/': No such file or directory

So I wonder how to make script work when called like this: ./script.sh ./bla/bla/bla/

share|improve this question
up vote 6 down vote accepted

Suppose you start from /some/dir.

mkdir -p ./bla/bla/cloud/          # create /some/dir/bla/bla/cloud
cd ./release/                      # cd /some/dir/release
cp -rf ./html/ ./bla/bla/cloud/    # copy to /some/dir/release/bla/bla/cloud

By definition, a relative path changes when you change the current directory.

share|improve this answer

The issue is you are using relative paths but you change directories in the middle of the script. If you mentally trace through the steps:

# command                     # relevant absolute path(s)
./script.sh ./bla/bla/bla/    # PWD/script.sh 
mkdir -p ./bla/bla/bla/       # PWD/bla/bla/bla/
cd ./release/                 # PWD/release
cp -rf ./html/ ./bla/bla/bla/ # PWD/release/html PWD/release/bla/bla/bla   

Notice how you created PWD/bla/bla/bla/ but are attempting to copy into PWD/release/bla/bla/bla/. You should make sure the mkdir and the cp occur in the same directory.

Here's a suggested alternative. I replaced the entire run () function with the shell options xtrace (-x), which displays every command before it is run, and errexit (-e), which exits if any command returns an error.

#!/bin/bash -xe

cd ./release/

if [ ! -d "$1" ]; then
        mkdir -p "$1"
cp -rf ./html "$1"
share|improve this answer

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.