2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Linux: ファイルの文字コードやタブ文字、改行等の特殊文字を確認する方法

Posted at

たまに必要となるのでメモ。

実施環境:
Linux
[testuser@testhost ~]$ uname -a
Linux testhost 4.18.0-147.8.1.el8_1.x86_64 #1 SMP Thu Apr 9 13:49:54 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
[testuser@testhost ~]$ echo $SHELL
/bin/bash

バッチで上手くコンフィグが読み込めない時等、
不具合が起こった時に実は文字コードや改行文字が原因だった、というのは
しばしば発生する事象です。
今回はそのような時に使用するコマンドをまとめます。

文字コード:

文字コードを確認したい場合は、fileコマンドを使用します。

Linux
file ファイル名

以下、実行例です。対象のファイルの文字コードがASCIIであることがわかります。

Linux
[testuser@testhost ~]$ file test.txt
test.txt: ASCII text
タブ文字や改行文字等の特殊文字:

特殊文字について確認したい場合は、odコマンドを使用します。

Linux
od -c ファイル名

以下、実行例です。改行文字がLF(\n)であること、タブ文字(\t)が含まれていることがわかります。

Linux
[testuser@testhost ~]$ cat test.txt
        1
        2
[testuser@testhost ~]$ od -c test.txt
0000000                            1  \n  \t   2  \n
0000015
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?