Finding files
Notes:
The find command searches for files in the filesystem by name, owner, size,
date, ... The format of the find command is:
find [path...] [expression]
where path is the starting point for the search and expression
can be a set of options, tests and actions. Some of the tests:
-
-name pattern : search for files whose name matches the given pattern
-
-mtime -n: search for files whose modification time is less than n days (n*24
hours)
-
-user username: search for files owned by the specified user
-
-type x: search for files of type x, where x can be f (file), d (directory),
l (link) ...
-
-empty: search for empty files
Examples:
-
find /home -name readme
This starts the search in the directory /home and looks for a file named
readme.
-
find / -user alterego
This starts the search at the root directory and looks for all files which
belong to alterego.
-
find / -mtime -1
This starts the search at the root directory and looks for all files which
were modified less than 1 day (24 hours) ago.
-
find / -type d -name read
This starts the search in the root directory and finds all directories named
read.
-
As find scans through the directory tree, there will be directories which
you do not have permissions to search. The find command will not scan these
directories but will display permission denied messages on standard error.
To suppress the permission denied messages, redirect standard error to
/dev/null.
find / -mtime -1 2> /dev/null
This searches for all files that were modified in the last day (24 hours)
starting the search in the root directory and sending all 'permission denied'
messages to the bit bucket
Exercises:
-
Find a file named vtree.tgz.
-
Find all files with the filename alterego.
-
Find all files that belong to floopy.
-
Find all empty files in the /etc directory.
-
Find all files under the /etc directory with a filename that starts with
K (in uppercase).
-
Find all directories starting in /var.
-
Find a file named penguin.
-
Find all files (not directories) that start with "p".
-
Find all files that end with ".gz".
-
Find everything that belongs to the mail group.
Questions & Answers:
-
What command will find all filenames ending in .html?
Answer: find / -name *.html
-
What command will find all files belonging to alterego?
Answer: find / -user alterego
-
What command will find a directory named netlp starting in the /var
directory?
Answer: find /var -type d -name netlp
-
What command will suppress the display of the "permission denied" messages
that are displayed when you use the find command?
Answer: find / -name penguin 2> /dev/null
-
What command will find all empty directories (use man to lookup options for
the find command).
Answer: find / -type d -empty
Copyright © L.
M. MacEwan