9

I have several files with encoding issues in their file names (German umlauts, burned on CD with Windows, read by Windows and synced to Linux with Seafile. Something, somewhere went wrong...). Bash and zsh only show "?" instead of umlauts, stat shows something like

$ stat Erg�nzung.doc 
File: ‘Erg\344nzung.doc’
Size: 2609152         Blocks: 5096       IO Block: 4096   regular file
Device: 806h/2054d      Inode: 12321475    Links: 1

I can enter the filename only with autocompletion. How do I rename the file? The affected files seem to be unreadable by LibreOffice (or other programs for other file types), they complain about "No such file or device".

I was thinking about mv --by-inode 12321475 Ergänzung.doc, but there's no --by-inode switch for mv. What else can I do?

Michael Durrant
  • 41,213
  • 69
  • 165
  • 232
Jasper
  • 3,538
  • 4
  • 16
  • 22
  • 6
    Umm... you already figured out how to do it with `stat`; just do the same with `mv`. – psusi Oct 11 '15 at 13:37
  • 1
    I was sure that I tried this and got some error. Now it seems to work. But the `find -inum`-way is more reliable/easier if there are multiple files with the same prefix. – Jasper Oct 11 '15 at 13:59
  • Possible duplicate of [Rename files with all types of characters, with POSIX portability](http://unix.stackexchange.com/q/33756/80216), [How can we rename a file with semi-colon as part of the filename?](http://unix.stackexchange.com/q/33193/80216) and [Move a directory with a space as its name](http://unix.stackexchange.com/q/67758/80216). – G-Man Says 'Reinstate Monica' Oct 11 '15 at 15:05
  • 1
    See also for example [How to remove a file where the file name has utf-8 character issues](http://serverfault.com/q/537749/58408). – user Oct 11 '15 at 15:39
  • You said you can enter the filename with autocompletion. So why not `mv Erg Ergnzung.doc`? – user253751 Oct 12 '15 at 00:01
  • @immibis Consider a directory with many files sharing the same prefix. – Jasper Oct 12 '15 at 07:26

3 Answers3

15

You could try:

find . -inum 12321475 -exec mv {} new-filename \;

or

find . -inum 12321475 -print0 | xargs -0 mv -t new-filename

Generally I prefer xargs over exec. Google for why. It's tricky though. See Find -exec + vs find | xargs. Which one to choose?

Michael Durrant
  • 41,213
  • 69
  • 165
  • 232
3

There is a utility convmv for this type of problem. It allows you to change the encoding of a filename from eg windows cp1256 to utf8, etc.

meuh
  • 49,672
  • 2
  • 52
  • 114
3

Just for the record, the correct xargs -0 usage is:

find . -inum 12321475 -print0 | xargs -0 -I '{}' mv '{}' new-filename

but as already pointed out it wasn't necessary anyway.