{"id":809,"date":"2021-01-26T00:19:19","date_gmt":"2021-01-26T00:19:19","guid":{"rendered":"https:\/\/dft.wiki\/?p=809"},"modified":"2026-01-11T15:44:10","modified_gmt":"2026-01-11T20:44:10","slug":"using-find-in-linux-cli","status":"publish","type":"post","link":"https:\/\/dft.wiki\/?p=809","title":{"rendered":"Using Find in Linux CLI"},"content":{"rendered":"<p>Find is a very powerful tool for searching for files and directories in a command-line interface that can also perform tasks to the findings.<\/p>\n<p>See examples:<\/p>\n<pre>find .\r\nfind directory<\/pre>\n<p>Defining type: directory, file, or symbolic link<\/p>\n<pre>find . -type d\r\nfind . -type f\r\nfind . -type l<\/pre>\n<p>Name pattern, wildcard, case insensitive, and user:<\/p>\n<pre>find . -type f -name \"file.txt\"\r\nfind . -type f -name \"file*\"\r\nfind . -type f -iname \"file*\"\r\nfind . -type f -i -name \"*.py*\"\r\nfind . -user root\r\nfind . ! -user root<\/pre>\n<p>Find file modified less than 10 minutes ago, more than 10 minutes ago, and in a period:<\/p>\n<pre>find . -type f -mmin -10\r\nfind . -type f -mmin +10\r\nfind . -type f -mmin +10 -mmin -5<\/pre>\n<p>For days instead of minutes:<\/p>\n<pre>find . -type f -mtime +5<\/pre>\n<p>For printing the size of the file:<\/p>\n<pre>find . -type f -mmin +30 -exec ls -lh {} \\; | awk '{print $5 \" \" $9}'<\/pre>\n<p>Based on a specific size rule:<\/p>\n<pre>find . -size +5k\r\nfind . -size +5M\r\nfind . -size +5G\r\nfind . -empty<\/pre>\n<p>Based on permission than meet criteria:<\/p>\n<pre>find . -perm 777<\/pre>\n<p>Performing action on the results:<\/p>\n<pre>find . -exec chown user: {} +\r\nfind . -exec chown user: {} \\;\r\nfind . -exec chmod 775 {} +\r\nfind . -perm 664\r\nfind . -exec rm {} +\r\nfind . -delete\r\nfind . -print<\/pre>\n<p>Applying max depth for the recursive finding:<\/p>\n<pre>find . -maxdepth 1<\/pre>\n<p>Counting results:<\/p>\n<pre>find . | wc -l\r\nfind . | wc -w\r\nfind . | wc -m\r\nfind . | wc -c<\/pre>\n<p>Printing file: user, type, size, name, and last modified<\/p>\n<pre>find . -printf \"%u %y %s %p %t \\n<\/pre>\n<p>Searching for files that contain the &#8220;<strong>text<\/strong>&#8220;:<\/p>\n<pre>find . -type f -exec grep -H '<strong>text<\/strong>' {} \\; -print 2&gt;\/dev\/null<\/pre>\n<p>Find files modified in the last 5 minutes:<\/p>\n<pre>find . -type f -mmin -5<\/pre>\n<p>Finding hidden files:<\/p>\n<pre>find . -type f -iname \".*\"\r\nfind . -type f -iname \".*\" -ls<\/pre>\n<p>Simply find files with the name of the tree directory tree and above:<\/p>\n<pre>find | grep file_name<\/pre>\n<p>Search, count, and calculate the total weight of files sorted by extension:<\/p>\n<pre>find . -name '?.' -type f -printf '%b.%f\\0' | awk -F . -v RS='\\0' '{s[$NF] += $1; n[$NF]++} END {for (e in s) printf \"%15d %4d %s\\n\", s[e]*512, n[e], e}' | sort -n<\/pre>\n<hr \/>\n<p><strong>BONUS<\/strong><\/p>\n<p>Unrelated but in the same realm of finding files or some sort, <code>rdfind<\/code> can find duplicated files fast and efficiently [<a href=\"https:\/\/github.com\/pauldreik\/rdfind\">Link<\/a>].<\/p>\n<pre>sudo apt install rdfind -y\r\nrdfind ~\/Documents<\/pre>\n<p>It will generate a report of all files that were found to be duplicated using the following strategies:<\/p>\n<ul>\n<li>Unique inodes,<\/li>\n<li>File size\n<ul>\n<li>Hash just the first few bites,<\/li>\n<li>Hash just the last few bites,<\/li>\n<li>Only then, hash the whole file.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>Clean-ups can be performed by replacing identical files with hard links or deleting them.<\/p>\n<p>Dry-run<\/p>\n<pre>rdfind -dryrun true ~\/Documents<\/pre>\n<p>Hard-links<\/p>\n<pre>rdfind -makehardlinks true ~\/Documents<\/pre>\n<p>Deletion<\/p>\n<pre>rdfind -deleteduplicates true ~\/Documents<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Find is a very powerful tool for searching for files and directories in a command-line [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,6],"tags":[],"class_list":["post-809","post","type-post","status-publish","format-standard","hentry","category-linux","category-raspberry-pi"],"_links":{"self":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/809","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=809"}],"version-history":[{"count":13,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/809\/revisions"}],"predecessor-version":[{"id":5227,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/809\/revisions\/5227"}],"wp:attachment":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=809"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=809"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=809"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}