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

リダイレクト">"を使用してファイルに出力できなかったので調べて対応した

Posted at

課題

Linux標準教科書を読んで">"でファイルに出力しようとしたが以下のメッセージが出力された。

$ sudo echo "Hello" > ls-result
bash: ls-result: 許可がありません

">”のところで、権限がないユーザーによりファイル出力をするため。エラーメッセージが出力された。

解決法

まず、ls-resultファイルの権限を確認

-rw-r--r-- 1 root root       889  5月 18 23:51 ls-result

その他のユーザーには読み取りしかないことを知った。

teeコマンドを使った。

  • teeコマンドとは出力した結果をファイルに出力でき、結果も返してくれるコマンド

つまり、echoコマンドで出力させる"Hello"を引数としてteeを実行すればよい。
こんな感じでやってみた。

echo "Hello" | sudo tee ls-result
[sudo] ユーザ名 のパスワード: 
Hello

まとめ

">”を使う際にはユーザーが現在のユーザーになってしまい、sudoでrootユーザー">”以降を実行できないため注意が必要。

参考文献

以下を参考にして学習しました。
ありがとうございます。
sudoで">"でファイル出力しようとすると、Permission deniedエラーが出る時の対処法

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