{"id":453,"date":"2009-04-16T20:50:41","date_gmt":"2009-04-16T17:50:41","guid":{"rendered":"http:\/\/www.void.gr\/kargig\/blog\/?p=453"},"modified":"2009-05-28T23:02:27","modified_gmt":"2009-05-28T20:02:27","slug":"delete-all-empty-directories-using-xarg","status":"publish","type":"post","link":"https:\/\/www.void.gr\/kargig\/blog\/2009\/04\/16\/delete-all-empty-directories-using-xarg\/","title":{"rendered":"delete all empty directories using xargs"},"content":{"rendered":"<p>I was trying to figure out one 1-liner to delete all empty directories in a tree.<br \/>\nThe following should do it&#8217;s job:<br \/>\n<code>%find -type d -empty | xargs rm -rvf<\/code><\/p>\n<p>BUT! While this works for directories with &#8220;regular&#8221; filenames, it doesn&#8217;t work when there are special characters inside the filename. Consider this for example:<\/p>\n<p><code>%ls -1<br \/>\ntest 1<br \/>\ntest2<br \/>\ntest _ 3<br \/>\n%find -type d -empty<br \/>\n.\/test 1<br \/>\n.\/test2<br \/>\n.\/test _ 3<br \/>\n%find -type d -empty | xargs rm -rvf<br \/>\nremoved directory: `.\/test2'<br \/>\n%find -type d -empty<br \/>\n.\/test 1<br \/>\n.\/test _ 3<br \/>\n<\/code><\/p>\n<p>Only directory &#8220;test2&#8221; was deleted. To delete the rest of the directories when they contain &#8220;special&#8221; characters like whitespace and quotes one needs to modify the command like this:<\/p>\n<p><code>%find -type d -empty -print0 | xargs -0 rm -rvf<br \/>\nremoved directory: `.\/test 1'<br \/>\nremoved directory: `.\/test _ 3 '<\/code><\/p>\n<p>\ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was trying to figure out one 1-liner to delete all empty directories in a tree. The following should do it&#8217;s job: %find -type d -empty | xargs rm -rvf BUT! While this works for directories with &#8220;regular&#8221; filenames, it doesn&#8217;t work when there are special characters inside the filename. Consider this for example: %ls [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ep_exclude_from_search":false,"footnotes":""},"categories":[3],"tags":[104,105,595,47,106],"class_list":["post-453","post","type-post","status-publish","format-standard","hentry","category-linux","tag-delete","tag-directory","tag-linux","tag-oneliner","tag-xargs"],"aioseo_notices":[],"views":23763,"_links":{"self":[{"href":"https:\/\/www.void.gr\/kargig\/blog\/wp-json\/wp\/v2\/posts\/453","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.void.gr\/kargig\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.void.gr\/kargig\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.void.gr\/kargig\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.void.gr\/kargig\/blog\/wp-json\/wp\/v2\/comments?post=453"}],"version-history":[{"count":4,"href":"https:\/\/www.void.gr\/kargig\/blog\/wp-json\/wp\/v2\/posts\/453\/revisions"}],"predecessor-version":[{"id":550,"href":"https:\/\/www.void.gr\/kargig\/blog\/wp-json\/wp\/v2\/posts\/453\/revisions\/550"}],"wp:attachment":[{"href":"https:\/\/www.void.gr\/kargig\/blog\/wp-json\/wp\/v2\/media?parent=453"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.void.gr\/kargig\/blog\/wp-json\/wp\/v2\/categories?post=453"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.void.gr\/kargig\/blog\/wp-json\/wp\/v2\/tags?post=453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}