Renaming files with special characters

There are situations in wich a file containing special characters can be created in your file system, making it hard to remove or rename them as the file manager and even the console won’t escape the name correctly. If the file was created using UTF-8 there is no problem, but if it was first named using a different character set (say, SJIS for instance) or its name is stored in a descriptor (i.e. a ZIP file), Linux won’t be able to open it.

You can still get acces to it via its index node, without using the filename.

(一般コミック) [大友克洋] AKIRA 第01巻.zip

This file was created using Windows. KDE understands the japanese kanji and you can rename or delete it. But when you extract its contents, the file names stored inside the ZIP file do not have any character set associated, so the filesystem does not recognize them.


You can use find to rename them.

First, get their inode using ls -i

12323499 ?????S?t????1_????.png
12323351 ?????S?t????1_?-??.png
12323581 ?????S?t????1_???-?+??.png
12323418 ?????S?t????1_???-????.png
12323479 ?????S?t????1_???-?-??.png

Then using find …

$ find . -maxdepth 1 -inum 12323499  -exec mv '{}' ./akira_01.png \;

And so the file gets renamed.

To do this for a group of files in a subdirectory we will use a counter

$ let c=0;for i in `ls *.png -i|cut -d ' ' -f1`; \
do let c=c+1 ;  \
find *.png -maxdepth 1 -inum $i -exec mv '{}' ./AKIRA_01_$c.png \;  ; \

so files get their new readable name


Note: you must end the mv command given by -exec with ; before exiting the loop with ;done. Also, it is a good practice to prepend the semicolon with a \ to prevent the special characters messing with the mv.

Esta entrada fue publicada en english, GNU / linux. Guarda el enlace permanente.

Leave a Reply