cp
Copy files and directories in ruby
linux manual
Basic Usage
require 'kanrisuru'
host = Kanrisuru::Remote::Host.new(host: '127.0.1.1', username: 'ubuntu', keys: ['~/.ssh/id_rsa'])
host.cat('~/test.txt')
[
'hello',
'world'
]
result = host.cp('~/test.txt', '~/test-copy.txt')
result.success?
true
host.cat('~/test-copy.txt')
[
'hello',
'world'
]
Parameters
Field | Type | Description |
---|---|---|
source |
string
array
|
Required A list of files / directories to copy |
destination |
string
|
Required If the the source was a single file, destination can either be a file or directory to copy the file to. For multiple files, the destination is a directory to copy to |
backup |
boolean
string
|
Makes a backup of each existing destination file |
recursive |
boolean
|
This option will copy directories recursively |
one_file_system |
boolean
|
Stay on this file system |
update |
boolean
|
Copy only when the source file is newer than the destination file or when the destination file is missing |
follow |
boolean
|
Follow command line links in source |
no_clobber |
boolean
|
Do not overwrite an existing file |
strip_trailing_slashes |
boolean
|
Remove any trailing slashes from each source argument. |
preserve |
boolean
string
array
|
Preserves the file / directory attributes of source to destination (default - mode,ownership,timestamps) |
no_target_directory |
boolean
|
Treat destination as a normal file |
target_directory |
string
|
Copy all source arguments into directory |
Result
No explicit data struct returned, only option is success?
, failure?
, and status
to see if the program exited properly.
Return Example
result = host.cp(['file1.conf', 'file2.conf', 'file3.conf'], 'dir')
result.success?
true
host.cp(['file1.conf', 'file2.conf', 'file3.conf'], 'dir', backup: true).status
0
host.ls(path: '~/dir').map(&:path)
[
"file1.conf",
"file1.conf~",
"file2.conf",
"file2.conf~",
"file3.conf",
"file3.conf~"
]
Exit Status
Code | Description |
---|---|
0 | Success |
1 | Failure |
Tested On
- Ubuntu, Debian, Centos, Fedora, Redhat, OpenSuse, SLES