    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>Linux.com :: Bash script help</title>
        <link>http://www.linux.com/archive/forums/topic/1569</link>
        <description>Everything Linux and Open Source</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Linux.com Admin</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.linux.com" /> 
        <admin:errorReportsTo rdf:resource="mailto:noreply@linux.com" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>



<div class="xarbb-noteswrapper">
</div>

   <div class="xarbb-noteswrapper">
       <div class="xar-sub rightnote">
           <div>
                    <a href="http://www.linux.com/archive/forums/topic/1625">
Previous Topic</a>
|
                    <a href="http://www.linux.com/archive/forums/topic/1585">
Next Topic</a>
</div>

           <div>
                    <a href="http://www.linux.com/archive/forums/printtopic/1569?theme=print">
Print this topic</a> |
You are not subscribed            </div>
        </div>
        <div style="clear: both;"></div>
    </div>

    <h1>
        <a class="xar-title" href="http://www.linux.com/archive/forums/topic/1569">Bash script help</a>
   </h1>


   <div class="xarbb-topiccontrols">
<form action="http://www.linux.com/archive/forums?func=jump" method="post">
   <label for="xarbb_jump_menu">Forum Jump</label>
    <select name="f" id="xarbb_jump_menu" onchange="this.form.submit();">
        <optgroup label="Options">
            <option value="5" selected="selected">
Please select forum            </option>
            <option value="5">--------------------</option>
        </optgroup>
        <optgroup label="Forums">
                <option value="1">New to Linux</option>
                <option value="2">Hardware</option>
                <option value="3">Linux Distributions</option>
                <option value="4">Applications</option>
                <option value="5">Programming and Development</option>
                <option value="6">Miscellaneous Discussion</option>
                <option value="7">Linux.com Writers Forum</option>
                <option value="8">Jobs</option>
        </optgroup>
    </select>
    <input type="submit" class="button" value="Jump" />
</form>
<div style="clear: both;"></div>
   </div>

   <div class="xarbb-topiccontrols">

<span class="xar-sub">
    <a href="http://www.linux.com/archive/forums">
Forum Index    </a>
    <span style="font-size: larger;">&#187;</span>
    <a href="http://www.linux.com/archive/forums/category/4181">
        Forums 
    </a>
    <span style="font-size: larger;">&#187;</span>
    <a href="http://www.linux.com/archive/forums/forum/5">
        Programming and Development   </a>
</span>
<div style="clear: both;"></div>
   </div>

<div class="xarbb-tablewrapper">



   <table>
       <tr>
           <th scope="col">
Author</th>
           <th scope="col">
Message</th>
       </tr>



            <tr>
                <td class="xar-norm author">
                    <div>
                        <a href="http://www.linux.com/archive/roles/361318">jeffelkins</a>
                   </div>
                   <div class="xar-sub">
Joined: Jan 01, 1970<br/>Posts: 4                        <br />
                        <a href="http://www.linux.com/archive/forums?by=361318&amp;func=searchtopics">
Other Topics</a>
                   </div>
               </td>

               <td class="xar-norm message">
                   <div class="xarbb-messagecontrols">
                       <div class="leftnote">
                           <span class="xar-sub">
Posted Apr 22, 2008 at 1:26:09 PM</span>
                       </div>



<div style="float: left;">
                           <strong class="xar-sub">
Subject: Bash script help                            </strong>
                        </div>
                        <div style="clear: both;"></div>
                    </div>

                    <div>
                        I need a bash script to loop through a directory and do a filename change on each item: specifically on each file remove the last character prior to the extension. i.e. file1a.txt becomes file1.txt - The logic of accomplishing this escapes me. Or, is there a way to do this with mmv? The looping is no problem, it's zapping that last pre-extension character.

Thanks!</div>

                </td>
            </tr>

            <tr>
                <td class="xar-norm author">
                    <a href="http://www.linux.com/archive/forums/topic/1569#top">
Back to top</a>
</td>
               <td class="xar-alt">

                        <a href="http://www.linux.com/archive/roles/361318">
                            <img src="modules/xarbb/xarimages/new/icon_profile.gif" alt="Profile" />
                        </a>
                        <a href="http://www.linux.com/archive/?module=roles&amp;func=email&amp;uid=361318">
                            <img src="modules/xarbb/xarimages/new/icon_email.gif" alt="Email" />
                       </a>
