Kanrisuru

find

Search for files in a directory hierarchy in ruby
linux manual

Basic Usage

require 'kanrisuru'
host = Kanrisuru::Remote::Host.new(host: '127.0.1.1', username: 'ubuntu', keys: ['~/.ssh/id_rsa'])

result = host.find(paths: '/bin/*')
result.success?
true

result.data.map(&:path)
[
  [   0] "/bin/NF",
  [   1] "/bin/VGAuthService",
  [   2] "/bin/[",
  [   3] .. [1150],
  [1151] "/bin/znew",
  [1152] "/bin/zsh",
  [1153] "/bin/zsh5"
]

Parameters

Field Type Description
paths string
array
Which directories to search in
path string
The shell pattern the filename matches.
type string
Which file type to search, directory, file, symlinks, block, character, pipe, or socket
size string
integer
File uses n units of space. An optional suffix can be appeneded, in the format "+1M", where the suffix 'b' stands for 512-byte blocks, 'c' for bytes, 'w' for two-byte words, 'k' for kilobytes, 'M' for Megabytes, 'G' for Gigabytes.
regex string
File name matches a regular expression pattern. This is a match on the whole path, not a search. For example, to match a file named './fubar3', you can use the regular expression '.*bar.' or '.*b.*3', but not 'f.*r3'. The regular expressions understood by find are by default Emacs Regular Expressions, but this can be changed with the -regextype option.
iregex string
Like regex, but the match is case insensitive.
regex_type string
Changes the regular expression syntax understood by regex. Currently-implemented types are emacs (this is the default), posix-awk, posix-basic, posix-egrep and posix-extended.
follow string
How find should handle symbolic links. With 'never', find will use the information from the properties of the symbolic link itself. 'always' will use the properties of the file the link is pointing to. 'command' only follows symbolic links when find is processing command line arguments.
name string
Base of the filename matches pattern.
iname string
Like name, but the match is case insensitive.
gid integer
File's numeric group id is n
uid integer
File's numeric user id is n
user string
File belongs to user uname
group string
File belongs to group gname
inode integer
File has inode number n.
links integer
File as n links.
maxdepth integer
Descend at most n levels of directories (non-negative number).
mindepth integer
Do not apply any tests or actions at levels less than levels (non-negative number)
atime string
File was last accessed n*24 hours ago. When find figures out how many 24-hour periods ago the file was last accessed, any fractional part is ignored, so to match -atime +1, a file has to have been accessed at least two days ago.
ctime string
File's status was last changed n*24 hours ago. See the comments for atime to understand how rounding affects the interpretation of file status change times.
mtime string
File's data was last modified n*24 hours ago. See the comments for atime to understand how rounding affects the interpretation of file modification times.
amin integer
File was last accessed n minutes ago
cmin integer
File's status was last changed n minutes ago.
mmin integer
File's data was last modified n minutes ago.
executable boolean
Matches files which are executable and directories which are searchable.
empty boolean
File is empty and is either a regular file or a directory.
readable boolean
Matches files which are readable.
writable boolean
Matches files which are writable.
nogroup boolean
No group corresponds to file's numeric group ID.
nouser boolean
No user corresponds to file's numeric user ID.
mount boolean
Don't descend directories on other filesystems.

Result

Returns an array of FilePath structs.

FilePath Fields

Field Type Description
path string Symbolic or absolute file path

Return Example

host.find(paths: '/dev', regex: '/dev/tty[0-9]*').data
[
  [ 0] #<Struct:Kanrisuru::Core::Find::FilePath:0x00000690
    path = "/dev/tty63"
  >,
  [ 1] #<Struct:Kanrisuru::Core::Find::FilePath:0x000006e0
    path = "/dev/tty62"
  >,
  [ 2] #<Struct:Kanrisuru::Core::Find::FilePath:0x00000730
    path = "/dev/tty61"
  >,
  [ 3] .. [61],
  [62] #<Struct:Kanrisuru::Core::Find::FilePath:0x000019f0
    path = "/dev/tty1"
  >,
  [63] #<Struct:Kanrisuru::Core::Find::FilePath:0x00001a40
    path = "/dev/tty0"
  >,
  [64] #<Struct:Kanrisuru::Core::Find::FilePath:0x00001a90
    path = "/dev/tty"
  >
]

Exit Status

Code Description
0 All files are processed succesfully.
1+ Error occured.

Tested On

  • Ubuntu, Debian, Centos, Fedora, Redhat, OpenSuse, SLES