{"id":966,"date":"2024-12-27T21:40:11","date_gmt":"2024-12-27T21:40:11","guid":{"rendered":"https:\/\/w3buddy.com\/?p=966"},"modified":"2026-01-15T13:15:43","modified_gmt":"2026-01-15T07:45:43","slug":"understanding-the-find-command","status":"publish","type":"post","link":"https:\/\/w3buddy.com\/blog\/understanding-the-find-command\/","title":{"rendered":"Understanding the Find Command"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Learn how to efficiently search and manage files with the <code>find<\/code> command. This post covers essential examples for finding, deleting, and compressing files based on various criteria like name, size, and modification time.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Generalized Syntax for find Command:<\/h2>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\">find [path] [conditions] [actions]<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>[path]:<\/strong> Directory to start the search (e.g., . for current directory, \/path\/to\/directory for a specific path).<\/li>\n\n\n\n<li><strong>[conditions]:<\/strong> Criteria to filter files (e.g., -name &#8220;*.log&#8221; for files with a specific name, -mtime +7 for files older than 7 days).<\/li>\n\n\n\n<li><strong>[actions]:<\/strong> What to do with the found files (e.g., -exec rm {} \\; to delete files, -exec gzip {} \\; to compress files).<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Example Commands:<\/h2>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\"># 1. Delete .trc files older than 1 day\nfind . -name \"*.trc\" -type f -mtime +1 -exec rm {} \\;\n\n# 2. Compress .log files older than 10 days\nfind . -name \"*.log\" -type f -mtime +10 -exec gzip {} \\;\n\n# 3. Delete empty files (size 0)\nfind . -type f -size 0 -exec rm {} \\;\n\n# 4. Compress files larger than 10MB\nfind . -type f -size +10M -exec gzip {} \\;\n\n# 5. Find files modified in the last 5 days\nfind . -type f -mtime -5 -print\n\n# 6. Delete .arc files older than 30 days in a specific directory\nfind \/path\/to\/directory -name \"*.arc\" -type f -mtime +30 -exec rm {} \\;\n\n# 7. Find and print the 5 largest files modified in the last day\nfind . -type f -mtime -1 -printf \"%p %s\\n\" | sort -k2nr | head -5\n\n# 8. Delete all .gz files older than 7 days\nfind . -name \"*.gz\" -type f -mtime +7 -exec rm {} \\;\n\n# 9. Compress .trm files older than 0 days (immediate compression)\nfind . -name \"*.trm\" -type f -mtime +0 -exec gzip {} \\;\n\n# 10. Delete files larger than 100MB\nfind . -type f -size +100M -exec rm {} \\;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This provides a concise overview of the <code>find<\/code> command with examples for common use cases, including deleting, compressing, and filtering files based on different criteria.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to efficiently search and manage files with the find command. This post covers essential examples for finding, deleting, and compressing files based on various criteria like name, size, and modification time. Generalized Syntax for find Command: Example Commands: This provides a concise overview of the find command with examples for common use cases, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"googlesitekit_rrm_CAowu461DA:productID":"","footnotes":""},"categories":[1225],"tags":[],"class_list":["post-966","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/posts\/966","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/comments?post=966"}],"version-history":[{"count":1,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/posts\/966\/revisions"}],"predecessor-version":[{"id":967,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/posts\/966\/revisions\/967"}],"wp:attachment":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/media?parent=966"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/categories?post=966"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/tags?post=966"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}