Skip to content

Instantly share code, notes, and snippets.

@leftclickben
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leftclickben/6550e269edc7a64dfac3 to your computer and use it in GitHub Desktop.
Save leftclickben/6550e269edc7a64dfac3 to your computer and use it in GitHub Desktop.
Rename whitespace in files to underscores, with any level of nesting
maxdepth=`find . -printf '%d\\n' | sort -n | tail -1` && for depth in `seq 1 $maxdepth`; do find -mindepth 1 -maxdepth $depth -exec bash -c 'f=`echo "{}" | sed -r s:\\\s+:_:g` ; if [ "$f" != "{}" ]; then mv "{}" "$f"; fi' \; ; done
@leftclickben
Copy link
Author

I'm sure this isn't the shortest code to achieve the result, but it does handle the case where a file or directory with spaces is nested inside another directory with spaces (to any level of recursion). It does this simply by processing one level of depth at a time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment