{"id":4159,"date":"2019-09-14T11:33:04","date_gmt":"2019-09-14T08:33:04","guid":{"rendered":"https:\/\/kifarunix.com\/?p=4159"},"modified":"2024-03-12T21:56:23","modified_gmt":"2024-03-12T18:56:23","slug":"using-find-command-to-search-for-files-and-directories-in-linux","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/using-find-command-to-search-for-files-and-directories-in-linux\/","title":{"rendered":"Using Find Command to Search for Files and Directories in Linux"},"content":{"rendered":"\n<p>Welcome to our guide on using find command to search for files and directories in Linux. <code>find<\/code>&nbsp;is a command-line utility that searches one or more directory trees of a file system, locates files based on some user-specified criteria and applies a user-specified action on each matched file. It can also be use with other Linux\/Unix commands to apply further actions on files or directories found.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to Use Find Command in Linux<\/h2>\n\n\n\n<p>We will walk through various examples on how to search for files and directories in Linux using find command.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Search for Files Using Find command<\/h3>\n\n\n\n<p>To search for a file using find command, you need to speficy the directory where to search. For example, to search for a file called&nbsp;<code>file.txt<\/code>&nbsp;on the directory,&nbsp;<code>\/home\/user<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>find \/home\/user -name file.txt<\/code><\/pre>\n\n\n\n<p>To explicitly specify that the file you are searching for is a file, use&nbsp;<code>-type f<\/code>&nbsp;where&nbsp;<code>f<\/code>&nbsp;specifies that what is being searched for should be a file.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>find \/home\/user -name file.txt -type f<\/code><\/pre>\n\n\n\n<p>To search for all files with the extensions&nbsp;<code>.txt<\/code>&nbsp;on the directory&nbsp;<code>\/<\/code>;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>find \/r -name \"*.txt\" -type f<\/code><\/pre>\n\n\n\n<p>To search for all the files that begins with the keyword&nbsp;<code>foo<\/code>&nbsp;in the current directory;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>find . -name \"foo.*\" -type f<\/code><\/pre>\n\n\n\n<p>To search for files in multiple directories;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>find \/usr \/etc \/home\/user -name file.txt -type f<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Search for Directories Using Find Command<\/h3>\n\n\n\n<p>To search for directiries, use&nbsp;<code>-type d<\/code>. For example to search for directory with the name&nbsp;<code>squid<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>find \/ -name squid -type d<\/code><\/pre>\n\n\n\n<p>To search for directories with the keyword&nbsp;<code>php<\/code>;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>find \/ -name \"php*\" -type d<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Search for Empty Files and Directories Using Find Command<\/h3>\n\n\n\n<p>To search for empty files in the root file system tree;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>find \/ -type f -empty<\/code><\/pre>\n\n\n\n<p>To search for empty directories in the root file system tree;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>find \/ -type d -empty<\/code><\/pre>\n\n\n\n<p>To search for all files beginning with the keyword <code>php<\/code> and are empty;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>find \/ -name \"php*\" -type f -empty<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Perform Case Insensitive Search Using Find Command<\/h3>\n\n\n\n<p>To perform a case insensitive search using find command, use the&nbsp;<code>-iname<\/code>&nbsp;option.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>find . -iname file.txt<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>find \/ -iname file.txt -type f<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>find \/etc -iname txt-file -type d<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Find and Process Files and Directories<\/h3>\n\n\n\n<p>To find and process files using find command, use the&nbsp;<code>-exec<\/code>&nbsp;option. The&nbsp;<code>-exec<\/code>&nbsp;option enables you to run other commands against all the files returned by your current find term.<\/p>\n\n\n\n<p>For example, to find all files that ends with&nbsp;<code>.sh<\/code>&nbsp;extension in the current directory and make them executable;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>find . -name \"*.sh\" -type f -exec chmod +x {} \\;<\/code><\/pre>\n\n\n\n<p>It is however much more secure and recommended to use the&nbsp;<code>-execdir<\/code>&nbsp;option which run the specified command in the subdirectory containing the matched file instead of the directory in which you started find.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>find . -name \"*.sh\" -type f -execdir chmod +x {} \\;<\/code><\/pre>\n\n\n\n<p>To remove all empty files in the current directory;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>find . -type f -empty -exec rm -rf {} \\;<\/code><\/pre>\n\n\n\n<p>To remove all empty directories in the current directory;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>find . -type d -empty -exec rmdir {} \\;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Find and Delete Files and Directories Using Find Command<\/h3>\n\n\n\n<p>You can find and delete files and directories using the&nbsp;<code>-delete<\/code>&nbsp;option.<br>For example, to delete empty directories on the current directory.<\/p>\n\n\n\n<p>Use this option when you are certain of the matches as this is similar to executing the&nbsp;<code>rm -rf<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>find . -type d -empty -delete<\/code><\/pre>\n\n\n\n<p>To find and delete all files with the&nbsp;<code>.txt<\/code>&nbsp;extension,<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>find . -name \"*.txt\" -type f -empty -delete<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Find files by Modification time<\/h3>\n\n\n\n<p>Use the&nbsp;<code>-newerXY<\/code>&nbsp;option to find file change date.<\/p>\n\n\n\n<p>Where&nbsp;<code>XY<\/code>&nbsp;can be any of the;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>a<\/strong>&nbsp;&#8211; The access time of the file reference<\/li>\n\n\n\n<li><strong>B<\/strong>&nbsp;&#8211; The birth time of the file reference<\/li>\n\n\n\n<li><strong>c<\/strong>&nbsp;&#8211; The inode status change time of reference<\/li>\n\n\n\n<li><strong>m<\/strong>&nbsp;&#8211; The modification time of the file reference<\/li>\n\n\n\n<li><strong>t<\/strong>&nbsp;&#8211; reference is interpreted directly as a time<\/li>\n<\/ul>\n\n\n\n<p>The syntax is as follows:<\/p>\n\n\n\n<p><code>find \/dir\/ -type f -newerXY 'yyyy-mm-dd'<\/code><\/p>\n\n\n\n<p>To find and list the files;<\/p>\n\n\n\n<p><code>find \/dir\/ -type f -newerXY 'yyyy-mm-dd' -ls<\/code><\/p>\n\n\n\n<p>Let us create three files and directories for example, with custom modification time;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>touch -d \"2019-01-01\" testfile1\ntouch -d \"2019-03-01\" testfile1\ntouch -d \"2019-05-01\" testfile1<\/code><\/pre>\n\n\n\n<p>Find files modified on&nbsp;<code>2019-01-01<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>find . -type f -newermt 2019-01-01<\/code><\/pre>\n\n\n\n<p>Find files modified between&nbsp;<code>2019-01-01<\/code>&nbsp;and&nbsp;<code>2019-05-01<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>find . -type f -newermt 2019-01-01 ! -newermt 2019-05-01<\/code><\/pre>\n\n\n\n<p>Find files accessed on&nbsp;<code>2019-05-01<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>find . -type f -newerat 2019-05-01<\/code><\/pre>\n\n\n\n<p>Find the files accessed 7 days ago.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>find \/etc -iname \"*.conf\" -atime -7 -type f<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>find \/etc -type d -atime -7<\/code><\/pre>\n\n\n\n<p>or<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>find \/etc -iname \"*.conf\" -atime -7 -type f -ls<\/code><\/pre>\n\n\n\n<p>Find files accessed more than 7 days ago<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>find \/etc -iname \"*.conf\" -atime +7 -type f<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>find \/etc -type d -atime +7<\/code><\/pre>\n\n\n\n<p>or<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>find \/etc -iname \"*.conf\" -atime +7 -type f -ls<\/code><\/pre>\n\n\n\n<p>List all the files accessed exactly 7 days ago.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>find \/etc -iname \"*.conf\" -atime 7 -type f<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>find \/etc -iname \"*.conf\" -atime 7 -type f -ls<\/code><\/pre>\n\n\n\n<p>List all the directories accessed exactly 7 days ago.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>find \/etc -type d -atime 7<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Find files that don\u2019t match a pattern<\/h3>\n\n\n\n<p>Use the&nbsp;<code>-not<\/code>&nbsp;argument of the find command to find all files that don\u2019t match a filename pattern.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>find . -type f -not -name \"*.txt\"<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Find All Files of a Particular Size<\/h3>\n\n\n\n<p>To locate files with specific sizes, use the&nbsp;<code>-size<\/code>&nbsp;argument. The units that can be used to specify the sizes;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>b \u2013 for 512-byte blocks (this is the default if no suffix is used)<\/li>\n\n\n\n<li>c \u2013 for bytes<\/li>\n\n\n\n<li>w \u2013 for two-byte words<\/li>\n\n\n\n<li>k \u2013 for Kilobytes (units of 1024 bytes)<\/li>\n\n\n\n<li>M \u2013 for Megabytes (units of 1048576 bytes)<\/li>\n\n\n\n<li>G \u2013 for Gigabytes (units of 1073741824 bytes<\/li>\n<\/ul>\n\n\n\n<p>For example, to locate all the files with&nbsp;<code>10 bytes<\/code>&nbsp;in the current directory;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>find . -type f -size 10c -exec ls {} \\;<\/code><\/pre>\n\n\n\n<p>For directories;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>find . -type d -size +10c<\/code><\/pre>\n\n\n\n<p>To find all the files and directories with more than 10 Kilobytes;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>find . -type f -size +10k -exec ls -lh {} \\;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>find \/ -type d -size +10k -exec ls -alhd {} \\;<\/code><\/pre>\n\n\n\n<p>Find files or directories that are less than 10k;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>find . -type f -size -10k -exec ls {} \\;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>find \/ -type d -size -10k -exec ls -alhd {} \\;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Find Files Based On their Permissions<\/h3>\n\n\n\n<p>find any file with group write permission;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>find -perm -g=w<\/code><\/pre>\n\n\n\n<p>Note that you can use&nbsp;<code>+<\/code>&nbsp;instead of&nbsp;<code>=<\/code>.<\/p>\n\n\n\n<p>Find files writable by the file owner;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>find -perm -u=w<\/code><\/pre>\n\n\n\n<p>Find file which are writable by all (owner, group, world);<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>find -perm -a=w<\/code><\/pre>\n\n\n\n<p>Find files which are writable by both their owner and their group;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>find -perm -g+w,u+w<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Find Files Based On octal Permissions<\/h3>\n\n\n\n<p>Find files with read, write, execute permissions for owner, group and world (777).<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>find -perm 777<\/code><\/pre>\n\n\n\n<p>Find files with read and write permissions for owner and group<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>find -perm 550<\/code><\/pre>\n\n\n\n<p>To locate files in current directory that are not owned by any user;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>find . -type f -nouser<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Locate Programs with SUID\/SGID Permissions set<\/h3>\n\n\n\n<p>Set User ID and Set Group ID are Linux permissions that are applied to executable programs. This sometimes poses a security threat in the sense that it causes the system to treat programs with these bits set to be executed with the permissions of the program owner (SUID) or group owner (SGUID) rather than with the permissions of the user running the program itself.<\/p>\n\n\n\n<p>Similarly, you can locate these programs using find command with the <code>-perm mode<\/code> option.<\/p>\n\n\n\n<p>For example, to search for all the file with SUID and SGID bits set, run the command below;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>find \/ -perm +6000 -type f<\/code><\/pre>\n\n\n\n<p>Well, we have so far covered few examples of using find command to search for directories and files on Linux.<\/p>\n\n\n\n<p>Read more on find command manual pages.<\/p>\n\n\n\n<p><code><a href=\"http:\/\/man7.org\/linux\/man-pages\/man1\/find.1.html\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"man find (opens in a new tab)\">man find<\/a><\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Other Tutorials<\/h3>\n\n\n\n<p><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/kifarunix.com\/extract-log-lines-of-specific-dates-from-a-log-file\/\" target=\"_blank\">Extract Log Lines of Specific Dates from a Log File<\/a><\/p>\n\n\n\n<p><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/kifarunix.com\/delete-lines-matching-a-specific-pattern-in-a-file-using-sed\/\" target=\"_blank\">Delete Lines Matching a Specific Pattern in a File using SED<\/a><\/p>\n\n\n\n<p><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/kifarunix.com\/delete-lines-matching-specific-pattern-in-a-file-using-vim\/\" target=\"_blank\">Delete Lines Matching Specific Pattern in a File using VIM<\/a><\/p>\n\n\n\n<p><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/kifarunix.com\/how-to-install-and-use-7zip-on-ubuntu-18-04-command-line\/\" target=\"_blank\">How to Install and Use 7zip File Archiver on Ubuntu 18.04<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/viewing-system-processes-using-ps-and-top-commands\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">Viewing System Processes using ps and top commands<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Welcome to our guide on using find command to search for files and directories in Linux. find&nbsp;is a command-line utility that searches one or more<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[121,1119,49],"tags":[1120,1121,1122],"class_list":["post-4159","post","type-post","status-publish","format-standard","hentry","category-howtos","category-find","category-command-cheatsheets","tag-find","tag-find-command","tag-search-using-find","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50"],"_links":{"self":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/4159"}],"collection":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/comments?post=4159"}],"version-history":[{"count":3,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/4159\/revisions"}],"predecessor-version":[{"id":21197,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/4159\/revisions\/21197"}],"wp:attachment":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media?parent=4159"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=4159"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=4159"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}