27.08.2015 Views

Advanced Bash−Scripting Guide

Advanced Bash-Scripting Guide - Nicku.org

Advanced Bash-Scripting Guide - Nicku.org

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Advanced</strong> <strong>Bash−Scripting</strong> <strong>Guide</strong><br />

12.2. Complex Commands<br />

Commands for more advanced users<br />

find<br />

−exec COMMAND \;<br />

Carries out COMMAND on each file that find matches. The command sequence terminates with \; (the<br />

";" is escaped to make certain the shell passes it to find literally). If COMMAND contains {}, then find<br />

substitutes the full path name of the selected file for "{}".<br />

bash$ find ~/ −name '*.txt'<br />

/home/bozo/.kde/share/apps/karm/karmdata.txt<br />

/home/bozo/misc/irmeyc.txt<br />

/home/bozo/test−scripts/1.txt<br />

find /home/bozo/projects −mtime 1<br />

# Lists all files in /home/bozo/projects directory tree<br />

#+ that were modified within the last day.<br />

#<br />

# mtime = last modification time of the target file<br />

# ctime = last status change time (via 'chmod' or otherwise)<br />

# atime = last access time<br />

DIR=/home/bozo/junk_files<br />

find "$DIR" −type f −atime +5 −exec rm {} \;<br />

# Deletes all files in "/home/bozo/junk_files"<br />

#+ that have not been accessed in at least 5 days.<br />

#<br />

# "−type filetype", where<br />

# f = regular file<br />

# d = directory, etc.<br />

# (The 'find' manpage has a complete listing.)<br />

find /etc −exec grep '[0−9][0−9]*[.][0−9][0−9]*[.][0−9][0−9]*[.][0−9][0−9]*' {} \;<br />

# Finds all IP addresses (xxx.xxx.xxx.xxx) in /etc directory files.<br />

# There a few extraneous hits − how can they be filtered out?<br />

# Perhaps by:<br />

find /etc −type f −exec cat '{}' \; | tr −c '.[:digit:]' '\n' \<br />

| grep '^[^.][^.]*\.[^.][^.]*\.[^.][^.]*\.[^.][^.]*$'<br />

# [:digit:] is one of the character classes<br />

# introduced with the POSIX 1003.2 standard.<br />

# Thanks, S.C.<br />

The −exec option to find should not be confused with the exec shell builtin.<br />

Example 12−2. Badname, eliminate file names in current directory containing bad characters<br />

and whitespace.<br />

Chapter 12. External Filters, Programs and Commands 151

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!