I'm trying to recursively create directories through ssh.

The directory /home/user/staging exists. But the user has no permission to read /home. mkdir doesn't know if /home/user exists and tries to create it.

mkdir -p /home/user/staging/first/second/third/fourth/
mkdir: cannot create directory ‘/home/user’: Permission denied

How to skip this directory and go on creating where user has permission?

I'm trying to do it with my own script,

    if [[ ! -e $dir ]]; then
        echo "creating $dir"
        newdir=$(echo $dir | sed 's:/[^/]*$::')
        rec $newdir
        mkdir $dir

Fore some reason this check fails if [[ ! -e $dir ]] and loop goes down past existing dirs and eventually doesn't create anything.

  • what would help: id; ls -ld /home
    – A.B
    Apr 22, 2018 at 9:23
  • How about cd'ing into the user's home, then creating relative directories?
    – Xen2050
    Apr 22, 2018 at 9:42
  • if user doesn't have execute permission on /home, then this is the expected behaviour. This answer about directory permissions might help you.
    – danzel
    Apr 22, 2018 at 11:16

1 Answer 1


I didn't manage to make this script work.

However, solution is to use --relative option of rsync itself.

rsync -a --relative /new/x/y/z/ user@remote:/pre_existing/dir/

As described in this answer: https://stackoverflow.com/a/22908437/6505678

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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