Hitendra Arvapalli Hitendra Arvapalli - 5 months ago 8x
Bash Question

Error while executing shell script of copiying a directory in ruby if the directory is having a space in it

Lets say I have a directory with name 'test folder' and when I tried to make a copy of it using ruby script it threw an error.

Here is what I tried:

`cp -R test\ folder folder`


usage: cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file
cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file ... target_directory
=> ""


Putting the folder name in apostrophes should work for you:

`cp -R 'test folder' folder`

But a more ruby-style method would be to use FileUtils.cp_r:

require 'fileutils'
FileUtils.cp_r 'test folder', 'folder'

Both of these methods will copy the test folder into the folder so there will be a test folder subdirectory created under folder. If you want to copy the contents of the test folder only, use 'test folder/.' instead of 'test folder' in the commands.