</td>
           </tr>





           <tr>
               <td class="xar-norm author">
                   <strong>

                            <a href="http://www.linux.com/archive/roles/379079">
                                Shashank Sharma                           </a>
</strong>

                   <span class="xar-sub">
<br />
Joined Jan 01, 1970<br/>Posts: 1657<br />
Location:New Delhi, India<br />
                        <br />
                        <a href="http://www.linux.com/archive/forums?by=379079&amp;func=searchtopics">
Other Topics                        </a>
                    </span>
                </td>

                <td class="xar-accent message">
                   <div class="xarbb-messagecontrols">
                       <div class="leftnote">
                           <span class="xar-sub">
Posted:
                                Apr 28, 2008 5:48:45 PM                           </span>
                       </div>
                       <div class="rightnote">
</div>
                       <div style="float:left;">
                           <strong class="xar-sub">
Subject:
                                <a name="1188573" href="http://www.linux.com/archive/forums/topic/1569#1188573">
                                    Bash script help                                </a>
                            </strong>
                        </div>
                        <div style="clear: both;"></div>
                    </div>

                    <div style="clear: left;">
                        Is there any order to the file names? I mean, do all files have 5 character names, and you want to drop the fifth character? Do all files have same extension (or same length extension, like .jpg, .txt, .png, .mpg)? Is the character to be removed the same for all files?

This link might give you some ideas:

http://www.ivorde.ro/How_to_remove_first_last_character_from_a_string_using_SED-75.html<p class="hiddensignature">
Coauthor of <a href="http://www.amazon.com/Beginning-Fedora-Novice-Professional/dp/1590598555">Beginning Fedora: From Novice to Professional</a> published by <a href="http://www.apress.com">Apress</a>.
<br><br>
Please follow the <a href="http://www.linux.com/forums/topic/1665">Forum Guidelines</a></p>
                    </div>
                </td>
            </tr>

            <tr>
                <td class="xar-norm author">
                    <a href="http://www.linux.com/archive/forums/topic/1569#top">
Back to top</a>
</td>

               <td class="xar-alt">

                            <a href="http://www.linux.com/archive/roles/379079">
                                <img src="modules/xarbb/xarimages/new/icon_profile.gif" alt="Profile" />
                            </a>
                            <a href="http://www.linux.com/archive/?module=roles&amp;func=email&amp;uid=379079">
                                <img src="modules/xarbb/xarimages/new/icon_email.gif" alt="Email" />
                           </a>
                                <a href="http://linuxlala.net/">
                                    <img src="modules/xarbb/xarimages/new/icon_www.gif" alt="Website" />
                               </a>
                                <a href="http://edit.yahoo.com/config/send_webmesg?.target=linuxlala&amp;.src=pg">
                                    <img src="modules/xarbb/xarimages/new/icon_yim.gif" alt="Yahoo!" />
                               </a>
</td>
           </tr>

           <tr>
               <td class="xar-norm author">
                   <strong>

                            <a href="http://www.linux.com/archive/roles/386930">
                                linuxdynasty                           </a>
</strong>

                   <span class="xar-sub">
<br />
Joined Aug 07, 2007<br/>Posts: 47<br />
                        <br />
                        <a href="http://www.linux.com/archive/forums?by=386930&amp;func=searchtopics">
Other Topics                        </a>
                    </span>
                </td>

                <td class="xar-norm message">
                   <div class="xarbb-messagecontrols">
                       <div class="leftnote">
                           <span class="xar-sub">
Posted:
                                Apr 29, 2008 4:03:36 AM                           </span>
                       </div>
                       <div class="rightnote">
</div>
                       <div style="float:left;">
                           <strong class="xar-sub">
Subject:
                                <a name="1188596" href="http://www.linux.com/archive/forums/topic/1569#1188596">
                                    Re:  Re:  Re:  Re:  Re:  Bash script help                                </a>
                            </strong>
                        </div>
                        <div style="clear: both;"></div>
                    </div>

                    <div style="clear: left;">
                        I can not help you with a shell script but I can help you do it in python script..

I will post this on my site as well.
You can check it out here http://linuxdynasty.org

