2
0

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 3 years have passed since last update.

lessコマンド でファイル書き換えてみる

Posted at

lessコマンド でファイル書き換えてみる

lessコマンドとは?

テキストファイルを1画面ずつ表示するコマンド
less /var/log/messages で、messagesのログファイルを確認することができます。
大体、確認する際にlessを使うことが多いかと思います。

lessコマンドでファイルの書き換えをしてみる

  • サンプルファイルの準備
$ echo aaaaaaa > sample.txt
$ cat sample.txt 
aaaaaaa
  • サンプルファイルの書き換え
less sample.txt 
==========
aaaaaaa
==========

1.png

↑の less を開いた状態で v を押すと↓のように変換モードになる

aaaaaaa

2.png

test という文字列を追加

aaaaaaa
test

3.png

:wq で保存

aaaaaaa
test

4.png

less の状態で戻るので、閉じます。

確認すると変更されている

$ cat sample.txt 
==========
aaaaaaa
test
==========

対応策

重要なファイルをタイポで書き換えてしまう可能性があります。
書き換えれないように、 以下のようにcatコマンドを使用することで防ぐことができます。

cat sample.txt | less

ちなみに、↑のコマンドで v を押すと、以下のように編集できない旨表示されます。

5.png

2
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?