find¶
find¶
find command allows a quick and easy scan through the file system and perform tasks on the returned results.
Find for files and folders¶
find .- Lists all the files and folders in the current directory, and also returns the files and folders from all subfolders too
find dir_name/- Works similar to
find .but only lists the files and folders from the directory nameddir_name
- Works similar to
Find only files or folders¶
find . -type d- Works similar to
find .but only returns the directories and not the files (works recursively)
- Works similar to
find . -type f- Works similar to
find .but only returns the files and not the directories (works recursively)
- Works similar to
Find by name¶
find . -type f -name "file_name"- Will search recursively in the current directory through the files and try to locate the file named
file_name - if something like
file_*is used instead offile_name, all files with the prefixfile_will be returned (handy in case the exact file name is not known) - the
-nameoption is case sensitive - using the file name as
*.pywill return all the files with the extension.py
- Will search recursively in the current directory through the files and try to locate the file named
find . -type f -iname "file_name"- Performs the same search as the previous command but in a case insensitive fashion
Find by last modified, last accessed and changed time¶
find . -type f -mmin -10- Finds all the files whose contents have been modified in the last 10 minutes
mminrefers to modified in minutes-10refers to less than 10 minutes+10refers to greater than 10 minutes
find . -type f -mmin +5 -min -10- Demonstrates how to combine the queries
- here we search for all the files that have been modified in the last 5-10 minutes
find . -type f -mtime -1- Will return all the files modified in the last 1 day
-mtimerefers to the units in days- Similar to
-mmin,+1will return files modified more than a day back
- Other flags
aminandatimerefer to the last access time in minutes and days respectivelycminandctimerefer to the last changed time in minutes and days respectively; changed refers to changes in both metadata and content
Find by file size¶
find . -size +5M- Finds all the files that have file size upwards of 5 MB
-sizeis the flag to search by file size- The syntax is simlar to the find by last modified where
+refers to more than while-is for less than +5Mis greater than 5 Megabyteskcan be used for kilobytesGcan be used for Gigabytes
Logical Operators¶
- In all the earlier examples, specifying multiple flags together implicitly means using the
andoperator andandoroperators are available as the-aand-oswitch()can also be used to group certain flags, but they must be enclosed in''or""since they are special characters in linuxfind . -type f "(" -mmin -5 -o -mtime +1 ")"- finds all files
andonly those that were modified(either less than 5 minutes agoormore than a day ago) find . -type f -a "(" -mmin -5 -o -mtime +1 ")"is an equivalent formulation since the-ais redundant
- finds all files
Find all empty files¶
find . -empty- Will return all empty files and there is no need to apply the file flag
Find by permission¶
find . -perm 777- Finds all the files and folders with the given permissions in the
777format
- Finds all the files and folders with the given permissions in the
Controlling the depth of traversal¶
-maxdepthflag is useful when we wish to find the files till a certain level of depthfind . -type f -maxdepth 1- Will find all the files in the current directory only
find . -type f -maxdepth 2on the other hand searches both the current directory and only the subdirectories (not the directories inside them)- not specifying this flag (as done in earlier examples) means searching the entire depth of the directory
- Similarly
-mindepthwill start the search at the specified minimum depth
Executing commands¶
- Commands can be executed on files with the
-execflag find . -type f -name "*.md" -exec head {} ';'- Will execute the
headcommand on all the files that are found {}refer to the placeholder that will contain the entire file path on which to execute the command';'terminates the command;'are used since;is also a special character in linux
- Will execute the
- In addition to ending the command with
;, we also have the+option when we wish to pass in the list of files to a command find . -type f -name "*.md" -exec tar -czf md_files.tar.gz {} '+'- passes all the files of extension
.mdto thetarcommand
- passes all the files of extension
- Similar to
-exec, there is-execdirto execute commands on directories with the;and+options available