0

Not actually a question, but a response to How to replace spaces in file names using a bash script; as I've not yet acrued enough 'reputation'. However, I too have recently suffered from the issue of 'rogue' character including pathnames arriving into secondary storage, that needed Ux fixing; spaces included. Expanding on Michael Krelin soln there, though, I was finally able to figure out an 'all Ux' command solution; no extra special code. So, as a follow on, check this out, as it is able to change any Ux 'roguely; named file/dir name, into one that is composed of only satisfactorily composed character string names. It is also able to, (and does) deal w/I18n modification capability, as well. It too, is able to clean up full directory tree worth of fns/dirnms. Have fun:

$ idx=0; find . -depth -name "*[ &;()]*" | while IFS= read -r pathNm ; do ((idx++)); printf "\n%d\t%s\t-->\n\t%s" "$idx" "$pathNm" "$(dirname "$pathNm" | tr '\050' '\137' | tr '\051' '\137')/$(basename "$pathNm" | tr '\040' '\055' | tr '\041' '\055' | tr '\042' '\055' | tr '\043' '\055' | tr '\044' '\055' | tr '\045' '\055' | tr '\046' '\055' | tr '\047' '\055' | tr '\050' '\137' | tr '\051' '\137' | tr '\052' '\055' | tr '\053' '\055' | tr '\054' '\055' | tr '\072' '\055' | tr '\073' '\055' | tr '\342' '\055' | tr '\200' '\055' | tr '\223' '\055' | sed s/[_-]/_/g | sed s/-_/_/g | sed 's/--/_/g' | sed s/\\.-/_/g | sed s/[_-]\\./_/g | sed 's!__!!g' )"; done
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
  • If this answers the question you're referring to, you should add it as an answer there (no rep requirements for that). If it is sufficiently different, you should post it as a separate question and then self-answer it. As it is now, the question isn't self-contained, and the question contains the answer instead of the answer being separate. – Benjamin W. Jan 02 '20 at 22:00
  • That script is horrendously unreadable. The partial sequence of `tr` commands `tr '\040' '\055' | tr '\041' '\055' | tr '\042' '\055' | tr '\043' '\055'` should be reduced to one: `tr '\040\041\042\043' '\055'` — that replaces blanks, `!`. `"`, and `#` with `-`. You can add the `-s` option to squeeze duplicate characters out. You can extend the mapping for all the search characters to be replaced by dash, and use another to handle the conversions to underscore (`\137`). – Jonathan Leffler Jan 02 '20 at 22:01
  • You could use `tr -s '_-'` to squeeze all sequences of dashes to a single dash and all sequences of underscores to a single underscore. Then maybe there'd be some chance of understanding the script. I'm not convinced that identifying the characters by octal escapes helps; there might be a need for a few of them, but not many. You've not accounted for control characters (code less than `\040`), or things like square brackets, backslashes, carets, curly braces, tilde which appear after the upper-case and lower-case alphabets. – Jonathan Leffler Jan 02 '20 at 22:05
  • Similarly, the sequence of `sed` commands "`sed s/[_-]/_/g | sed s/-_/_/g | sed 's/--/_/g' | sed s/\\.-/_/g | sed s/[_-]\\./_/g | sed 's!__!!g'`" should be one invocation of `sed`, thus: `sed -e 's/[_-]/_/g' -e 's/-_/_/g' -e 's/--/_/g' -e 's/\\.-/_/g' -e 's/[_-]\\./_/g' -e 's/__//g'` (and there are those who would argue that you could combine the whole lot into a single script argument with semicolons separating terms: `sed 's/[_-]/_/g;s/-_/_/g;s/--/_/g;s/\\.-/_/g;s/[_-]\\./_/g;s/__//g'`. I think the extra spacing provided by multiple `-e` options makes it easier to read. – Jonathan Leffler Jan 02 '20 at 22:11
  • With the judicious use of `tr -s`, some of the terms in the `sed` script may be unnecessary. (Incidentally, with `tr -s '_-'`, do not put the dash before the underscore; it becomes an unrecognized option to `tr`.) – Jonathan Leffler Jan 02 '20 at 22:13
  • Question must be question. Follow the tips that people you've said. – TigerTV.ru Jan 02 '20 at 22:48
  • 1
    The 30+ subshells spawned per-iteration should be reduced to the greatest extent possible. – David C. Rankin Jan 02 '20 at 22:56
  • I'm voting to close this question as off-topic because the OP claim to post an answer to a different question. Proposed answer got lot of negative comments. – dash-o Jan 04 '20 at 06:36

0 Answers0