0

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].

1

2 Answers 2

0

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

2
  • Thanks but it failed: bash-3.2$ for f in *", attachment"; do > mv "$f" "${f%, attachment}" > done mv: rename *, attachment to *: No such file or directory
    – aksani56
    Jun 16, 2014 at 10:16
  • This worked : for f in *", attachment.mov"; do mv "$f" "${f%, attachment.mov}"; done
    – aksani56
    Jun 16, 2014 at 10:18
0

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.

2
  • Any Idea how to make it work on MacOS?
    – aksani56
    Jun 16, 2014 at 9:46
  • have you read the other question on this subject? brew install rename ?
    – pataluc
    Jun 16, 2014 at 9:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .