{"id":34,"date":"2004-05-01T00:04:06","date_gmt":"2004-04-30T22:04:06","guid":{"rendered":"http:\/\/frenchfragfactory.net\/ozh\/archives\/2004\/05\/01\/find-one-liners\/"},"modified":"2007-05-10T00:02:08","modified_gmt":"2007-05-09T22:02:08","slug":"find-one-liners","status":"publish","type":"post","link":"https:\/\/planetozh.com\/blog\/2004\/05\/find-one-liners\/","title":{"rendered":"Find one liners"},"content":{"rendered":"<p>Collection of one liners using the GNU\/Linux utility &quot;find&quot;. Can&#39;t remember where I found most of them :)<br \/>\n<!--more--><\/p>\n<div class=\"igsh-code-box\" id=\"ig-sh-1\"><pre class=\"language-none line-numbers\" data-no-optimize=\"1\" data-cfasync=\"false\"><code class=\"language-none\"># Find a file and delete it. Replace foo.bar with the file or use wild cards *.bar\r\n$ find \/home -name foo.bar -type f -exec rm -f &quot;{}&quot; &#039;;&#039;\r\n\r\n# When trying to find large files in \/ filesytem\r\n# the find command will return results from\r\n# other files systems. Try using the -dev option.\r\n$ find \/ -dev -size +3000 -exec ls -l {} ; \r\n\r\n# This will not return files in \/usr\/local file system.\r\n# This will return files large then 3000 blocks \r\n# only in the root file system.\r\n$ find \/usr -dev -size +5000 -exec ls -l {} ;\r\n\r\n# Someone&#039;s done a &#039;cp -r&#039; and filled up\r\n# the filesystem. Here&#039;s a simple way to locate the unwanted files...\r\n$ find . -ctime -1 -print\r\n\r\n# Find has a partiularly powerful mechanism, -exec,\r\n# which lets you execute commands on a per-file \r\n# basis, while being selective about the files you&#039;re operating on.\r\n# Lets say we want to delete all the *.bak or *.backup \r\n# files which are older than 30 days.\r\n# Type the following all on 1 line.\r\n$ find . ( -name &#039;*.bak&#039; -o -name *.backup ) -type f -atime +30 -exec rm &#039;{}&#039; ;\r\n\r\n# It would be even better if we only deleted \r\n# the *.bak file if the original file was still \r\n# available. In this case, we can use csh and test to help\r\n# Type the following all on 1 line. \r\n$ find . ( -name &#039;*.bak&#039; -o -name *.backup ) -type f -atime +30 -exec csh -c &#039;if ( -f $1:r ) rm $1&#039; &#039;{}&#039; ;\r\n\r\n# or if you&#039;re really paraniod, use &#039;test -s&#039; \r\n# to verify that the original file has not been truncated\r\n# Type the following all on 1 line. \r\n$ find . -name &#039;*.bak&#039; -type f -atime +30 -exec csh -c &#039;test -s $1:r &amp;&amp; rm $1&#039; &#039;{}&#039; ;\r\n\r\n# Protecting users from themselves - file permissions\r\n$ find . -type l -exec gawk &#039;BEGIN { &quot;ls -lag}&#039; \/dev\/null ;\r\n\r\n# Find text in whole directory tree:\r\n$ find . -type f | xargs grep &quot;text&quot;\r\n\r\n# Find and Replace in whole directory tree:\r\n$ find . -type f [-name &quot;...&quot;] -exec perl -pi -e &#039;s|xxx|yyy|g&#039; {} ;\r\n\r\nfor example:\r\nfind . -type f -name &quot;*html&quot; -exec \r\nperl -pi -e &#039;s|pibeta.psi.ch\/~stefan|midas.psi.ch\/~stefan|g&#039; {} ;\r\n\r\n# Show sorted file size of subtree\r\n$ du -a . | sort -r -n | less<\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Collection of one liners using find<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_ozhmd_markdown":"","footnotes":""},"categories":[],"tags":[5,2,53,3],"class_list":["post-34","post","type-post","status-publish","format-standard","hentry","tag-bash","tag-code","tag-find","tag-linux"],"_links":{"self":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/34","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/comments?post=34"}],"version-history":[{"count":0,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/34\/revisions"}],"wp:attachment":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/media?parent=34"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/categories?post=34"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/tags?post=34"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}