osx - Batch APPEND (RENAME) a specific keyword to set of files

07
2014-07
  • aksani56

    I have few files namely - "a", "b", "c",etc.. I want to add the keyword: ".mov" to 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
  • Robbie Mckennie

    Something like this ought to work:

    #!/bin/sh
    for i in *; do
      mv "$i" "$i.mov"
    done
    

    Put that in the directory above the directory with your files. Use chmod +x script.sh, then from within the directory with your files, ../script.sh.

    EDIT: This assumes your 'set' of files is every file in that directory. If it isn't, you'll need to more clearly define your set.


  • Related Question

    linux - CLI: batch file rename -- with CONTENT ?? (e.g. specific text mentioned in file)
  • ajo

    I would like to batch rename files (*.txt) by inserting a number of the format 'RXR1234567' (RXR+7digits) [if such number (and ideally only one) is found in the text] at the front of the filename, e.g. instead of

    letter_235.txt
    

    the file should be called

    RXR1234567_letter_235.txt
    

    Could this be done from the command line (grep, rename)? The files are in various subdirectories.

    Your thoughts will be appreciated (as always).


  • Related Answers
  • cYrus

    Try this in the root directory:

    find -name '*.txt' -exec sh -c 'PREFIX=`grep -m 1 -oe "RXR[0-9]\{7\}" "$0"` && mv "$0" "${0%/*}/${PREFIX}_${0##*/}"' {} \;
    

    Note: If a file contains multiple matches of the pattern the first one will be used.