Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
add comment

2 Answers

up vote 5 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
add comment

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
add comment

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.