LoginSignup
0
0

More than 3 years have passed since last update.

【コピペOK】基本的なLinuxコマンド集【18選】

Last updated at Posted at 2020-11-24


最近、LAMP環境構築を行っているのですが、その中でLinuxコマンドに多く触れる機会があり、知識のストックも溜まってきたので、今回はコマンドの中でも頻出のものを解説していこうと思います。



予備知識

ディレクトリ

簡単に言えばフォルダのこと。

ルートディレクトリ

そのコンピュータ内の全ディレクトリ、ファイルを格納しているディレクトリ。

ホームディレクトリ

ユーザーが抱える全ディレクトリ、ファイルを格納しているディレクトリ。

カレントディレクトリ

現在作業しているディレクトリ。

絶対パス

ルートディレクトリから見たファイルパス。

相対パス

現在作業中のディレクトリから見たファイルパス。





参照系

pwd

ホームディレクトリから、現在作業中のファイルパスを表示。

【記述例】

$ pwd


【出力例】

/Users/username




ls

現在作業中のディレクトリの、保存されているファイルやディレクトリの一覧を表示。

【記述例】

<!-- 一覧を表示 -->
$ ls

<!-- 隠しファイル(. から始まる)も含めて表示 -->
$ ls -a

<!-- 権限などの詳細情報を表示 -->
$ ls -l

<!-- 隠しファイルも含めて権限などの詳細情報を表示 -->
$ ls -la


【出力例】

<!-- ls -->
Desktop             Sinatra
Documents           Sites
...

<!-- ls -a -->
.Trash              Documents
.bundle             Downloads
...

<!-- ls -l -->
drwx------@  5 username  username   160 11 14 16:12 Applications
drwx------@  9 username  username   288 11 24 19:55 Desktop
...

<!-- ls -la -->
drwx------   2 username  username     64 11 21 14:01 .Trash
drwxr-xr-x   4 username  username    128 10 20 23:12 .bundle
...




cat

ファイルの中身を表示。スクロールはできないので、1画面に収まるような分量の内容の場合に使用。

【記述例】

$ cat exampleFile


【出力例】

<!-- exampleFileの中身の記述 -->
これは見本です。




less

ファイルの内容を表示。スクロールが可能。1画面以上の分量の内容の場合に使用。

【記述例】

$ less exampleFile


【出力例】

<!-- exampleFileの中身の記述 -->
これは見本です。
.
.
.




history

実行したコマンドの履歴を表示。

【記述例】

<!-- コマンドの履歴を表示 -->
$ history

<!-- 最新のコマンドを5つ表示(数字は任意) -->
$ history 5

<!-- historyで表示されたナンバリングに対応するコマンドを実行(数字は任意) -->
$ !5

<!-- historyで表示されたナンバリングに対応するコマンドを履歴から削除(数字は任意) -->
$ history -d 5


【出力例】

<!-- history -->
  .
  .
  .
  848  pwd
  849  pwd
  850  ls
  851  ls -a
  852  ls -l
  853  ls -la
  854  cat
  855  ls

<!-- history 5 -->
  851  ls -a
  852  ls -l
  853  ls -la
  854  cat
  855  ls

<!-- !855(上記なら ls を実行) -->
Desktop             Sinatra
Documents           Sites
...

<!-- history -d 853 -->
<!-- 853 ls -laを削除 -->
  .
  .
  .
  851  ls
  852  ls -a
  853  ls -l
  854  cat
  855  ls






作成系

mkdir

カレントディレクトリ内に、ディレクトリを新規作成。

【記述例】

$ mkdir exampleFile




touch

カレントディレクトリ内に、ファイルを新規作成。

【記述例】

$ touch exampleFile






削除系

rm

ファイルやディレクトリの削除。通常は出力無しですが、ファイルやディレクトに記述や保存がされている場合、Y(yes)N(no) で実行確認を要求されます。

【記述例】

<!-- ファイルの削除 -->
$ rm exampleFile

<!-- ファイルの強制削除(確認無し) -->
$ rm -f exampleFile

<!-- ディレクトリの削除 -->
$ rm -r exampleDirectory

<!-- ディレクトリの強制削除 -->
$ rm -rf exampleDirectory




複製系

cp

ファイルやディレクトリのコピーを作成。

【記述例】

<!-- exampleFile を exampleFile2 としてコピー作成 -->
$ cp exampleFile exampleFile2

<!-- exampleDirectory を exampleDirectory2 としてコピー作成 -->
$ cp -r exampleDirectory exampleDirectory2




移動系

cd

カレントディレクトリ(現在作業中のディレクトリ)の移動。絶対パスか相対パスで指定可能。

【記述例】

<!-- ホームディレクトリに移動 -->
$ cd

<!-- exampleDirectory に作業場所を移動 -->
$ cd exampleDirectory

<!-- 一階層上のディレクトリに移動 -->
$ cd ..




mv

ファイルの移動や、ファイル名の変更が可能。絶対パスか相対パスで指定可能。

【記述例】

<!-- exampleDirectory1 にある exampleFile1 を exampleDirectory2 に移動 -->
$ mv exampleDirectory1/exampleFile1 exampleDirectory2

<!-- exampleFile2 の名前を exampleFile3 に変更 -->
$ mv exampleFile2 exampleFile3




便利系

clear

画面をリフレッシュする。

【記述例】

$ clear

または「control」+「L」でも可能。



shutdown

OSをシャットダウンする。

【記述例】

$ shutdown -h now






ユーザー系

useradd

ユーザーの新規追加。(この時、ユーザー用のホームディレクトリも作成される)

【記述例】

<!-- example と言うユーザー名でユーザー新規作成 -->
$ useradd example




passwd

新規作成したユーザーのパスワードを新規登録、既存のユーザーのパスワード変更。(本番環境では"鍵認証"と言う別の認証方法を利用する事が多い)

【記述例】

<!-- example と言うユーザー名のパスワードを変更 -->
$ passwd example




userdel

ユーザーの削除。

【記述例】

<!-- example ユーザーの削除 -->
$ userdel example

<!-- example ユーザーの削除と、削除したユーザーのホームディレクトリも削除 -->
$ userdel -r example




su

ユーザーの切り替え。

【記述例】

<!-- example ユーザーに切り替え(カレントディレクトリは同じ) -->
$ su example

<!-- example ユーザーに切り替え(そのユーザーのホームディレクトリから開始) -->
$ su - example




exit

ユーザーのログアウト。多重にログインしている場合は、元のユーザーに戻る。

【記述例】

<!-- 現在使用しているユーザーのログアウト -->
$ exit






まとめ

Linuxディストリビューション(Linux系のOS)での環境構築などをする際は、頻繁にLinuxコマンドを使用するので、解説した基本的なコマンドは、ある程度使い慣れておくと役立ちます。

macなら「ターミナル」
Windowsなら「Power Shell」
これらで実行できますので、ぜひお試しください。

最後までご覧いただき、ありがとうございました!





筆者:yuki|学習10日目で初案件獲得→現在はフルスタックエンジニア転職に向けて学習中
Qiita:https://qiita.com/yuki4839
Twitter:https://twitter.com/yuki35522891

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