shell - Batch TRIM (RENAME) a specific keyword from set of files

07
2014-07
  • aksani56

    I have few files namely - "a.mov-, attachment", "b.mov-, attachment", "c.mov-, attachment",etc.. I want to trim the keyword: "-, attachment" from all files in that folder.

    Please suggest me an approach which script will be best suited for this - AppleScript, Shell, Python etc. (I being a non-scripting guy).

    Note: I m using MacOSX-Maverics[Terminal].

  • Answers
  • glenn jackman

    Using just the shell:

    for f in *", attachment"; do
        mv "$f" "${f%, attachment}"
    done
    

    The form ${f%something} returns the value of the variable f with the text following the % removed from the end of the value.

    $ var="hello world"
    $ echo "${var%orld}"
    hello w
    

    If you put a glob pattern in there, the shortest match will be removed with ${var%pattern} and the longest match with ${var%%pattern}

    $ echo "${var%l*}"
    hello wor
    $ echo "${var%%l*}"
    he
    

    If the pattern does not match the end of the string, nothing is removed

    $ echo "${var%foo}"
    hello world
    

    See http://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion

  • pataluc

    you can use rename command if you're using linux:

    rename 's/-, attachment//' *attachment

    with a GUI, there is a lot of tools like ANT Renamer for example.


  • Related Question

    osx - Mac OS X: Update Python for Shell
  • Nathan G.

    Possible Duplicate:
    Using Python on Mac

    So, I see similar questions, but none of the answers work for me.

    I updated Python to 3.1.3 from 2.6.1. Everything works, except: When I type python into Terminal, I get:

    Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49)
    [GCC 4.2.1 (Apple Inc. build 5646)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>>

    So, how do I change the version of Python that runs in the Shell? I've tried the script that they provide. It adds their directory to my $PATH, but it still doesn't change the version that'd displayed from Terminal. Here's what I get when I echo $PATH:

    /Library/Frameworks/Python.framework/Versions/3.1/bin:/Library/Frameworks/Python.framework/Versions/3.1/bin:/Library/Frameworks/Python.framework/Versions/3.1/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin

    It appears that the script provided has added their directory for every time I ran the script (I tried it a few times, naturally).

    Here's some caps of what is in the other relevant folders it mentions:

    /Library/Frameworks/Python.framework/Versions/3.1/bin

    enter image description here

    /usr/local/bin

    enter image description here

    /usr/bin

    enter image description here


  • Related Answers
  • Daniel Beck

    Just type python3. You might need to change your $PATH by editing ~/.bash_profile:

    PATH="/Library/Frameworks/Python.framework/Versions/3.1/bin:${PATH}"
    export PATH
    

    They did this for compatibility reasons, Python 3 breaks things.


    You could define a shell function or alias to map python to python3, this way old scripts would continue to run, and you can type python and get version 3.

    Add to .bash_profile:

    alias python='python3'
    

    /usr/bin/env python continues to provide Python 2.