-1

I need a little help with a bash command in Ubuntu 14.04.

I have a folder with many subfolders and some of the folder- and filenames have leading or tailing whitespaces. I like to rename these folders and files, but with the same filename, just without the whitespace an the beginning and the end of the filename. Like a recursive file-rename or something like that. I've tried it with sed, but i don't get it running like it should.

I hope somebody can help me.

Nimderp
  • 3
  • 3

1 Answers1

0

This should do:

shopt -s extglob
while IFS= read -r -d '' f; do
    d=${f%/*} b=${f##*/*([[:space:]])} b=${b%%+([[:space:]])}
    echo mv -v -- "$f" "$d/$b"
done < <(
    find -depth \( -name '[[:space:]]*' -o -name '*[[:space:]]' \) -print0
)

As written, it won't do anything, it'll only echo the mv that will be performed. Remove the echo if you're happy with that.

gniourf_gniourf
  • 44,650
  • 9
  • 93
  • 104