How to remove leading whitespace from file and folder names?

Posted by timoto on Super User See other posts from Super User or by timoto
Published on 2012-03-26T23:51:07Z Indexed on 2012/10/04 21:42 UTC
Read the original article Hit count: 208

How to remove leading whitespace from file and folder names? (I'm running OS X 10.6 Snow Leopard.)

As provided below by @Lri I was able to remove trailing whitespace using this:

#!/bin/bash

IFS=$'\n'
for d in {1..9}; do
    find ~/Desktop -name '* ' -depth $d | while read f; do
        mv "$f" "$(sed 's/ *$//' <<< "$f")"
    done
done

Now I'm trying to remove leading whitespace with this:

#!/bin/bash

IFS=$'\n'
for d in {1..9}; do
    find ~/Desktop -name '* ' -depth $d | while read f; do
        mv "$f" "$(sed 's/^ *//;s/ *$//' <<< "$f")"
    done
done

but it doesn't work.

What am I doing wrong?

© Super User or respective owner

Related posts about osx-snow-leopard

Related posts about removal