[code=xml]#!/usr/bin/env python
import os, re, sys
os.chdir(sys.argv[1])
ls = os.listdir('./')
for file_o in ls:
  file_n = re.sub(&quot;\w\.&quot;, &quot;.&quot;, file_o)
  os.rename(file_o, file_n)
print os.listdir(&quot;./&quot;)
[/code]
[Modified by: linuxdynasty on April 28, 2008 11:26 PM]

[Modified by: linuxdynasty on April 28, 2008 11:43 PM]

[Modified by: linuxdynasty on April 28, 2008 11:44 PM]

[Modified by: linuxdynasty on April 29, 2008 12:58 PM]
                    </div>
                </td>
            </tr>

            <tr>
                <td class="xar-norm author">
                    <a href="http://www.linux.com/archive/forums/topic/1569#top">
Back to top</a>
</td>

               <td class="xar-alt">

                            <a href="http://www.linux.com/archive/roles/386930">
                                <img src="modules/xarbb/xarimages/new/icon_profile.gif" alt="Profile" />
                            </a>
                            <a href="http://www.linux.com/archive/?module=roles&amp;func=email&amp;uid=386930">
                                <img src="modules/xarbb/xarimages/new/icon_email.gif" alt="Email" />
                           </a>
                                <a href="http://www.linuxdynasty.org">
                                    <img src="modules/xarbb/xarimages/new/icon_www.gif" alt="Website" />
                               </a>
</td>
           </tr>

           <tr>
               <td class="xar-norm author">
                   <strong>

                            <a href="http://www.linux.com/archive/roles/402478">
                                xi                           </a>
</strong>

                   <span class="xar-sub">
<br />
Joined May 02, 2008<br/>Posts: 5<br />
                        <br />
                        <a href="http://www.linux.com/archive/forums?by=402478&amp;func=searchtopics">
Other Topics                        </a>
                    </span>
                </td>

                <td class="xar-accent message">
                   <div class="xarbb-messagecontrols">
                       <div class="leftnote">
                           <span class="xar-sub">
Posted:
                                May 02, 2008 3:59:42 AM                           </span>
                       </div>
                       <div class="rightnote">
</div>
                       <div style="float:left;">
                           <strong class="xar-sub">
Subject:
                                <a name="1188879" href="http://www.linux.com/archive/forums/topic/1569#1188879">
                                    Re:  Bash script help                                </a>
                            </strong>
                        </div>
                        <div style="clear: both;"></div>
                    </div>

                    <div style="clear: left;">
                        [code=xml]for i in *.* ; do
  extension=&quot;${i##*.}&quot;
  newname=&quot;${i%?.*}&quot;
  mv &quot;$i&quot; &quot;$newname.$extension&quot;
done[/code]                    </div>
                </td>
            </tr>

            <tr>
                <td class="xar-norm author">
                    <a href="http://www.linux.com/archive/forums/topic/1569#top">
Back to top</a>
</td>

               <td class="xar-alt">

                            <a href="http://www.linux.com/archive/roles/402478">
                                <img src="modules/xarbb/xarimages/new/icon_profile.gif" alt="Profile" />
                            </a>
                            <a href="http://www.linux.com/archive/?module=roles&amp;func=email&amp;uid=402478">
                                <img src="modules/xarbb/xarimages/new/icon_email.gif" alt="Email" />
                           </a>
                                <a href="http://">
                                    <img src="modules/xarbb/xarimages/new/icon_www.gif" alt="Website" />
                               </a>
</td>
           </tr>

           <tr>
               <td class="xar-norm author">
                   <strong>

                            <a href="http://www.linux.com/archive/roles/401286">
                                Vassil Dichev                           </a>
</strong>

                   <span class="xar-sub">
<br />
Joined Apr 10, 2008<br/>Posts: 1<br />
                        <br />
                        <a href="http://www.linux.com/archive/forums?by=401286&amp;func=searchtopics">
Other Topics                        </a>
                    </span>
                </td>

                <td class="xar-norm message">
                   <div class="xarbb-messagecontrols">
                       <div class="leftnote">
                           <span class="xar-sub">
Posted:
                                May 02, 2008 8:25:00 AM                           </span>
                       </div>
                       <div class="rightnote">
</div>
                       <div style="float:left;">
                           <strong class="xar-sub">
Subject:
                                <a name="1188892" href="http://www.linux.com/archive/forums/topic/1569#1188892">
                                    Bash script help                                </a>
                            </strong>
                        </div>
                        <div style="clear: both;"></div>
                    </div>

                    <div style="clear: left;">
                        As Larry Wall said, There's More Than One Way To Do It (you'll see it often as TMTOWTDI). So here's my version:

