Linuxコマンドの基本操作まとめ

Linuxとは

Linuxは、オープンソースUnixオペレーティングシステムの一つ。 多くのディストリビューションと呼ばれるバリエーションがあり、それぞれ異なる機能やパッケージを提供している。 安定性、セキュリティ、柔軟性に優れ、特にサーバー環境で広く使用されている。

Linuxコマンドとは?

Linuxコマンドは、Linuxオペレーティングシステムを操作するためのテキストベースのコマンドラインユーティリティ。 Linuxコマンドライン環境を使用することで、ファイルとディレクトリの操作、プロセスの管理、システム設定の変更など、さまざまなタスクを実行できる。 Linuxコマンドはターミナルウィンドウを通じて入力され、テキストベースの応答を提供する。

以下に、よく使用される基本的なLinuxコマンドを紹介します。

cd(Change Directory)

ディレクトリを移動するコマンド 例:

$ cd <ディレクトリ名>

実行結果:

ozri@OzrinoMacBook-ea ~ % cd Documents
ozri@OzrinoMacBook-ea Documents %

pwd(Print Working Directory)

現在のディレクトリ(currentディレクトリ)が表示される 例:

$ pwd

実行結果:

ozri@OzrinoMacBook-ea Documents % pwd
/Users/ozri/Documents

ls(List)

例:

$ ls

実行結果:

ozri@OzrinoMacBook-ea Documents % ls
Chap        Picture     Ruby_practice   app     js_practice
HTMLtest    Rails_practice  Todo        docker      rails_docker

オプション:

ls -a

隠しファイルを含むすべてのファイルとディレクトリを表示する。 隠しファイルはファイル名が.で始まるもの。

ls -l

各ファイル、フォルダの詳細情報を表示する

mkdir(Make Directory)

例:

$ mkdir <ディレクトリ名>

実行結果:

ozri@OzrinoMacBook-ea Documents % mkdir TEST1
ozri@OzrinoMacBook-ea Documents % ls
Chap        Picture     Ruby_practice   Todo        docker      rails_docker
HTMLtest    Rails_practice  TEST1       app     js_practice

オプション: -p 復数階層のディレクトリを1回で作成できる

ozri@OzrinoMacBook-ea Documents % mkdir -p TEST2/2023/09

rmdir(Remove Directory)

空のディレクトリを削除する 例:

$ rmdir <ディレクトリ名>

実行結果:

ozri@OzrinoMacBook-ea Documents % rmdir TEST1
ozri@OzrinoMacBook-ea Documents % ls
Chap        Picture     Ruby_practice   Todo        docker      rails_docker
HTMLtest    Rails_practice  TEST2       app     js_practice

オプション: -i 対象ファイルを本当に削除してよいか、確認のための問い合わせをします。誤って削除することを防ぐために利用される。処理を行なうときの確認

cat(Concatenate)

ファイルの中身を表示する 例:

$ cat <ファイル名>

実行結果:

ozri@OzrinoMacBook-ea Todo % cat index.html

オプション:

cat -n

行番号が表示される

less

ファイルの中身をスクロール表示する 中身が長いファイルの時に良い 例:

$ less <ファイル名>

実行結果:

ozri@OzrinoMacBook-ea Documents % less index.html
コマンド 動作
f 一画面下にスクロール
b 一画面上にスクロール
j 一行下にスクロール
k 一行上にスクロール
q lessコマンド終了
/ 下に向かって検索
? 上に向かって検索
n 次の検索結果へ移動
N 前の検索結果へ移動

tail

ファイルの終わり部分を標準出力する オプションをつけない場合は末尾から10行を出力 例:

$ tail [オプション]<ファイル名>

実行結果:

ozri@OzrinoMacBook-ea Documents % tail example.txt
1から
222
3
4
5
6
7
8
9
10
ozri@OzrinoMacBook-ea Documents %

touch

空のファイルを作成する 例:

$ touch <新規ファイル名>
$ touch <新規ファイル名 1><新規ファイル名 2>

実行結果:

ozri@OzrinoMacBook-ea Documents % touch README.md

rm(Remove)

ファイル、ディレクトリを削除する 完全削除されるので気を付けること 例:

$ rm <ファイル名>
$ rm <ファイル名1><ファイル名2>
$ rm -r <ディレクトリ名>

実行結果:

ozri@OzrinoMacBook-ea Documents % rm README.md
ozri@OzrinoMacBook-ea Documents % rm -r TEST2

ディレクトリを削除する時は-rオプションをつける

mv(Move)

ファイルの移動、ファイル名をrenameする 例:

$ mv <古いファイル名><新しいファイル名>
$ mv <ファイル名><移動先ディレクトリ/>
$ mv <ディレクトリ名><移動先ディレクトリ/>

