{"id":643,"date":"2012-03-20T11:22:23","date_gmt":"2012-03-20T09:22:23","guid":{"rendered":"http:\/\/www.shukko.com\/x3\/?p=643"},"modified":"2012-03-20T11:22:23","modified_gmt":"2012-03-20T09:22:23","slug":"deleting-tons-of-files-in-linux-argument-list-too-long","status":"publish","type":"post","link":"https:\/\/www.shukko.com\/x3\/2012\/03\/20\/deleting-tons-of-files-in-linux-argument-list-too-long\/","title":{"rendered":"Deleting tons of files in Linux (Argument list too long)"},"content":{"rendered":"<p>Quick Linux Tip:<\/p>\n<p>If you\u2019re trying to delete a very large number of files at one time (I deleted a directory with 485,000+ today), you will probably run into this error:<\/p>\n<div>\n<div>\/bin\/rm: Argument list too long.<\/div>\n<\/div>\n<p>The problem is that when you type something like \u201crm -rf *\u201d, the \u201c*\u201d is replaced with a list of every matching file, like \u201crm -rf file1 file2 file3 file4\u2033 and so on. There is a reletively small buffer of memory allocated to storing this list of arguments and if it is filled up, the shell will not execute the program.<\/p>\n<p>To get around this problem, a lot of people will use the find command to find every file and pass them one-by-one to the \u201crm\u201d command like this:<\/p>\n<div>\n<div>find . -type f -exec rm -v {} \\;<\/div>\n<\/div>\n<p>My problem is that I needed to delete 500,000 files and it was taking <strong>way too long<\/strong>.<\/p>\n<p>I stumbled upon a much faster way of deleting files \u2013 the \u201cfind\u201d command has a \u201c-delete\u201d flag built right in! Here\u2019s what I ended up using:<\/p>\n<div>\n<div>find . -type f -delete<\/div>\n<\/div>\n<p>Using this method, I was deleting files at a rate of about 2000 files\/second \u2013 much faster!<\/p>\n<p>You can also show the filenames as you\u2019re deleting them:<\/p>\n<div>\n<div>find . -type d -print -delete<\/div>\n<\/div>\n<p>\u2026or even show how many files will be deleted, then time how long it takes to delete them:<\/p>\n<div>\n<div>root@devel# ls -1 | wc -l &amp;&amp; time find . -type f -delete<br \/>\nreal \u00a0 \u00a00m3.660s<br \/>\nuser \u00a0 \u00a00m0.036s<br \/>\nsys \u00a0 \u00a0 0m0.552s<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Quick Linux Tip: If you\u2019re trying to delete a very large number of files at one time (I deleted a directory with 485,000+ today), you will probably run into this error: \/bin\/rm: Argument list too long. The problem is that when you type something like \u201crm -rf *\u201d, the \u201c*\u201d is replaced with a list [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-643","post","type-post","status-publish","format-standard","hentry","category-kategerisiz"],"_links":{"self":[{"href":"https:\/\/www.shukko.com\/x3\/wp-json\/wp\/v2\/posts\/643","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.shukko.com\/x3\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shukko.com\/x3\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shukko.com\/x3\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shukko.com\/x3\/wp-json\/wp\/v2\/comments?post=643"}],"version-history":[{"count":1,"href":"https:\/\/www.shukko.com\/x3\/wp-json\/wp\/v2\/posts\/643\/revisions"}],"predecessor-version":[{"id":644,"href":"https:\/\/www.shukko.com\/x3\/wp-json\/wp\/v2\/posts\/643\/revisions\/644"}],"wp:attachment":[{"href":"https:\/\/www.shukko.com\/x3\/wp-json\/wp\/v2\/media?parent=643"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shukko.com\/x3\/wp-json\/wp\/v2\/categories?post=643"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shukko.com\/x3\/wp-json\/wp\/v2\/tags?post=643"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}