Some distributions have a &quot;rename&quot; utility. The problem is, it's not very consistent across distributions: some don't have it and some have a different syntax. Debian-based distributions (including Ubuntu) have the more powerful regular expression syntax, which you can use:

rename 's/(.*).\.(.+)/$1.$2/' *

Test with the &quot;-n&quot; option first to just see what would be done.

If you cannot have that, the simplest thing I could use is &quot;sed&quot;. First verify if it works right, because the regular expression can get hairy:

ls | sed &quot;s/\(.*\).\.\(.\+\)/mv \0 \1.\2/&quot;

Then if you're sure of the result, execute it by piping to &quot;sh&quot;:

ls | sed &quot;s/\(.*\).\.\(.\+\)/mv \0 \1.\2/&quot; | sh                    </div>
                </td>
            </tr>

            <tr>
                <td class="xar-norm author">
                    <a href="http://www.linux.com/archive/forums/topic/1569#top">
Back to top</a>
</td>

               <td class="xar-alt">

                            <a href="http://www.linux.com/archive/roles/401286">
                                <img src="modules/xarbb/xarimages/new/icon_profile.gif" alt="Profile" />
                            </a>
                            <a href="http://www.linux.com/archive/?module=roles&amp;func=email&amp;uid=401286">
                                <img src="modules/xarbb/xarimages/new/icon_email.gif" alt="Email" />
                           </a>
                                <a href="http://">
                                    <img src="modules/xarbb/xarimages/new/icon_www.gif" alt="Website" />
                               </a>
</td>
           </tr>

           <tr>
               <td class="xar-norm author">
                   <strong>

                            <a href="http://www.linux.com/archive/roles/361318">
                                jeffelkins                           </a>
</strong>

                   <span class="xar-sub">
<br />
Joined Jan 01, 1970<br/>Posts: 4<br />
                        <br />
                        <a href="http://www.linux.com/archive/forums?by=361318&amp;func=searchtopics">
Other Topics                        </a>
                    </span>
                </td>

                <td class="xar-accent message">
                   <div class="xarbb-messagecontrols">
                       <div class="leftnote">
                           <span class="xar-sub">
Posted:
                                May 05, 2008 5:08:52 PM                           </span>
                       </div>
                       <div class="rightnote">
</div>
                       <div style="float:left;">
                           <strong class="xar-sub">
Subject:
                                <a name="1189181" href="http://www.linux.com/archive/forums/topic/1569#1189181">
                                    Re:  Bash script help                                </a>
                            </strong>
                        </div>
                        <div style="clear: both;"></div>
                    </div>

                    <div style="clear: left;">
                        Thanks for the replies! I used the bash solution but all were interesting and educational.

Jeff                    </div>
                </td>
            </tr>

            <tr>
                <td class="xar-norm author">
                    <a href="http://www.linux.com/archive/forums/topic/1569#top">
Back to top</a>
</td>

               <td class="xar-alt">

                            <a href="http://www.linux.com/archive/roles/361318">
                                <img src="modules/xarbb/xarimages/new/icon_profile.gif" alt="Profile" />
                            </a>
                            <a href="http://www.linux.com/archive/?module=roles&amp;func=email&amp;uid=361318">
                                <img src="modules/xarbb/xarimages/new/icon_email.gif" alt="Email" />
                           </a>
</td>
           </tr>
</table>
</div>

<div class="xarbb-topiccontrols">

<span class="xar-sub">
    <a href="http://www.linux.com/archive/forums">
Forum Index    </a>
    <span style="font-size: larger;">&#187;</span>
    <a href="http://www.linux.com/archive/forums/category/4181">
        Forums 
    </a>
    <span style="font-size: larger;">&#187;</span>
    <a href="http://www.linux.com/archive/forums/forum/5">
        Programming and Development   </a>
</span>

</div>



            </channel>
</rss>
