find: search files and do batch operations
Table of Contents
You can use ‘find’ to look for files based on their names, sizes, or other properties and execute commands against them.
Basic usage
find [<command options>] <path where you want to search> <search options>
# Add single quotes when using patterns
$ find . -name '*.pdf'
./input/test2.pdf
./input/test3.pdf
You can pipe standard error to hide “Permission denied” errors:
find . -name '*.pdf' 2>/dev/null
Command options
-L
: follow symlinks.
Search options
-name <some word>
: search by word, you can use wildcards (like*
, remember to enclose the pattern in single quotes).-iname <some word>
: case insensitive search.-size <bytes or number and 'b', 'k', 'M', 'G',...>
: search based on file size. You can add+
or-
for “more than” or “less than”.
# find files with a size of more than 10MB
find . -size +10M
- Bear in mind that file size is rounded up to the next unit.
# this will only find empty files
find . -size -1M
# show filesize
find . -size +100M -exec ls -sh {} \;
-mmin <minutes>
: files that were last modified in less, more or exactly the specified minutes (use+
or-
for “more than” and “les than”). You can use-mtime <days>
as well.-amin <minutes>
: files that were accessed in the last specified minutes. You can use-atime <days>
as well.-exec <command>
: execute a command with every result. Use{}
where you would put the filename and end the command with\;
or+
(+
can be faster with some commands because makesfind
to only run<command>
one time appending every result as an argument. Commands that don’t allow several arguments will not work with+
).
# this will remove all txt files in current directory (note the space after {})
find . -name '*.txt' -exec rm {} \;
# difference between + and \;
$ time find . -type f -exec du -a {} + > /dev/null
real 0m0.015s
user 0m0.008s
sys 0m0.006s
$ time find . -type f -exec du -a {} \; > /dev/null
real 0m0.829s
user 0m0.624s
sys 0m0.213s
-type d
: search only for folders.-type f
: search only for files.-maxdepth <number>
: limit the depth of your search. For example,-maxdepth 1
only searches at the current level. Use this parameter before any other.-not
: search for the inverse of a parameter placed after this parameter.-not -path <path>
: exclude directories.
find . -mmin 5 -not -path "*cache/*" -not -path "*config/*"
-user <username>
: search files/folders owned by a user.-executable
: search files/folders that the current user can execute.-writable
: files/folders writable by the user.
Operators
You can add operators (AND, OR, NOT,…) when using two or more search options. Without operators, defaults to ‘AND’.
-o
: ‘OR’ operator.
find . -name '*.mp3' -o -name '*.jpg'
\!
: ‘NOT’ operator.-not
: similar to\!
but is not POSIX compliant.-a
: ‘AND’.
More examples
- Find files by type (audio, video, image, etc.):
find . -type f -exec file --mime-type {} \; | awk -F ':' '$2 ~ /video/ {print}'
- Find empty directories/files
find . -type d -empty
# '-type f' to find files
Test with this online terminal:
If you have any suggestion, feel free to contact me via social media or email.
Latest tutorials and articles:
Featured content: