chmod
Change file mode bits 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.stat("~/test.txt").mode.to_s
"-rw-rw-r--"
result = host.chmod("~/test.txt", "600")
result.success?
true
result.mode.to_s
"-rw-------"
Parameters
| Field | Type | Description |
|---|---|---|
| path |
string
|
Required location of file / directory to update bits. |
| mode |
string
integer
Kanrisuru::Mode
|
Required. If value is string, can take the format of a symbolic represenation, ie "-rwxrwxrwx", or a numeric value "777". If the value is an integer, it can the format of an octal format, 0o777. Note a normal integer will throw an error. Finally, a Kanrisuru::Mode instance can be used, which is a helper class for manipulating a file mode, see mode for more details. |
| recursive |
boolean
|
If path is a directory, recursively update all sub-directories and files to the specified file mode bits. |
Result
Returns a single FileStat struct.
FileStat Fields
| Field | Type | Description |
|---|---|---|
| mode |
Kanrisuru::Mode
|
The mode bits set for the file / directory. |
| blocks |
integer
|
The number of blocks allocated for the file. |
| device |
string
|
The major and minor device number combined into one value (in hex and decimal) of the device on which the file resides. |
| file_type |
string
|
What's the file type, ie: directory, file, block_device, char_device, symlink |
| gid |
integer
|
The group id in which the file belongs to. |
| group |
string
|
The group name in which the file belongs to. |
| links |
integer
|
Number of hard links pointing to the underlying inode. |
| inode |
integer
|
Unique inode value assigned to the file |
| path |
string
|
Where is the file located at |
| fsize |
integer
|
Size of the file (in bytes) |
| uid |
integer
|
The user id that owns the file |
| user |
string
|
The user that owns the file |
| last_access |
DateTime
|
When was the last time the file was accessed. |
| last_modified |
DateTime
|
When was the last time the file was updated. |
| last_change |
DateTime
|
When was the last time the file was changed. |
Return Example
host.chmod("~/test.txt", 0o604).data
#<Struct:Kanrisuru::Core::Stat::FileStat:0x00000988
fsize = 0,
blocks = 0,
device = "fc01",
file_type = "regular empty file",
gid = 1000,
group = "ubuntu",
inode = 361173,
last_access = #<DateTime: 2021-08-23T23:43:19-05:00 ((2459451j,16999s,296770022n),-18000s,2299161j)>,
last_changed = #<DateTime: 2021-08-24T00:10:21-05:00 ((2459451j,18621s,385906520n),-18000s,2299161j)>,
last_modified = #<DateTime: 2021-08-23T23:43:19-05:00 ((2459451j,16999s,296770022n),-18000s,2299161j)>,
links = 1,
mode = #<Kanrisuru::Mode:0x1360 @numeric=604 @symbolic=-rw----r-->,
path = "/home/ubuntu/test.txt",
uid = 1000,
user = "ubuntu"
>
Exit Status
| Code | Description |
|---|---|
| 0 | Success |
| 1 | Failure |
Additional Examples
mode = host.stat("~/test-dir").mode
mode.symbolic
"drwxrwxr-x"
mode.symbolic = "drwxrwxr--"
mode = host.chmod("~/test-dir", mode).mode
#<Kanrisuru::Mode:0x1540 @numeric=774 @symbolic=drwxrwxr-->
mode.other.numeric = "0"
host.chmod("~/test-dir", mode).mode
#<Kanrisuru::Mode:0x1560 @numeric=770 @symbolic=drwxrwx--->
Tested On
- Ubuntu, Debian, Centos, Fedora, Redhat, OpenSuse, SLES