1 / 12

CIT 383: Administrative Scripting

Find. 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 -size -atime, -mtime, -ctime. Find times. Numbers refer to 24-hour periods

sheera
Download Presentation

CIT 383: Administrative Scripting

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Find CIT 383: Administrative Scripting 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 -size -atime, -mtime, -ctime

  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

  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.

  5. CIT 383: Administrative Scripting 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

  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.

  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)$/

  8. CIT 383: Administrative Scripting 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

  9. CIT 383: Administrative Scripting Storing found filenames in an Array matching_files = Array.new Find.find(topdir) do |path| if condition matching_files << path end end

  10. CIT 383: Administrative Scripting 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 end

  11. CIT 383: Administrative Scripting 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 end end

  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.

More Related