{"id":773,"date":"2010-03-17T12:26:47","date_gmt":"2010-03-17T16:26:47","guid":{"rendered":"http:\/\/nylinuxhelp.com\/blogs\/?p=773"},"modified":"2010-03-17T12:31:28","modified_gmt":"2010-03-17T16:31:28","slug":"cygwin-rename-command-help","status":"publish","type":"post","link":"https:\/\/nylinuxhelp.com\/blogs\/command-line\/cygwin-rename-command-help","title":{"rendered":"Cygwin rename command help"},"content":{"rendered":"<h2>Rename files in cygwin, overcoming a limitation<\/h2>\n<p><a title=\"Cygwin, the Linux-like terminal environment\" href=\"http:\/\/www.cygwin.com\/\" target=\"_blank\">Cygwin is a Linux-like environment for Windows<\/a>.\u00a0 It comes in handy when you&#8217;re at work, or at home, and your only operating system happens to be Microsoft Windows.\u00a0 <strong>Cygwin allows you to issue Linux commands in a console<\/strong> in Microsoft Windows.\u00a0 I find it easier to use than the DOS CMD prompt when I&#8217;m using a Windows OS.\u00a0 It&#8217;s a great way to get things done quicker, and can be scripted\u2014in a similar environment to a Linux Terminal.<\/p>\n<p>So, let&#8217;s say you&#8217;re seasoned in the Linux shell.\u00a0 You&#8217;re now at work, using Cygwin, and you want to rename a lot of files based on their name matching a pattern of letters or numbers or both.\u00a0 This is where cygwin has some trouble.<\/p>\n<p>By default, cygwin does not come with <strong>rename<\/strong> ability.\u00a0 The package <strong>util-linux is required<\/strong>.<\/p>\n<p>Cygwin&#8217;s rename command renames files if you tell it <strong>the exact name<\/strong> of your target file.\u00a0 This is the same as the <strong>mv<\/strong> command. I&#8217;m no Cygwin expert, but this is the experience I&#8217;ve had.<\/p>\n<p>In Linux, you can rename files using wildcards or pattern matches.\u00a0 Cygwin&#8217;s rename command cannot seem to handle regexes in the same way.\u00a0 I&#8217;ve tried a fair amount of googling to see why this command has limited behavior in cygwin.\u00a0 I haven&#8217;t found a usable solution or add-on to cygwin to overcome this limitation.<\/p>\n<p>So, here&#8217;s a shell script that uses sed, mv, and find to overcome the limitation.\u00a0 I was unable to attach it, so I copied and pasted it below.<\/p>\n<p>Please understand and accept the following disclaimer:\u00a0 <span style=\"color: #000000;\"><em>YOU MAY USE &amp; MODIFY THE SCRIPT.\u00a0 AUTHOR ASSUMES ZERO LIABILITY.\u00a0 SCRIPT INTENDED FOR USE IN CYGWIN WITHIN A WINDOWS OS. <\/em><em>SCRIPT COMES WITH NO WARRANTY, <\/em><\/span><em><span style=\"color: #800000;\"><span style=\"color: #000000;\">USE AT YOUR OWN RISK.<\/span> <\/span><\/em>Thanks.<\/p>\n<pre><span style=\"color: #993300;\">#!\/bin\/sh\r\n# cygrename : A Program to overcome limitation of cygwin's \"rename\" function.\r\n# Author: Adam Teller\r\n#\r\n# Cygwin's \"rename\" doesn't work same way as *Nix \"rename\"\r\n# because it cannot use wildcards. So, what can we do?\r\n## Get the basename of the file if it matches search pattern.\r\n## If 4th argument is set, change file to that suffix.\r\n## else, match pattern within file name and keep same suffix\r\n#\r\n## Arguments: [search pattern] [replace pattern] [in suffix] [to new suffix]\r\n\r\nLIST1=$(find . -type f -name \"*${1}*\\.${3}\")\r\nif [ -z \"${LIST1}\" ];\r\n then\r\n   printf \"Did not find any file matches, program will exit\\n\"\r\n exit 1\r\nfi\r\n\r\nfor found in $LIST1;\r\n do\r\n   MATCHEDFILE=$(basename $found);\r\n   NEWFILENAME=$(echo $MATCHEDFILE | sed -e \"s\/${1}\/${2}\/\");\r\n   NEWFILENAMENOEXTENSION=$(echo $NEWFILENAME | sed -r \"s\/\\.${3}\/\/\");\r\n\r\n   ## TEST if $4 has been set, it means want a new file suffix\r\n   if [ -z \"${4}\" ];\r\n     then\r\n       printf \"$MATCHEDFILE :to be renamed as: $NEWFILENAME for files of type ${3}\\n\";\r\n       mv $MATCHEDFILE $NEWFILENAME;\r\n     else\r\n       printf \"${NEWFILENAMENOEXTENSION}.${3} will get $4 as its suffix, \";\r\n       printf \"and be renamed to ${NEWFILENAMENOEXTENSION}.${4} \\n\";\r\n       mv $MATCHEDFILE \"${NEWFILENAMENOEXTENSION}.${4}\"\r\n  fi\r\n done\r\n\r\n exit 0<\/span><\/pre>\n<p>Ok, I&#8217;ve copied the script and saved it, how do I use it?<\/p>\n<ul>\n<li>Run your script from the same directory that has the files that you want to rename. I&#8217;ve named my copy &#8220;<strong>cygrename<\/strong>&#8221; (short for &#8220;cygwin rename&#8221;) but you may save your file with another name if you wish.\u00a0 Make sure your script is executable. Use the cd command to get to the correct directory.<\/li>\n<li>It accepts 3 or 4 command line arguments.<\/li>\n<li>Invoke it in the shell using <strong>sh cygrename &#8220;what-to-find&#8221; &#8220;replace-it-with&#8221; &#8220;file extension&#8221;<\/strong> &#8220;new file extension&#8221; (optional).<\/li>\n<li>The &#8220;file extension&#8221; argument does not include the dot (.), just use letters such as <strong>txt<\/strong> or <strong>doc<\/strong> or <strong>gif<\/strong>.<\/li>\n<li>3 arguments is the minimum and I&#8217;ve omitted the argument counting, you may however wish to add it into your program so that the 3 argument minimum is enforced.<\/li>\n<li>In the even that the program does not find a match, the program exits.<\/li>\n<li>The script uses the find command to look for files matching your pattern.\u00a0 The result of a &#8220;find&#8221; usually includes the path information.\u00a0 This is why we use the basename directive.\u00a0 We just want the actual file name.\u00a0 For example, a call to find might result in &#8220;.\/file1.pdf&#8221; and what we really want to use is &#8220;file1.pdf&#8221;<\/li>\n<li>If the optional 4th argument is used, it indicates a desire to change the file suffix.\u00a0 Otherwise, you can rename files and keep the file suffix as is.<\/li>\n<\/ul>\n<p>Here&#8217;s a way to store &amp; use the script.\u00a0 This script is yours to use &amp; modify as you wish.<\/p>\n<ol>\n<li>Store the script (assuming you&#8217;ve named it cygrename) in the &#8220;bin&#8221; folder in your user&#8217;s home folder.\u00a0 In a windows environment, this is usually C:\\cygwin\\home\\userName\\bin.\u00a0 When you first see this folder, it should be empty.\u00a0 There is a cygwin &#8220;system&#8221; bin that will be full of executable files. THIS IS NOT what you&#8217;re looking for. You are looking for <strong>your user&#8217;s bin folder<\/strong>, which will be empty until you start putting files into it.<\/li>\n<li>Once you&#8217;ve used the cd command to get to the folder where the target files reside, you can invoke the script.\u00a0 The script can be more easily invoked if you change the PATH variable in Cygwin.\u00a0 <a title=\"Cygwin FAQ page\" href=\"http:\/\/cs.nyu.edu\/~yap\/prog\/cygwin\/FAQs.html#windows\" target=\"_blank\">Find instructions here<\/a>.\u00a0 Adding your user&#8217;s bin directory to your search PATH is recommended because it will allow you to call the script just by its filename, and won&#8217;t require the entire path name of the script.<\/li>\n<li>In this example, for simplicity, I&#8217;m assuming you have several files in the same &#8220;bin&#8221; directory as the script you have saved.\u00a0 You have 4 image files named &#8220;picture1.jpg&#8221; &#8220;picture2.jpg&#8221; &#8220;picture3.jpg&#8221; and &#8220;picture4.jpg&#8221; .\u00a0 You want to change &#8220;picture&#8221; to &#8220;img&#8221; and keep the files as *jpg\u00a0 -From within the &#8220;bin&#8221;directory, Call the script as <strong>sh cygrename &#8220;picture&#8221; &#8220;img&#8221; &#8220;jpg&#8221;<\/strong><\/li>\n<li>Your 4 jpg files should now be named &#8220;img1.jpg&#8221;, &#8220;img2.jpg&#8221; and etc.<\/li>\n<li>If (based on previous example) you wanted to change some web files (html) AND their file suffix, you could call the script as <strong>sh cygrename &#8220;website&#8221; &#8220;site&#8221; &#8220;htm&#8221; &#8220;html&#8221;<\/strong> and it would rename files such as &#8220;<span style=\"color: #993300;\">websiteLocation.htm<\/span>&#8221; to &#8220;<span style=\"color: #993300;\">siteLocation.html<\/span>&#8221; because you used the optional 4th argument.<\/li>\n<li>Please be aware that simply changing a file extension DOES NOT CONVERT a file.\u00a0 What I mean is that if you change a file suffix of &#8220;img2.jpg&#8221; to &#8220;img2.gif&#8221; you are not converting to the gif file type.\u00a0 The file becomes a jpg file named with an incorrect suffix.\u00a0 This is not recommended.\u00a0 However, changing an extension from .htm to .html is ok because it is a text-based file.\u00a0 Image files, office suite files, and multimedia files should only have their suffix changed by using another application to convert the files.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Rename files in cygwin, overcoming a limitation Cygwin is a Linux-like environment for Windows.\u00a0 It comes in handy when you&#8217;re at work, or at home, and your only operating system happens to be Microsoft Windows.\u00a0 Cygwin allows you to issue Linux commands in a console in Microsoft Windows.\u00a0 I find it easier to use than [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,23],"tags":[48,50,55,35,38,57,49],"class_list":["post-773","post","type-post","status-publish","format-standard","hentry","category-command-line","category-use-linux","tag-cli","tag-console","tag-cygwin","tag-find","tag-mv","tag-rename","tag-terminal"],"_links":{"self":[{"href":"https:\/\/nylinuxhelp.com\/blogs\/wp-json\/wp\/v2\/posts\/773","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nylinuxhelp.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nylinuxhelp.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nylinuxhelp.com\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nylinuxhelp.com\/blogs\/wp-json\/wp\/v2\/comments?post=773"}],"version-history":[{"count":45,"href":"https:\/\/nylinuxhelp.com\/blogs\/wp-json\/wp\/v2\/posts\/773\/revisions"}],"predecessor-version":[{"id":816,"href":"https:\/\/nylinuxhelp.com\/blogs\/wp-json\/wp\/v2\/posts\/773\/revisions\/816"}],"wp:attachment":[{"href":"https:\/\/nylinuxhelp.com\/blogs\/wp-json\/wp\/v2\/media?parent=773"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nylinuxhelp.com\/blogs\/wp-json\/wp\/v2\/categories?post=773"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nylinuxhelp.com\/blogs\/wp-json\/wp\/v2\/tags?post=773"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}