Skip to content
Home » Linux » How to Fix Garbled Filename and Text

How to Fix Garbled Filename and Text

There're several ways to display your garbled filename correctly.

  1. LANG Environment Variable
  2. PuTTY Character Set
  3. Rename Badly Encoded Filename

A. LANG Environment Variable

Tried to list a file which name is made by a multi-byte language like this.

[root@test ~]# ll
total 8
-rw-r--r--  1 root root  217 Oct 17 00:39 ▒?▒▒?▒▒?▒.txt
...

This is because we use an improper character set in our shell, which is usually set in LANG environment variable which determines the current locale for language, territory and character set.

[root@test ~]# echo $LANG
en_US.ISO88591

Please note that, while LANG is used by OS to set environment locale, NLS_LANG is only used by Oracle to set session NLS settings.

To correct the garbled filename, you have to set a multi-byte character set for the environment, say UTF8. Then we list the file again.

[root@test ~]# export LANG=en_US.UTF8
[root@test ~]# ll
total 8
-rw-r--r--  1 root root  217 Oct 17 00:39 リンゴ.txt

To match the language of the filename, we may also set LANG like this:

[root@test ~]# export LANG=ja_JP.UTF8
[root@test ~]# ll
合計 8
-rw-r--r--  1 root root  217 10月 17 00:39 リンゴ.txt

This time, we also changed the interface language by setting the legal LANG value ja_JP.UTF8.

For getting all possible values of locale, you can use locale -a.

[root@test ~]# locale -a

B. PuTTY Character Set

Sometimes, you may have set the locale correctly, but it still showed the garbled filename and text.

[root@test ~]# ll
total 8
...
-rw-r--r--  1 root root  217 Oct 17 00:39 ãªã³ã´.txt
[root@test ~]# echo $LANG
en_US.UTF8

This is a terminal's problem, because it did not decode the output character string in the correct way. We have to change the decoding character set of the terminal. For example, PuTTY, the most popular terminal for administrators. Let's see how we change the character set while connection is still alive.

First of all, right click on the title bar of the terminal and you will see a function menu.

PuTTY - Right Click on Title Bar
PuTTY - Right Click on Title Bar

Next, click on "Change Settings" on the menu.

PuTTY - Click "Change Settings" on the Menu
PuTTY - Click "Change Settings" on the Menu

The setting page will be shown like below, then click on "Translation".

PuTTY - Click "Translation" on Settings
PuTTY - Click "Translation" on Settings

Change the character set into "UTF-8" on the list.

PuTTY - Click "UTF-8" on Character Set List
PuTTY - Click "UTF-8" on Character Set List

Apply the change.

PuTTY - Click "Apply" on Character Set List
PuTTY - Click "Apply" on Character Set List

Then, we can see the correctly decoded filename and text.

[root@test ~]# ll
total 8
...
-rw-r--r--  1 root root  217 Oct 17 00:39 リンゴ.txt
PuTTY - Correct Filename Display
PuTTY - Correct Filename Display

C. Rename Badly Encoded Filename

If the filename was badly encoded in the first place, then you can rename it into a meaningful one.

Check inode number of the file

[root@test ~]# ls -li
total 8
402653338 -rw-r--r--  1 root root  217 Oct 17 00:39 ãªã³ã´.txt
...

Next, we use find command with inode number to identify the file, then it pipes the filename to the execution string, which renames the file into apple.txt.

[root@test ~]# find . -inum 402653338 -exec mv {} apple.txt \;

List the file name.

[root@test ~]# ls -li
total 8
402653338 -rw-r--r--  1 root root  217 Oct 17 00:39 apple.txt
...

Good! This is what we want.

Leave a Reply

Your email address will not be published. Required fields are marked *