{"id":123056,"date":"2020-11-30T17:00:15","date_gmt":"2020-11-30T14:00:15","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-bulk-rename-files-to-numeric-file-names-in-linux-cloudsavvy-it\/"},"modified":"2020-11-30T17:00:15","modified_gmt":"2020-11-30T14:00:15","slug":"how-to-bulk-rename-files-to-numeric-file-names-in-linux-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-bulk-rename-files-to-numeric-file-names-in-linux-cloudsavvy-it\/","title":{"rendered":"#How to Bulk Rename Files to Numeric File Names in Linux \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a2e9a5982b54\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a2e9a5982b54\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-bulk-rename-files-to-numeric-file-names-in-linux-cloudsavvy-it\/#Numeric_File_Names\" >Numeric File Names<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-bulk-rename-files-to-numeric-file-names-in-linux-cloudsavvy-it\/#Bulk_Rename_Files_to_Numeric_File_Names\" >Bulk Rename Files to Numeric File Names<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Bulk Rename Files to Numeric File Names in Linux \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<figure id=\"attachment_8274\" style=\"width: 700px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-8274 size-full\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/3e34302c95c8231cec6b441df7d930b5\/p\/uploads\/2020\/11\/501ea86a.png\" alt=\"\" width=\"700\" height=\"300\" data-crediturl=\"https:\/\/www.shutterstock.com\/image-vector\/calculator-digital-numbers-456060745\" data-credittext=\"Shutterstock\/estherpoon\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><figcaption class=\"wp-caption-text\"><span class=\"imagecredit\"><a rel=\"nofollow noopener noreferrer\" target=\"_blank\" href=\"https:\/\/www.shutterstock.com\/image-vector\/calculator-digital-numbers-456060745\">Shutterstock\/estherpoon<\/a><\/span><\/figcaption><\/figure>\n<p>Want to rename a whole set of files to a numeric sequence (1.pdf, 2.pdf, 3.pdf, \u2026) in Linux? This can be done with some light <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">script<\/a>ing and this article will show you how to do exactly that.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Numeric_File_Names\"><\/span>Numeric File Names<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Usually when we scan a PDF file using some hardware (mobile phone, dedicated PDF scanner), the file name will read something like <em>2020_11_28_13_43_00.pdf<\/em>. Many other semi-automated systems produces similar date and time based filenames.<\/p>\n<p>Sometimes the file may also contain the name of the application being used, or some other information like for example the applicable DPI (dots per inch) or the scanned paper size.<\/p>\n<p>When collecting PDF files together from different sources, file naming conventions may differ significantly and it may be good to standardize on a numeric (or part numeric) file name.<\/p>\n<p>This also applies to other domains and sets of files. For example, your recipes or photo collection, data samples generated automated monitoring systems, log files ready for archiving, a set of SQL files for the database engineer, and <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/general\/\" data-internallinksmanager029f6b8e52c=\"3\" title=\"General\" target=\"_blank\" rel=\"noopener\">general<\/a>ly any data collected from different sources with different naming schemes.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Bulk_Rename_Files_to_Numeric_File_Names\"><\/span>Bulk Rename Files to Numeric File Names<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In Linux, it is easy to quickly rename a whole set of files with completely different file names, to a numerical sequence. \u201cEasy\u201d means \u201ceasy to execute\u201d here: the problem of bulk renaming files to numerical numbers is complex to code in itself: the oneliner script below took 3-4 hours to research, create and test. Many other commands tried all had limitations which I wanted to avoid.<\/p>\n<p>Please note that no warranties are given or provided, and this code is provided \u2018as is\u2019. Please do your own research before running it. That said, I did test it successfully against files with various special characters, and also against more then 50k files without any file being lost. I also checked a file named <code>'a'$'n''a.pdf'<\/code> which contains a newline.<\/p>\n<pre>if [ ! -r _e -a ! -r _c ]; then echo 'pdf' &gt; _e; echo 1 &gt; _c ;find . -name \"*.$(cat _e)\" -print0 | xargs -0 -I{} bash -c 'mv -n \"{}\" $(cat _c).$(cat _e);echo $[ $(cat _c) + 1 ] &gt; _c'; rm -f _e _c; fi&#13;\n<\/pre>\n<p>Let\u2019s first look at how this works, and then analyze the command. We have a created a directory with eight files, all named quite differently, except their extension matches and is <i>.pdf<\/i>. We next run the command above:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8255\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/fc6539e3926bebd1a2d4b5f6d3d5cc1d\/p\/uploads\/2020\/11\/b1dd0181.png\" alt=\"Bulk Rename Files to Numeric File Names in Linux\" width=\"1049\" height=\"127\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>The outcome was that the 8 files have been renamed to 1.pdf, 2.pdf, 3.pdf, etc., even though their names were quite offset before.<\/p>\n<p>The command assumes you do not have any <i>1.pdf<\/i> to <i>x.pdf<\/i> named files yet. If you do, you can move those files into a separate directory, set the <code>echo 1<\/code> to a higher number to start the renaming the remaining files at a given offset, and then merge the two directories together again.<\/p>\n<p>Please always take care not to overwrite any files, and it is always a good idea to take a quick backup before updating anything.<\/p>\n<p>Let\u2019s look at the command in detail. It can help to see what is happening by adding the <code>-t<\/code> option to <code>xargs<\/code> which lets us see what is going on behind the scenes:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8256\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/f93d6e84e05e93e836c1094b933218b2\/p\/uploads\/2020\/11\/886e30a9.png\" alt=\"xargs with -t option lets us see what is happening during the rename process\" width=\"1050\" height=\"259\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>To start, the command uses two small temporary files (named <i>_e<\/i> and <i>_c<\/i>) as temporary storage. At the start of the oneliner it does a <i>safety check<\/i> using an <code>if<\/code> statement to ensure that both <i>_e<\/i> and <i>_c<\/i> files are not present. If there is a file with that name, the script will not proceed.<\/p>\n<p>On the topic of using small temporary files versus variables, I can say that whereas using variables would have been ideal (saves some disk I\/O), there were two issues I was running into.<\/p>\n<p>The first one is that if you EXPORT a variable at the start of the oneliner and then use that same variable later, if another script uses the same variable (including this script run more then once simultaneously on the same machine), then that script, or this one, may be affected. Such interference is best avoided when it comes to renaming many files!<\/p>\n<p>The second one was that <i>xargs<\/i> in combination with <i>bash -c<\/i> seems to have a limitation in variable handling inside the <code>bash -c<\/code> command line. Even extensive research online did not provide a workable solution for this. Thus, I ended up using a small file <i>_c<\/i> which keep progress.<\/p>\n<p><i>_e<\/i> Is the extension we will be searching for and using, and <i>_c<\/i> is a counter which will be automatically increased on each rename. The <code>echo $[ $(cat _c) + 1 ] &gt; _c<\/code> code takes care of this, by displaying the file with <code>cat<\/code>, adding one number, and re-writing it.<\/p>\n<p>The command also uses the best possible method of handling special file name characters by using null-termination instead of the standard newline termination, i.e. the <code>\u0000<\/code> character. This is ensured by the <code>-print0<\/code> option to <code>find<\/code>, and by the <code>-0<\/code> option to xargs.<\/p>\n<p>The find command will search for any files with the extension as specified in the <i>_e<\/i> file (created by the <code>echo 'pdf' &gt; _e<\/code> command. You can vary this extension to any other extension you want, but please do not prefix it with a dot. The dot is already included in the later <code>*.$(cat _e)<\/code> <code>-name<\/code> specifier to <code>find<\/code>.<\/p>\n<p>Once find has located all the files and sent them \u2013 <code>\u0000<\/code> terminated to <i>xargs<\/i>, <i>xargs<\/i> will rename the files one by one using the counter file (<i>_c<\/i>) and the same extension file (<i>_e<\/i>). To obtain the contents of the two files, a simple <code>cat<\/code> command is used, executed from within a subshell.<\/p>\n<p>The <code>mv<\/code> move command uses <code>-n<\/code> to avoid overwriting any file already present. Finally we cleanup the two temporary files by removing them.<\/p>\n<p>While the cost of using two state files and subshell forking may be limited, this does add some overhead to the script, especially when dealing with a large amount of files.<\/p>\n<p>There are all sorts of other solutions for this same problem online, and many have tried and failed to create a fully working solution. A lot of solutions forgot all sorts of side cases, like using <code>ls<\/code> without specifying <code>--color=never<\/code>, which may lead to hex codes being parsed when directory listing color coding is used.<\/p>\n<p>Yet other solutions missed handling files with spaces, newlines and special characters like \u2018\u2019 correctly. For this, the combination <code>find ... -print0 ... | xargs -0 ...<\/code> is usually indicated and ideal (and both the <i>find<\/i> and <i>xargs<\/i> manuals allude to this fact quite strongly).<\/p>\n<p>Whereas I do not consider my implementation the perfect or end solution, it seems to make a significant furtherance to many of the other solutions out there, by using <code>find<\/code> and <code>\u0000<\/code> terminated strings, ensuring <em>maximum filename and parsing compatibility<\/em>, as well as having a few other niceties like being able to specify a <em>starting offset<\/em>, and being fully <em>Bash-native<\/em>.<\/p>\n<p><strong>Enjoy!<\/strong>\n<\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener noreferrer\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/8254\/how-to-bulk-rename-files-to-numeric-file-names-in-linux\/\" target=\"_blank\" rel=\"noopener noreferrer\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Bulk Rename Files to Numeric File Names in Linux \u2013 CloudSavvy IT&#8221; Shutterstock\/estherpoon Want to rename a whole set of files to a numeric sequence (1.pdf, 2.pdf, 3.pdf, \u2026) in Linux? This can be done with some light scripting and this article will show you how to do exactly that. Numeric File Names&#8230;<\/p>\n","protected":false},"author":1,"featured_media":123057,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2020\/11\/501ea86a.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-123056","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/123056","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=123056"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/123056\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/123057"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=123056"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=123056"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=123056"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}