実行結果:

ozri@OzrinoMacBook-ea TEST2 % mkdir dir
ozri@OzrinoMacBook-ea TEST2 % touch file
ozri@OzrinoMacBook-ea TEST2 % mv file file1
ozri@OzrinoMacBook-ea TEST2 % mv file1 dir/

cp(Copy)

ファイル、ディレクトリをコピーする 例:

$ cp <ファイル名><コピー後のファイル名>
$ cp <ファイル名><コピー先ディレクトリ名/>
$ cp -r <ディレクトリ名><コピー後のディレクトリ名/>

実行結果:

ozri@OzrinoMacBook-ea TEST2 % mkdir dir
ozri@OzrinoMacBook-ea TEST2 % touch file
ozri@OzrinoMacBook-ea TEST2 % cp file new_file
ozri@OzrinoMacBook-ea TEST2 % cp file dir/
ozri@OzrinoMacBook-ea TEST2 % cp -r dir new_dir

その他: ディレクトリをコピーする時は-rオプションを使用する

ファイルやディレクトリにリンクを作成する これは、他のファイルやディレクトリを参照する仕組みで、Windowsのショートカットのようなイメージ * ハードリンク:同じデータを複数の場所で参照するために使用。元のファイルを削除しても消えない * シンボリックリンク:別のファイルやディレクトリを参照する。リンク先がファイルの実体なので元のファイルを削除すると参照不可になる

例:

$ ln <リンク元ファイル名><リンク名>
$ ln -s <リンク元ファイル名><リンク名>

実行結果:

ozri@OzrinoMacBook-ea TEST2 % touch file
ozri@OzrinoMacBook-ea TEST2 % ln file file_hard
ozri@OzrinoMacBook-ea TEST2 % ln -s file file_symbolic
ozri@OzrinoMacBook-ea TEST2 % echo "hello" > file
ozri@OzrinoMacBook-ea TEST2 % ls
file        file_hard   file_symbolic
ozri@OzrinoMacBook-ea TEST2 % rm file
ozri@OzrinoMacBook-ea TEST2 % cat file_hard
hello
ozri@OzrinoMacBook-ea TEST2 % cat file_symbolic
cat: file_symbolic: No such file or directory

find

ファイルがどのディレクトリにあるかの検索 例:

$ find パス -name <ファイル名>

実行結果:

ozri@OzrinoMacBook-ea TEST2 % find . -name '*file*' -print
./file_hard
./file_symbolic

chmod(Change Mode)

ファイルやディレクトリのパーミッションを設定する 例:

$ chmod [ugoa][+-=][rwx]<ファイル名>

実行結果

$ chmod u+x file.txt
$ chmod 755 file.txt
記号 内容
u 所有者権限
g グループ権限
o その他のユーザ権限
a 全ユーザ権限
+ 権限追加
- 権限削除
= 記述権限

ファイルのパーミッション確認

ozri@OzrinoMacBook-ea TEST2 % ls -l
-rw-r--r--  1 ozri  staff  6  9 21 21:17 file_hard

[rw-] [r--] [r--]の3塊で分けて読む[オーナー][グループ][その他] ※パーミッションには記号がある(r:read, w:write, e:execute)

chown(Change Ownership)

ファイルの所有者を変更する 例:

$ chown 所有者 <ディレクトリ名 or ファイル名>

実行結果:

$ ozri@OzrinoMacBook-ea TEST2 % chown newowner index.html

ps(Process Status)

実行中のプロセスを表示する PID:プロセスID CMD:実行中のコマンド 例:

$ ps
$ ps aux

実行結果:

ozri@OzrinoMacBook-ea TEST2 % ps
  PID TTY           TIME CMD
75684 ttys002    0:00.34 -zsh
76579 ttys003    0:00.14 -zsh

オプション: aux:全ユーザのプロセスを詳細情報を併せて表示

kill

ジョブやプロセスを終了させるコマンド フォアグラウンドのジョブはCtrl+Cで終了するが、停止中やバックグラウンドのジョブはkillコマンド 例:

$ kill %<ジョブID>
$ kill <プロセスID>
$ kill <シグナル名><プロセスID>

実行結果:

ozri@OzrinoMacBook-ea / % sleep 100 &
[1] 93197
ozri@OzrinoMacBook-ea / % jobs
[1]  + running    sleep 100
ozri@OzrinoMacBook-ea / % kill %1
ozri@OzrinoMacBook-ea / %
[1]  + terminated  sleep 100
ozri@OzrinoMacBook-ea / % jobs
ozri@OzrinoMacBook-ea / %

参考

https://www.udemy.com/course/unscared_linux/

https://linuc.org/textbooks/linux/