CS 279 - Week 9 Lab - 10-17-12
intro to find command
---------------------
* the find command is used to locate files on a UNIX/
Linux system;
it can recursively search any set of directories
you specify for files that match the supplied
search criteria
* basic structure:
find where-to-look criteria what-to-do
find . -print
...there are defaults...
* on nrs-labs, if you JUST type find:
find
...that's the same as typing
find . -print
. says to start in the current working directory
-print is a criteria that's always true, and
print/produce to standard output the names of all
the files that have been found
* -name filename
...searches for files with that filename
...I want to find all the files named pig.txt
starting from my home directory
find ~ -name pig.txt -print
...looks for files index.php JUST looking
in my public_html directory
find ~/public_html -name index.php -print
...counts the number!
find ~/public_html -name index.php -print | wc -l
(a number of criteria/what-to-do options
have an implied -print included --
-name is one of those:
find ~/public_html -name index.php
find ~/public_html -name index.php | wc -l
* IF your find is going to encounter directories
it isn't allowed to read,
remember you can redirect error messages using
2> notation in bash;
(and if you just want to throw them away,
redirect them to /dev/null!)
2> /dev/null
* you can use filename expansion patterns
with find, BUT you need to escape special characters
like * ? etc.
* you can escape them with / or by quoting
them -- both single and double quotes seem to
work
* more criteria:
-type desired-type
-mtime amt --- files modified within the specified
number of days
-7 means within LESS than 7 days
(7 would mean exactly 7 days,
+7 would mean MORE than 7 days) --
(see find's man page for a discussion
of how the number of days is
calculated -- sounds a little
odd...)
* bash shell programming feature of the day:
arrays in bash!
------------------------------------------------
* you can create a bash array by setting a variable
to the desired contents within a set of parentheses
(separated by white space)
arr=(Hello World 313 foo oink)
* to SET/assign an array element,
you use the notation
arrayname[index]
to access (use) an array element,
you use the notation:
${arrayname[index]}
* additional useful funky array constructs:
${arrayname[*]} - all of the items in the array
${!arrayname[*]} - all of the indices in the array
${#arrayname[*]} - number of items in the array
${#arrayname[desiredIndex]} - length of item in the
array with that index
* if you want to initialize an array using the parenthesis
notation, but you want a non-consecutive index to
be filled,
use [index]=value WITHIN the parentheses
arr2=(moo a b [10]=oink [15]=moo)