How to remove trailing whitespace from file extensions and folders snow?

How can I remove trailing whitespace from file extensions and folders in snow leopard ?

Ideally I would be able to run a single command within a directory that would search and remove for all files and folders with trailing whitespace and their subfolders.

I couldn't seem to do this with automator.

It's a delicate operation I don't want to make mistakes with.

Help much appreciated.


Solution 1:

#!/bin/bash

IFS=$'\n'
find ~/Desktop -mindepth 1 -depth | while read f; do
    new="$(dirname "$f")/$(basename "$f" | sed 's/ *$//')"
    [[ "$f" != "$new" ]] && mv "$f" "$new"
done
  • Without IFS=$'\n', the f variables read by | while read f wouldn't have leading or trailing spaces
  • Changing sed 's/ *$//' to sed 's/^ *//;s/ *$// would remove spaces from the start as well