← Back to context

Comment by nybble41

4 years ago

Without changing the design too much, you could rearrange it like so to avoid renaming multiple times and still have the option to just "add another line":

  # Rename all files in a directory
  rn() {
    rename \
      -e "s/ /-/g" \
      -e "s/_/-/g" \
      -e "s/–/-/g" \
      -e "s/://g" \
      -e "s/\(//g" \
      -e "s/\)//g" \
      -e "s/\[//g" \
      -e "s/\]//g" \
      -e 's/"//g' \
      -e "s/'//g" \
      -e "s/,//g" \
      -e "y/A-Z/a-z/" \
      -e "s/---/--/g" \
      -e "s/-‎--/--/g" \
      *
  }

Though I would at least take advantage of character classes to reduce the number of substitutions:

  # Rename all files in a directory
  rn() {
    rename \
      -e 's/[ _—]/-/g' \
      -e 's/[:\(\)\[\]",]//g' \
      -e "s/'//g" \
      -e 'y/A-Z/a-z/' \
      -e 's/--+/--/g' \
      *
  }

(I'm using the `rename` command provided by the `rename` Debian package, a.k.a `file-rename`. The options may vary if you're using a different version.)