Syntax for directory paths that contain spaces.

vquest

New Member
Joined
Jul 20, 2023
Messages
2
Reaction score
0
Credits
21
Hi all. I'm having difficulty working out how to get a script working when there are spaces in the folder path. I've tried all the suggestions that google has turned up, but none have worked. The code below tries to escape the space, but it doesn't work. I know that I can remove spaces, from the code and the folder itself, but I'd really like to learn how to write code that works without having to. Any help will be much appreciated.


#!/bin/zsh

source_folder="/Users/rg/Documents/Test\ 1/"
destination_folder="/Users/rg/Documents/Test\ 2/"

rsync -av --dry-run $source_folder $destination_folder
 


Hi,

put it in single quotes without the escapes. Like this:

source_folder='/Users/rg/Documents/Test 1/'

Code:
export source_folder='/Users/rg/Documents/Test 1/'
echo $source_folder
/Users/rg/Documents/Test 1/

also put the variables in the actual command in double quotes:

rsync -av --dry-run "$source_folder" "$destination_folder"

regards,
CS
 
Hi,

put it in single quotes without the escapes. Like this:

source_folder='/Users/rg/Documents/Test 1/'

Code:
export source_folder='/Users/rg/Documents/Test 1/'
echo $source_folder
/Users/rg/Documents/Test 1/

also put the variables in the actual command in double quotes:

rsync -av --dry-run "$source_folder" "$destination_folder"

regards,
CS
Thanks heaps. That's got me back on track. I'm pleased I found this site.
 
Hi all. I'm having difficulty working out how to get a script working when there are spaces in the folder path. I've tried all the suggestions that google has turned up, but none have worked. The code below tries to escape the space, but it doesn't work. I know that I can remove spaces, from the code and the folder itself, but I'd really like to learn how to write code that works without having to. Any help will be much appreciated.


#!/bin/zsh

source_folder="/Users/rg/Documents/Test\ 1/"
destination_folder="/Users/rg/Documents/Test\ 2/"

rsync -av --dry-run $source_folder $destination_folder
You didn't need to escape the spaces in the first place because you were using double quotes!
All you had to do was this:
Code:
#!/bin/zsh

source_folder="/Users/rg/Documents/Test 1/"
destination_folder="/Users/rg/Downloads/Test 2/"

rsync -av --dry-run "$source_folder" "$destination_folder"
In the above - there is no need to escape the spaces in the directory paths, because we're using double quotes. And as an extra safety mechanism, in the rsync command - where we dereference/substitute out the values of the variables - I've enclosed the variable names inside double quotes to ensure that their contents are properly quoted.
 
Why create variables at all? Is there some benefit to it?

Why not simply:
Code:
#!/bin/zsh

rsync -av --dry-run "/Users/rg/Documents/Test 1/" "/Users/rg/Downloads/Test 2/"

A few other thoughts....

1) Why "/Users/rg/"... that looks more like a Windows path than Linux, which seems like it should be "/home/rg/" ? Are you running rsync in WSL? This has me confused, but no one else asked about it.

2) I would use "rsync -avs" or "rsync -avhs" options. The "h" will report sync numbers in "human" format. The "s" option (--protect-args) helps to ensure that spaces are handled properly by rsync and not interpreted by the shell. See this article for more information.

3) Lastly, just a word of caution about using the final / character after "Test 1" but you may already know this, so please forgive me if you do. Using the final / in the source means that the Test 1 directory itself will not copy to Test 2, but all the files/folders inside it will copy. If you want the Test 1 folder itself (and all its contents) to copy to Test 2, then don't use the final / character. It can really make a mess in your destination folder if you unexpectedly sync a bunch of loose files and folders and not their containing folder. :oops:

Good luck!
 

Members online


Top