{"id":2850,"date":"2010-09-02T18:12:07","date_gmt":"2010-09-02T17:12:07","guid":{"rendered":"http:\/\/www.walkingrandomly.com\/?p=2850"},"modified":"2010-09-02T18:12:07","modified_gmt":"2010-09-02T17:12:07","slug":"zero-padding-filenames-using-bash-in-linux","status":"publish","type":"post","link":"https:\/\/walkingrandomly.com\/?p=2850","title":{"rendered":"Zero padding filenames using bash in Linux"},"content":{"rendered":"<p>I recently had a set of files that were named as follows<\/p>\n<pre>frame1.png\r\nframe2.png\r\nframe3.png\r\nframe4.png\r\nframe5.png\r\nframe6.png\r\nframe7.png\r\nframe8.png\r\nframe9.png\r\nframe10.png\r\n<\/pre>\n<p>and so on, right up to frame750.png.  The plan was to turn these .png files into an uncompressed movie using mencoder via the following command (<a href=\"http:\/\/www.mplayerhq.hu\/DOCS\/HTML\/en\/menc-feat-enc-images.html\">original source<\/a>)<\/p>\n<pre>mencoder mf:\/\/*.png -mf w=720:h=720:fps=25:type=png -ovc raw -oac copy -o output.avi\r\n<\/pre>\n<p>but I ended up with a movie that jumped all over the place since the frames were in an odd order.  In the following order in fact<\/p>\n<pre>\r\nframe0.csv\r\nframe100.csv\r\nframe101.csv\r\nframe102.csv\r\nframe103.csv\r\nframe104.csv\r\nframe105.csv\r\nframe106.csv\r\nframe107.csv\r\nframe108.csv\r\nframe109.csv\r\nframe10.csv\r\nframe110.csv\r\n<\/pre>\n<p>This is because globbing expansion (the <strong>*.png<\/strong> bit) is alphabetical in bash rather than numerical.  <\/p>\n<p>One way to get the frames in the order that I want is to zero-pad them.  In other words I replace <strong>file1.png<\/strong> with <strong>file001.png<\/strong> and <strong>file20.png<\/strong> with <strong>file020.png<\/strong> and so on.  Here&#8217;s how to do that in bash<\/p>\n<pre>\r\n#!\/bin\/bash\r\nnum=`expr match \"$1\" '[^0-9]*\\([0-9]\\+\\).*'`\r\npaddednum=`printf \"%03d\" $num`\r\necho ${1\/$num\/$paddednum}\r\n<\/pre>\n<p>Save the above to a file called zeropad.sh and then do the following command to make it executable<\/p>\n<pre>\r\nchmod +x .\/zeropad.sh\r\n<\/pre>\n<p>You can then use the zeropad.sh script as follows<\/p>\n<pre>\r\n.\/zeropad.sh frame1.png\r\n<\/pre>\n<p>which will return the result <\/p>\n<pre>\r\nframe001.png\r\n<\/pre>\n<p> All that remains is to use this script to rename all of the .png files in the current directory such that they are zeropadded.<\/p>\n<pre>\r\nfor i in *.png;do mv $i `.\/zeropad.sh $i`; done\r\n<\/pre>\n<p>You may want to change the number of digits used in each filename from 3 to 5 (say).  To do this just change %03d in zeropad.sh to %05d<\/p>\n<p>Let me know if you find this useful or have an alternative solution you&#8217;d like to share (in another language maybe?)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I recently had a set of files that were named as follows frame1.png frame2.png frame3.png frame4.png frame5.png frame6.png frame7.png frame8.png frame9.png frame10.png and so on, right up to frame750.png. The plan was to turn these .png files into an uncompressed movie using mencoder via the following command (original source) mencoder mf:\/\/*.png -mf w=720:h=720:fps=25:type=png -ovc raw [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[5,7],"tags":[],"class_list":["post-2850","post","type-post","status-publish","format-standard","hentry","category-linux","category-programming"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3swhs-JY","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/2850","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2850"}],"version-history":[{"count":4,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/2850\/revisions"}],"predecessor-version":[{"id":2854,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/2850\/revisions\/2854"}],"wp:attachment":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2850"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2850"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2850"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}