Ask Different is a question and answer site for power users of Apple hardware and software. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Why doesn't this work?

(The automator is in swedish but the parts are: get selected finder objects - get folder contents - run shell script)

Printscreen Automator

I want to trim the first three characters of the filenames in a folder as part of an automator folder action. I don’t know shell scripting myself, but I’ve read around quite a bit and think this should do the trick.

I've also tried this with applescript: Automator Applescript

It works better but only with the first 8 files for some reason.

Does someone know how to sort this out? /Daniel

share|improve this question
up vote 3 down vote accepted

The reason your shell script isn't doing what you want is because Automator passes the full path of the files as arguments.

If the full path to one of the files is /Users/foo/Temp/file1.txt, then your script attempts to rename it to ers/foo/Temp/file1.txt, which isn't what you want.

Try this instead:

for f in "$@"; do
  FILENAME=$(basename "$f")
  DIRNAME=$(dirname "$f")
  mv "$f" "${DIRNAME}/${FILENAME:3}"

Better still (handles relative filenames correctly):

for f in "$@"; do
  FILENAME=$(basename "$f")
  DIRNAME=$(dirname "$f")
  if [ -z "$DIRNAME" ]; then
  mv "$f" "${DIRNAME}/${FILENAME:3}"
share|improve this answer
Lovely! Thank you very much! – danielfrisk Apr 16 '14 at 9:57

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.