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

command not found が出たときに chmod +x / chmod 755 で対応した話(備忘録)

Posted at

発生した事象

シェルスクリプトを実行しようとしたところ、以下のエラーが出た。

$ ./sample.sh
bash: ./sample.sh: command not found

ファイル自体は存在しており、パスも間違っていないのに実行できない。

原因

実行権限が付与されていなかった。
Linux では、ファイルを実行するために

  • 読み取り権限(r)
  • 書き込み権限(w)
  • 実行権限(x)

のうち、x が必須

権限を確認すると、x が付いていなかった。

$ ls -l sample.sh
-rw-r--r-- 1 user user 1234 sample.sh

対処 ①:chmod +x を使う場合

chmod +x で実行権限を付与する。

$ chmod +x sample.sh

何をしているか

  • 既存の権限はそのまま
  • 実行権限(x)だけを追加する

特徴

  • 最小限の変更で済む
  • 安全
  • 「とりあえず実行できるようにしたい」場合に向いている

再度確認。

$ ls -l sample.sh
-rwxr-xr-x 1 user user 1234 sample.sh

その後、正常に実行できた。

$ ./sample.sh
(正常に実行される)

対処 ②:chmod 755 を使う場合

$ chmod 755 sample.sh

何をしているか

数値指定は以下の意味。

数値 権限
7 r(4) + w(2) + x(1)
5 r(4) + x(1)
5 r(4) + x(1)

つまり、

  • 所有者:読み・書き・実行
  • グループ:読み・実行
  • その他:読み・実行
-rwxr-xr-x

特徴

  • 権限を明示的に固定できる
  • 既存の権限は上書きされる
  • 本番用スクリプトや公開前の整理に向いている

chmod +x と chmod 755 の違いまとめ

コマンド 特徴 向いている場面
chmod +x 実行権限だけ追加 一時的な実行、手元検証
chmod 755 権限を完全指定 本番・共有スクリプト

まとめ

  • 実行できない = まず権限確認
  • 手早く直す → chmod +x
  • 権限を整理する → chmod 755
  • command not found は権限不足でも起きる
1
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
1
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?