{"id":1150,"date":"2023-11-10T00:00:00","date_gmt":"2023-11-10T05:00:00","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=1150"},"modified":"2024-02-05T10:37:57","modified_gmt":"2024-02-05T15:37:57","slug":"bash-renaming-files-in-a-directory","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/bash-renaming-files-in-a-directory\/","title":{"rendered":"bash: renaming files in a directory"},"content":{"rendered":"\n<p>Suppose you have 2000 photos in a directory and you need to rename them. The files are named as:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>IMG_1232.JPG\nSP234324.JPG\nwebdown.JPG\n...<\/code><\/pre>\n\n\n\n<p>We need to rename them as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>salzburg_1.jpg\nsalzburg_2.jpg\n...<\/code><\/pre>\n\n\n\n<p>Here is how:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>j=1\ns=salzburg_\nfor f in *.JPG; do\n  mv $f $s$j.jpg\n  j=(($j+1))\n  echo renaming $f to $s$j.jpg\ndone<\/code><\/pre>\n\n\n\n<p>j in is the counter. Line 5 increments j. s is a string. Line 4 uses the mv command renaming each file to the desired combination of text and number. Line 6 prints a message on the command line. Note that in line 3, we only loop through files ending in .JPG.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Suppose you have 2000 photos in a directory and you need to rename them. The files are named as: We need to rename them as follows: Here is how: j in is the counter. Line 5 increments j. s is a string. Line 4 uses the mv command renaming each file to the desired combination [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[94,209],"class_list":["post-1150","post","type-post","status-publish","format-standard","hentry","category-bash","tag-bash","tag-file-management"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/1150","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/comments?post=1150"}],"version-history":[{"count":1,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/1150\/revisions"}],"predecessor-version":[{"id":1151,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/1150\/revisions\/1151"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=1150"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=1150"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=1150"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}