{"id":49,"date":"2022-03-19T21:14:00","date_gmt":"2022-03-20T01:14:00","guid":{"rendered":"http:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/?p=49"},"modified":"2025-08-11T12:23:21","modified_gmt":"2025-08-11T16:23:21","slug":"using-the-find-command","status":"publish","type":"post","link":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/?p=49","title":{"rendered":"using the &#8216;find&#8217; command"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code><strong>Using the find command<\/strong><br>find ~\/bin -iname filename<\/code><\/pre>\n\n\n\n<p>or:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>find ~\/bin -iname \"filen*\"<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>Moving large number of files<\/strong><br>find \/source\/directory -mindepth 1 -maxdepth 1 -name '*' -print0 | xargs -0 mv -t \/target\/directory;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>using find with rename recursively<\/strong><br>find . -depth -exec rename -v 's!\\texttoremove\/newtext\/' {} +<br>you may have to run it twice if the subfolders get renamed in the process<br>or:<br>find . -type f -iname \"Ghost*\" -exec rename -v 's\/S02\\ E\/S02E\/g' {} \\;<\/code><\/pre>\n\n\n\n<p><strong>move files from subfolders to parent folder<\/strong><\/p>\n\n\n\n<p>Move to target folder and execute:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>find . -mindepth 2 -type f -print -exec mv {} . \\;<\/code><\/pre>\n\n\n\n<p><strong>Delete empty directories<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>find . -empty -type d -delete<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Delete empty files<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>find . -empty -type f -delete<\/code><\/pre>\n\n\n\n<p><strong>Command<\/strong>\nfind \/tmp -name &#8220;foo.txt&#8221; ##Find a file a called foo.txt in \/tmp\nfind \/tmp -iname &#8220;foo.txt&#8221; ##Find a file (case insensitive) called foo.txt in \/tmp\nfind \/tmp -name &#8220;foo*&#8221; ##Find a file starting with the substring foo\nfind \/tmp -regex &#8220;.*f.*t&#8221; ##Find regex pattern (regex must include the full path)<\/p>\n\n\n\n<p><strong>Time<\/strong><br>-mtime -7 ##Modified within the last 7 days<br>-mtime +1 -mtime -7 ##Modified more than 1 day ago, but no more than 7<br>-daystart ##Start from today rather than from 24 hours ago<br><br><strong>Recursion<\/strong><br>-maxdepth levels : Descend at most levels (a non-negative integer) levels of directories below the starting-points. -maxdepth 0 means only apply the tests and actions to the starting-points themselves.<br>-mindepth levels : Do not apply any tests or actions at levels less than levels (a non-negative integer). -mindepth 1 means process all files except the starting-points.<br><br>-maxdepth 2 ##Go no more than 2 subdirectories deep during search<br>-mindepth 4 ##Ignore results that are less than 4 subdirectories deep<br>-mount -xdev ##Don&#8217;t search directories contained on another filesystem<br><br><strong>Other attributes<\/strong><br>-uid 1000User ID is 1000<br>-user tux User name is tux<br>-writable -readable File is writable, readable<br>-perm u=rwx -perm 700Permissions are exactly 700<br>-perm -u+w,g+w -perm -220User or group has write permission &#8211;<br>-perm \/a+w -perm \/222 At least one permission is set to write \/<br>-size +5M File is larger than 5 MB<br>-true Always true<br><br><strong>Actions<\/strong><br>-exec grep foo {} \\; Execute grep on each file found<br>-ok sed &#8216;s\/foo\/bar\/g&#8217; {} \\; Prompt user to execute sed on each file found<br>-execdir chmod 700 {} \\; Run chmod (in subdirectory of result) on each file found<br>-fprint Add a newline to output -fprint0Do not add a newline<br>-ls Print results in ls -dils format<br>-fls output.txt Write results, in ls -dils frormat, to output.txt<br>-fprint -fprint0Write output to out.txt &#8230;with no newline<br>-prune Don&#8217;t descend into subdirectories<br>-quit Quit (usually used after other actions)\u00a0<\/p>\n\n\n\n<p><strong>Find multiple files in Linux<\/strong><\/p>\n\n\n\n<p>The <code>find<\/code> command is used in various ways. One thing you don&#8217;t want to do as a system administrator is work harder than you need to. Instead of running the same command to search for one file over and over, you can use the <code>find<\/code> command to locate multiple files at the same time.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo find \/home -type f -name file.txt -exec {} \\;<\/pre>\n\n\n\n<p>This one-liner can be broken down. I find it best almost to read it as a sentence:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>searching the <code>\/home<\/code> directory<\/li>\n\n\n\n<li>searching for a file (<code>-type f<\/code>) or a directory (<code>-type d<\/code>)<\/li>\n\n\n\n<li>filename is file.txt (<code>-name file.txt<\/code>)<\/li>\n\n\n\n<li>executing another command from the previous output<\/li>\n<\/ul>\n\n\n\n<p><strong>Find large files in Linux<\/strong><\/p>\n\n\n\n<p>You can also use <code>find<\/code> to discover large files in Linux. Finding large files has proven helpful to me in the long run. <code>find<\/code> can help to locate large files quickly, such as backups and ISO files.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo find \/ -type f -size +500000k -exec ls -lh {} \\;<\/code><\/pre>\n\n\n\n<p>This one-liner can be broken down:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>searching the <code>\/<\/code> directory<\/li>\n\n\n\n<li>searching for a file (<code>type -f<\/code>)<\/li>\n\n\n\n<li>searching for a file larger than <code>500000k<\/code><\/li>\n\n\n\n<li>executing the command <code>ls -lh<\/code> on the files found in the previous output<\/li>\n<\/ul>\n\n\n\n<p><strong>Find specific file types in Linux<\/strong><\/p>\n\n\n\n<p>Another good method is to locate file extensions using the <code>find<\/code> command. I find this helpful, as it has shown me ways of finding specific files with only a specific keyword. In this case, the example below is looking for files that only contain a specific extension:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo find \/ -type f \\( -name \"*.sh\" -o -name \"*.txt\" )<\/code><\/pre>\n\n\n\n<p>To dissect this:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>searching in the <code>\/<\/code> directory<\/li>\n\n\n\n<li>searching for a file (<code>-type f<\/code>) or a directory (<code>-type d<\/code>)<\/li>\n\n\n\n<li>searching for a file name that is a wildcard but ends with the extension <code>.sh<\/code> or <code>.txt<\/code><\/li>\n<\/ul>\n\n\n\n<p><strong>Find modified files in Linux<\/strong><\/p>\n\n\n\n<p>The last example shows how to <code>find<\/code> a file modified in the last 50 days. This can be helpful when you need to locate recently modified files due to a security reason or if there are unwanted users on the network accessing other files.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo find \/ -type f -ctime +50 -exec rm -f {} \\;<\/code><\/pre>\n\n\n\n<p>The command above shows:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>searching in the <code>\/<\/code> directory<\/li>\n\n\n\n<li>searching for a file (<code>-type f<\/code>) or a directory (<code>-type d<\/code>)<\/li>\n\n\n\n<li>searching for files older than <code>50<\/code> days<\/li>\n\n\n\n<li>executing the command <code>rm -f<\/code> on the files found in the previous output<\/li>\n<\/ul>\n\n\n\n<p>This can help remove those malicious files all in one go. You just have to make sure that the files you select are the files you want to remove. One way to check is to run the command without the <code>exec<\/code> section to see the files that come up in the output. If there are a large number of files, redirect the output into a file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>find \/ -type f -ctime +50 &gt; files.txt<\/code><\/pre>\n\n\n\n<p>The content can be reviewed and verified before you run a one-liner that removes the <code>\/etc<\/code> folder. Not ideal.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> check out:   find . -criteria etc -print0 | xargs -0 command\n(might be safer than -exec stuff)<\/pre>\n\n\n\n<p><strong>List Symlinks<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo find \/ -type l<\/code><\/pre>\n\n\n\n<p><strong>Finding files by size<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>find -size +1G -ls 2&gt;\/dev\/nullthe +1G means \"larger than a gigabyte\"<\/code><\/pre>\n\n\n\n<p><strong>Finding files by inode number<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>find -inum 919674 -ls 2&gt;\/dev\/null919674 is the inode number<\/code><\/pre>\n\n\n\n<p><strong>Finding files with a specific file owner or group<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>find \/home -user bob -name \"*.png\"-lsfind \/tmp -group admins -ls<\/code><\/pre>\n\n\n\n<p><strong>Finding files with no owners or groups<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>find \/tmp -nouser -ls<\/code><\/pre>\n\n\n\n<p><strong>Finding files by last update time<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>find \/home\/bob -mtime -1<\/code><\/pre>\n\n\n\n<p><strong>Finding files by when permissions were last changed<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>find . -ctime -1 -ls<\/code><\/pre>\n\n\n\n<p><strong>Finding files based on last access times<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>find -name \"*.pdf\" -atime -2<\/code><\/pre>\n\n\n\n<p><strong>Finding files based on their age relative to another file<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>find . -newer dig1 -ls<\/code><\/pre>\n\n\n\n<p><strong>Finding files by type<\/strong><br>b block (buffered) special<\/p>\n\n\n\n<p>c character (unbuffered) special<\/p>\n\n\n\n<p>d directory<\/p>\n\n\n\n<p>p named pipe (FIFO)<\/p>\n\n\n\n<p>f regular file<\/p>\n\n\n\n<p>l symbolic link<\/p>\n\n\n\n<p>s socket<\/p>\n\n\n\n<p><br>find . -type l -ls<\/p>\n\n\n\n<p><strong>Limiting how deeply find should look<\/strong><\/p>\n\n\n\n<p>The -mindepth and -maxdepth options control how deeply into the file system the search will look (from the current location or starting point).<br>find -maxdepth 3 -name &#8220;*loop&#8221;<\/p>\n\n\n\n<p><br><strong>Finding files only if empty<\/strong><\/p>\n\n\n\n<p>find . -maxdepth 2 -empty -type f -ls<\/p>\n\n\n\n<p><strong>Finding files by permissions<\/strong><\/p>\n\n\n\n<p>find -perm 777 -type f -ls<\/p>\n\n\n\n<p><strong>Using find to help you get rid of files<\/strong><\/p>\n\n\n\n<p>find . -name filename -exec rm {} \\;<\/p>\n\n\n\n<p>The {} represents the name of each of the files located by the search criteria.<\/p>\n\n\n\n<p>replace -exec with -ok if you want it to ask for a confirmation before it removes any file.<\/p>\n\n\n\n<p>find . -name runme -ok rm -rf {} \\;<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>or: move files from subfolders to parent folder Move to target folder and execute: Delete empty directories Delete empty files Command find \/tmp -name &#8220;foo.txt&#8221; ##Find a file a called foo.txt in \/tmp find \/tmp -iname &#8220;foo.txt&#8221; ##Find a file (case insensitive) called foo.txt in \/tmp find \/tmp -name &#8220;foo*&#8221; ##Find a file starting with [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-49","post","type-post","status-publish","format-standard","hentry","category-file-management"],"_links":{"self":[{"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/49","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=49"}],"version-history":[{"count":5,"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/49\/revisions"}],"predecessor-version":[{"id":308,"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/49\/revisions\/308"}],"wp:attachment":[{"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=49"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=49"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=49"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}