{"id":6632,"date":"2014-03-05T05:03:48","date_gmt":"2014-03-04T18:03:48","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/wordpress\/?p=6632"},"modified":"2014-03-05T05:03:48","modified_gmt":"2014-03-04T18:03:48","slug":"rename-scenarios-in-linux-shell-script-primer-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/rename-scenarios-in-linux-shell-script-primer-tutorial\/","title":{"rendered":"Rename Scenarios in Linux Shell Script Primer Tutorial"},"content":{"rendered":"<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Linux\/Rename\/Rename.jpg\"><img decoding=\"async\" style=\"float:left; border: 15px solid pink;\" alt=\"Rename Scenarios in Linux Shell Script Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/Linux\/Rename\/Rename.jpg\" title=\"Rename Scenarios in Linux Shell Script Primer Tutorial\" \/><\/a><p class=\"wp-caption-text\">Rename Scenarios in Linux Shell Script Primer Tutorial<\/p><\/div>\n<p>Linux (or Unix) is a command line environment that is really powerful regarding file management.   This power is no surprise as the basis of Unix and hence, Linux, is that files are at the heart of the design of the operating system &#8230; if you like, its <a target=_blank title='kernel information from Wikipedia ... thanks' href='http:\/\/en.wikipedia.org\/wiki\/Kernel_%28computing%29'>kernel<\/a>.   Unix and Linux were designed for brevity, and so to read it does not always lead to immediate understanding, so, today, we present two scenarios you might want to do regarding the renaming of files in a shell script programmable way.<\/p>\n<p>Before we start, let&#8217;s review seven things about the organization of the practicalities of shell script usage are:<\/p>\n<ul>\n<li>The first line is a good place to define which type of shell scripting you are working with &#8230; these lines start with &#8220;#!&#8221; and then define a &#8220;bin&#8221; directory for your shell type &#8230; ours goes #!\/bin\/sh<\/li>\n<li>The last line should contain &#8220;exit&#8221; if you want to go back to a parent command line environment on finishing<\/li>\n<li>Be consistent with file extension arrangements, or not &#8230; for ours, here, we have no extension &#8230; Unix and Linux are fine with this.<\/p>\n<li>The permissions of the resultant script should have relevant execute bits set &#8230; to go chmod 777 [yourScriptFile] &#8230; allows everyone to execute your script &#8230; the &#8220;1&#8221; in 1 + 2<sup>1<\/sup> + 2<sup>2<\/sup> = 7 relates to the execute bit of the file &#8230; the three 7&#8217;s relate to &#8220;Owner&#8221; then &#8220;Group&#8221; then &#8220;World&#8221;.<\/li>\n<li>Sometimes you would do this work in your current directory, set the permissions and just go [yourScriptFile] on the command line, but you may find you need to go .\/[yourScriptFile] to make it happen.<\/li>\n<li>The power of scripting is the use of $1 $2 $3 etcetera referring to the parameters on the command line, allowing your single script to be &#8220;chameleon&#8221;-like in handling lots of scenarios &#8230; our examples today show bits of this type of parameter usage &#8230; but is the tip of the iceberg, as far as how your imagination should start working with what is possible!<\/li>\n<li>If you use parameters, there is, pretty obviously, lots of scope for bad &#8220;interactive&#8221; user usage &#8230; good to examine the user&#8217;s parameter usage, or not, and advise them about shell script advisory usage when they show signs of not knowing what is required by the script.<\/p>\n<\/ul>\n<p>Here is the first renaming scenario &#8230; called &#8220;rname&#8221; &#8230; rename files from old extension to new extension in current directory &#8230;<\/p>\n<p><code><br \/>\n#!\/bin\/sh<br \/>\n# rname<br \/>\n# Usage: Expecting parameters of old extension and then new extension used to rename files in current directory<\/p>\n<p>if [ ! -z \"$1\" -a ! -z \"$2\" ]; then            # were parameters $1 and $2 both defined?<br \/>\n  e1=`echo $1 | sed '\/[.]\/s\/\/\/g'`         # rid any user usage confusion regarding . usage in extension ... sed is a \"buffer pipe editor\" (like tr)<br \/>\n  e2=`echo $2 | sed '\/[.]\/s\/\/\/g'`<br \/>\n  for f in `ls *.$e1`                              # The ` ` is preprocessed and $f is looped through containing each filename satisfying the old extension criteria<br \/>\n  do                                                                                      # Start of loop processing ...<br \/>\n   if [ -f \"`echo $f | sed '\/[.]$e1\/s\/\/.$e2\/g'`\" ]; then             # Start of if block asking if output file already existing ...<br \/>\n    echo \"Output `echo $f | sed '\/[.]$e1\/s\/\/.$e2\/g'` file already exists\"      # Warn user ... debatable \"business logic\" of script<br \/>\n   else<br \/>\n    mv $f `echo $f | sed '\/[.]$e1\/s\/\/.$e2\/g'`                        # Rename the file from old extension to new extension<br \/>\n   fi                                                                                      # ... End of if block<br \/>\n  done                                                                                 # ... End of loop processing<br \/>\nelse<br \/>\n  echo \"Was expecting parameters of old extension and then new extension used to rename files in current directory\"   # Advise usage RE usage<br \/>\nfi<br \/>\nexit<\/p>\n<p><\/code><\/p>\n<p>Here is a link with fewer comments here you could rename to <a target=_blank title='rname' href='http:\/\/www.rjmprogramming.com.au\/Linux\/Rename\/rname._GETME'>rname<\/a><\/p>\n<p>Here is the second renaming scenario &#8230; called &#8220;mvfiles&#8221; &#8230; rename files from old extension to new  nonexistant subdirectory to rename files in current directory &#8230;<\/p>\n<p><code><br \/>\n#!\/bin\/sh<br \/>\n# mvfiles<br \/>\n# Usage: Expecting parameters of old extension and then new nonexistant subdirectory to rename files in current directory<\/p>\n<p>if [ ! -z \"$1\" -a ! -z \"$2\" -a ! -d \"$2\" ]; then   # were parameters $1 and $2 both defined, as well as parameter $2 not existing as a subdirectory (debatable)<br \/>\n  e1=`echo $1 | sed '\/[.]\/s\/\/\/g'`        # rid any user usage confusion regarding . usage in extension ... sed is a \"buffer pipe editor\" (like tr)<br \/>\n  mkdir $2                                          # create the non-existant parameter $2 subdirectory off the current directory<br \/>\n  for f in `ls *.$e1`                             # The ` ` is preprocessed and $f is looped through containing each filename satisfying the old extension criteria<br \/>\n  do                                                                                    # Start of loop processing ...<br \/>\n    mv $f $2                        # Rename the file from old extension to that newly created (previously)  non-existant parameter $2 subdirectory<br \/>\n  done                                                                                # ... End of loop processing ...<br \/>\nelse<br \/>\n  echo \"Was expecting parameters of old extension and then new nonexistant subdirectory to rename files in current directory\"   # Advise usage RE usage<br \/>\nfi<br \/>\nexit<\/p>\n<p><\/code><\/p>\n<p>Here is a link with fewer comments here you could rename to <a target=_blank title='mvfiles' href='http:\/\/www.rjmprogramming.com.au\/Linux\/Rename\/mvfiles._GETME'>mvfiles<\/a><\/p>\n<p>Today&#8217;s tutorial is based on a Yahoo Answers question <a target=_blank title='Yahoo Answers question' href='http:\/\/au.answers.yahoo.com\/question\/index?qid=20140303094603AAkez88'>here<\/a>.<\/p>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d6632' onclick='var dv=document.getElementById(\"d6632\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"http:\/\/www.rjmprogramming.com.au\/wordpress\/?tag=Linux\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d6632' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Linux (or Unix) is a command line environment that is really powerful regarding file management. This power is no surprise as the basis of Unix and hence, Linux, is that files are at the heart of the design of the &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/rename-scenarios-in-linux-shell-script-primer-tutorial\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,29,37],"tags":[234,671,707,885,997,1139,1319,1339,1488],"class_list":["post-6632","post","type-post","status-publish","format-standard","hentry","category-elearning","category-operating-system","category-tutorials","tag-command-line","tag-kernel","tag-linux","tag-operating-system-2","tag-programming","tag-shell-script","tag-tutorial","tag-unix","tag-yahoo-answers"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/6632"}],"collection":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/comments?post=6632"}],"version-history":[{"count":0,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/6632\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=6632"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=6632"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=6632"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}