Download presentation
Presentation is loading. Please wait.
1
CIT 383: Administrative Scripting
Find CIT 383: Administrative Scripting
2
CIT 383: Administrative Scripting
The find command Searches for files in a directory tree find path operators Can search for files based on -name -perm -size -type -user -group -atime, -mtime, -ctime CIT 383: Administrative Scripting
3
CIT 383: Administrative Scripting
Find times Numbers refer to 24-hour periods Find files modified during the 24-hour period that ended exactly 3 days ago, i.e. the time between 72 and 96 hours ago. find / -type f –mtime 3 - refers to times after the 24-hour period Find all files modified between now and 3 days ago. find / -type f –mtime -3 + refers to times before the 24-hour period Find all files not accessed in last 30 days find / -type f –atime +30 CIT 383: Administrative Scripting
4
CIT 383: Administrative Scripting
Fast find commands The locate command searches index for files locate filename Faster than find, but Can only search based on name. Will miss files not in index. Must update index regularly with updatedb. CIT 383: Administrative Scripting
5
Manipulating files with find
Find can exec a command on each file find . –name “*.bak” –exec rm –f {} \; find /var/logs –mtime +7 –exec rm –f {} \; find / –user jsmith –exec chown root {} \; To operate on all at once, pipe to xargs find /var/logs –mtime +7 | xargs grep failure find / -user jsmith | xargs file CIT 383: Administrative Scripting
6
CIT 383: Administrative Scripting
Find.find Execute block for all files under path Find.find(topdir) do |path| ... process path ... end Use prune inside block to ignore directories. CIT 383: Administrative Scripting
7
CIT 383: Administrative Scripting
Finding Files by Name Match whole name if path == “core” Match using substrings. if path[-4,4] == “.xls” Match using regular expressions. if path =~ /\.(xls|doc|ppt)$/ CIT 383: Administrative Scripting
8
Finding Files by Other Criteria
Obtain a File::Stat object Find.find(topdir) do |path| stat = File::Stat.new(path) ... process based on stat ... end Match by size if stat.size > 1_000_000 Match by owner if stat.uid == Process.uid CIT 383: Administrative Scripting
9
Storing found filenames in an Array
matching_files = Array.new Find.find(topdir) do |path| if condition matching_files << path end CIT 383: Administrative Scripting
10
Storing file metadata in a Hash
filedata = Hash.new Find.find(topdir) do |path| stat = File::Stat.new(path) if condition filedata[path] = stat.method end CIT 383: Administrative Scripting
11
Storing file data in a Hash
filedata = Hash.new Find.find(topdir) do |path| File.open(path) do |fh| if condition filedata[path] = fh.read end CIT 383: Administrative Scripting
12
CIT 383: Administrative Scripting
References Michael Fitzgerald, Learning Ruby, O’Reilly, 2008. David Flanagan and Yukihiro Matsumoto, The Ruby Programming Language, O’Reilly, 2008. Hal Fulton, The Ruby Way, 2nd edition, Addison-Wesley, 2007. Dave Thomas with Chad Fowler and Andy Hunt, Programming Ruby, 2nd edition, Pragmatic Programmers, 2005. CIT 383: Administrative Scripting
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.