4
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.

sudoで">"でファイル出力しようとすると、Permission deniedエラーが出る時の対処法

Last updated at Posted at 2020-04-27

権限がないユーザがsudoでコマンドを実行して、かつ、">"でファイル出力する場合、ファイル出力のところで権限がないユーザが実行する事になるので、Permission deniedのエラーが出ます。

$ echo aiueo > hoge.txt
-bash: hoge.txt: Permission denied

$ sudo echo aiueo > hoge.txt
-bash: hoge.txt: Permission denied

解決するには、パイプでsudo teeに渡してあげる。teeコマンドは標準入力から受け取った内容を、標準出力とファイルに書き出すコマンド。

$ echo aiueo | sudo tee hoge.txt
aiueo

参考サイト
sudo でファイルへのテキスト書き込み

4
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
4
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?