2

I am trying to rename all unicode files name in ASCII.

I wanted to do something like this :

for file in `ls | egrep -v ^[a-z0-9\._-]+$`; do mv "$file" $(echo "$file" | slugify); done

But it doesn't work yet.

  • first, regexp ^[a-z0-9\._-]+$ doesn't seem to be enough.
  • second, slugify also transform the extension of the file so I have to cut the extension first and after put it back.

Any idea of a way to do that ?

Curious
  • 2,783
  • 3
  • 29
  • 45
Natim
  • 17,274
  • 23
  • 92
  • 150
  • you need to be more specific. Are you trying to rename all names with characters outside of ISO-646? ISO-8859-1? read 'man basename' for disassembling extensions. – bmargulies Sep 03 '12 at 19:01
  • I think this question is more appropriate for [Unix & Linux forum](http://unix.stackexchange.com) – Curious Sep 03 '12 at 19:05

1 Answers1

4

First thing first, don't parse the output of ls. That is, in general, a bad idea, especially if you're expecting files that have any sort of strange characters in their names.

Assuming slugify does what you want with filenames in general, try:

for file in * ; do
  if [ -f "$file" ] ; then
    ext=${file##*.}
    name=${file%.*}
    new_name=$(echo "$name"|slugify)
    if [[ $name != $new_name ]] ; then
      echo mv -v "$name.$ext" "$new_name.$ext"
    fi
  fi
done

Warning: this will fail if you have files without an extension (it'll double-up the filename). See this other answer by Doctor J if you need to handle that.

Community
  • 1
  • 1
Mat
  • 202,337
  • 40
  • 393
  • 406
  • 1
    The slugify command is available with `pip install slugify` here : https://github.com/zacharyvoase/slugify – Natim Sep 03 '12 at 19:44