IT 1100 : Introduction to Operating Systems
Chapter 17 Searching for Files
Don’t forget the Finding Things Exercises in Canvas.
Textbook Time
There are a lot more options for find
in the book than we will use. So don’t feel overwhelmed. Just focus on the power that find
has.
Commands
locate
- find files by name. The locate command references an index compiled the night before, so the searches are very fast. The locate program performs a rapid database search of pathnames, and then outputs every name that matches a given substring.- The file has to be in the database in order for it to be found
- example:
locate bin/zip
, can combine with grep to restrict rows shown
- The locate database is created by another program named updatedb. Usually, it is run periodically as a cron jobonce per day, if you create a file, it won’t be detected by the
locate
command until this command has run.
Commands (find)
find
- search for files with a specific criteria. More powerful but more complex thanlocate
. The search is done real time, so searches may take longer.- More examples:
find ~ -type f -name "*.JPG" -size +1M | wc -l
#find by size, name- Can find by user, last modified time, permissions, more…
Commands (find)
- With find, you can also execute a particular action on the found file like so:
find ~ -type f -name 'foo*' -ok ls -l '{}' ';'
orfind ~ -name 'foo*' -exec rm -rf {} \;
Commands (xargs)
xargs
- The xargs command in UNIX is a command line utility for building an execution pipeline from standard input. What does this mean??- What if you want to search through a bunch of files for a string
find . | xargs grep 'someline'
vs.find . | grep 'someline'
- Compare these two
find ./foo -type f -name "*.txt" -exec rm {} \;
find ./foo -type f -name "*.txt" | xargs rm
touch
- change file times or create new filestat
- display file status
Examples
locate /etc/res
# will list files and directories in the /etc directory that start with resfind /etc -type f
# lists only files in the /etc directory that start with resfind /etc/res* -type d
# lists only directories in the /etc directory that start with resfind ~ -type f -name "*.jpg" -size +1M
# lists only files that end in .jpg and are greater than 1 MB in sizefind ~ -type f -name "*.jpg" -size +1M | xargs ls -l
# xargs takes the output of the find command and lists it in anls -l
format
Last Updated 03/07/2019