0

I want to copy everything from a source directory (directory name contains spaces) to a destination.

The output of the ls -l command inside my working directory is as below:

drwxrwxr-x 2 ubuntu ubuntu 4096 Apr  5 06:39  destination
drwxrwxr-x 2 ubuntu ubuntu 4096 Apr  5 06:08 'source directory with spaces'

When I do - cp -r ./source\ directory\ with\ spaces/. destination/ the command works perfectly well and files (A.txt, B.txt & C.txt) inside the 'source directory with spaces' folder are copied successfully.

I want to making it dynamic, so I store the source location in a variable as -

fileVar="./source\ directory\ with\ spaces/."

Now when I hit the cp command using either of the below command:

cp -r $fileVar destination/

Error:

cp: cannot stat './source\': No such file or directory
cp: cannot stat 'directory\': No such file or directory
cp: cannot stat 'with\': No such file or directory
cp: cannot stat 'spaces/A.txt': No such file or directory

OR

cp -r "$fileVar" destination/

Error:

cp: cannot stat './source\ directory\ with\ spaces/A.txt': No such file or directory

How can I fix it? Thanks in advance.

Cyrus
  • 84,225
  • 14
  • 89
  • 153
Ashwin
  • 12,081
  • 22
  • 83
  • 117

0 Answers0