{"id":387,"date":"2014-12-01T10:30:00","date_gmt":"2014-12-01T08:30:00","guid":{"rendered":"http:\/\/www.bashpi.org\/?page_id=387"},"modified":"2023-09-18T11:54:36","modified_gmt":"2023-09-18T08:54:36","slug":"find","status":"publish","type":"page","link":"https:\/\/www.bashpi.org\/?page_id=387","title":{"rendered":"find"},"content":{"rendered":"<h4>Find files and folders and optionally do something with them<\/h4>\n<p>Find how many files with specific user\/group ownership and permissions are in current directory and subfolders:<\/p>\n<pre><code class=\"language-bash\">find . -user USERNAME -group GROUPNAME -perm 644 |wc -l\n<\/code><\/pre>\n<p><strong>Find FILES with specific permissions in current directory and subfolders:<\/strong><\/p>\n<pre><code class=\"language-bash\">find . -type f -perm 640\n<\/code><\/pre>\n<p><strong>Change file owner and group for files\/directories which group membership is <em>oldgroup<\/em><\/strong><\/p>\n<pre><code class=\"language-bash\">find . -group oldgroup -exec chown newuser:newgroup '{}' \\;\n<\/code><\/pre>\n<p><strong>Find and delete files\/directories which are over 62 days old<\/strong><\/p>\n<pre><code class=\"language-bash\">find . -mtime +62 -exec rm '{}' \\; &gt; \/dev\/null 2&gt;&amp;1 \n<\/code><\/pre>\n<p><strong>Find and delete files not having .gz extension<\/strong><\/p>\n<pre><code class=\"language-bash\">find \/foo\/ -type f ! -name \"*.gz\" -exec rm -f '{}' \\;\n<\/code><\/pre>\n<p><strong>Find files with SUID bit<\/strong><\/p>\n<pre><code class=\"language-bash\">find . -perm -4000\nor\nfind . -perm -4000 -type f<\/code><\/pre>\n<p><strong>find files and print only file name<\/strong> instead of full path&nbsp; &#8211; use formating with printf<\/p>\n<pre><code class=\"language-bash\">find . -type f -printf '%f\\n'\n<\/code><\/pre>\n<p>there are many formating options, just run <em>man find<\/em> and search <em>-printf<\/em> in there.<\/p>\n<p>Note &#8211; always use single quotes around parenthesis when using -exec like so: -exec rm &#8216;{}&#8217;<\/p>\n<p><strong>find files and print out file size, permissions, owner ID and last modification time in seconds from unix epoch along with the files md5 hash<br><\/strong><\/p>\n<pre><code class=\"language-bash\">find . -mount -type f -printf \"%k %U %m %T@ \" -exec md5sum {} \\;<\/code><\/pre>\n<p>-mount will not descend directories on other filesystems. Its useful if you have mounted other filesystems, especially network shares to the directory tree.<\/p>\n<p><strong>Find files with suid bit which are owned by root, executable for group or all, from all local filesystems.<\/strong> Find runs against one local filesystem at the time not decending to other filesystems.<\/p>\n\n\n<pre class=\"wp-block-code\"><code>\n# for fs in `findmnt -it sysfs,cgroup,proc,devtmpfs,devpts,pstore,debugfs,hugetlbfs,mqueue,configfs,autofs,tmpfs,securityfs,sunrpc,fusectl,binfmt_misc,nfs,cifs -o SOURCE,TARGET -n -l |grep \"^\/\"|awk '{print $2}'`; do find ${fs} -type f -perm \/4000 \\( -perm -g=x -o -perm -o=x \\) -mount -uid 0 2>\/dev\/null ;done<\/code><\/pre>\n\n\n\n<p><strong>Find files with SUID or SGID bits from all listed filesystems, print out file permissions, user id group id and file name<\/strong>. This is another way how to find files from all local filesystems. Just include them in the list, currently onlyt xfs, ext2\/3\/4 are there. This example  I aklso used mount to list filesystems instead of findmnt.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># for mount in `for t in xfs ext2 ext3 ext4; do mount -l -t ${t};done |awk '{print $3}'`; do find ${mount} -xdev -type f \\( -perm -4000 -o -perm -2000 \\) -exec stat -c \"%a %u %g %n\" '{}' \\; 2>\/dev\/null ;done<\/code><\/pre>\n\n\n\n<p>If you found this useful, say thanks, click on some banners or <a href=\"http:\/\/www.bashpi.org\/?page_id=105\">donate<\/a>, I can always use some beer money.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Find files and folders and optionally do something with them Find how many files with specific user\/group ownership and permissions are in current directory and subfolders: find . -user USERNAME -group GROUPNAME -perm 644 |wc -l Find FILES with specific permissions in current directory and subfolders: find . -type f -perm 640 Change file owner [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":397,"menu_order":0,"comment_status":"closed","ping_status":"open","template":"","meta":{"footnotes":""},"class_list":["post-387","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.bashpi.org\/index.php?rest_route=\/wp\/v2\/pages\/387","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bashpi.org\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.bashpi.org\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.bashpi.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bashpi.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=387"}],"version-history":[{"count":13,"href":"https:\/\/www.bashpi.org\/index.php?rest_route=\/wp\/v2\/pages\/387\/revisions"}],"predecessor-version":[{"id":1226,"href":"https:\/\/www.bashpi.org\/index.php?rest_route=\/wp\/v2\/pages\/387\/revisions\/1226"}],"up":[{"embeddable":true,"href":"https:\/\/www.bashpi.org\/index.php?rest_route=\/wp\/v2\/pages\/397"}],"wp:attachment":[{"href":"https:\/\/www.bashpi.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=387"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}