Linux – Shell script Copy file from one directory to another not working

androidbashJenkinslinuxshell-script

Hi I am trying to automate the android build system through jenkins. Here I am trying to copy the image file from one directory to another directory but always getting " No such file or directory"

SRC=/var/lib/jenkins/jobs/Android\ Gradle\ test\ build/workspace/MainApp/app/src/main/res/drawable-hdpi/logo_splash.png
DEST=/var/lib/jenkins/jobs/Android\ Gradle\ test\ build/workspace/MainApp/app/src/main/res/drawable/logo_splash.png

cp -rf  $SRC $DEST

Error log:
cp: target `build/workspace/MainApp/app/src/main/res/drawable/logo_splash.png' is not a directory

Best Answer

Word-Splitting

Protect your parameter strings and expansions from word-splitting by wrapping them in "quotes". Notice how the following examples are colour-coded differently? It's revealing the differences between how the words are being grouped and separated.

You may or may not actually even need to \ escape\ the file path's white-space anymore, either.


Quoted

src="/var/lib/jenkins/jobs/Android\ Gradle\ test\ build/workspace/MainApp/app/src/main/res/drawable-hdpi/logo_splash.png"
dest="/var/lib/jenkins/jobs/Android\ Gradle\ test\ build/workspace/MainApp/app/src/main/res/drawable/logo_splash.png"

cp -rf  "$src" "$dest"

Un-Quoted

SRC=/var/lib/jenkins/jobs/Android\ Gradle\ test\ build/workspace/MainApp/app/src/main/res/drawable-hdpi/logo_splash.png
DEST=/var/lib/jenkins/jobs/Android\ Gradle\ test\ build/workspace/MainApp/app/src/main/res/drawable/logo_splash.png

cp -rf  $SRC $DEST
Related Question