[Osaka/Yokohama] Looking for infrastructure/server side engineers!

[Osaka/Yokohama] Looking for infrastructure/server side engineers!

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Successor to CentOS] AlmaLinux OS server construction/migration service

[Successor to CentOS] AlmaLinux OS server construction/migration service

[For WordPress only] Cloud server “Web Speed”

[For WordPress only] Cloud server “Web Speed”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Low cost] Wasabi object storage construction and operation service

[Low cost] Wasabi object storage construction and operation service

[Reservation system development] EDISONE customization development service

[Reservation system development] EDISONE customization development service

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Compatible with over 200 countries] Global eSIM “beSIM”

[Compatible with over 200 countries] Global eSIM “beSIM”

[Compatible with Chinese corporations] Chinese cloud / server construction, operation and maintenance

[Compatible with Chinese corporations] Chinese cloud / server construction, operation and maintenance

[YouTube] Beyond official channel “Biyomaru Channel”

[YouTube] Beyond official channel “Biyomaru Channel”

【超絶入門3分】できた!ディレクトリ作成と削除

こんにちは!
株式会社ビヨンド四国オフィスのペルシャ猫、いのうえです。

入社前、会社説明会で「りなっくす」なんていう聞いたこともない言葉を聞いて、脳内????
入社後、「Linux」とやらに出会い、かれこれお付き合い5ヶ月となりました。
今回は、入社当時お気に入りだったコマンド「mkdir」について!
mkdir」コマンドでディレクトリができるたび、嬉しくてニヤニヤ・・・(・´з`・)
そのため、ペルシャ猫のAWSのインスタンスには大量のディレクトリが作られて
あとで消すことが大変に・・・((; ゚Д゚))あわあわ
ということで、併せて「rm」「rmdir」についても書いていきますね!

mkdirコマンド

mkdir」とはディレクトリを作成するコマンドです。
まずは、オプションを付けずにディレクトリを作成してみます。

$ mkdir blog
$ls -l
drwxrwxr-x 2 ec2-user ec2-user  6 Aug  4 05:42 blog

「blog」という名前のディレクトリが作成されました。

次に「-m」オプションをつけて、ディレクトリ作成と同時に
ディレクトリのパーミッション(権限)を指定します。

$ mkdir -m 500 blog2
$ ls -l
dr-x------ 2 ec2-user ec2-user  6 Aug  4 05:51 blog2

パーミッションはこちら

r 読み込み
w 書き込み
x 実行

最後に「-p」オプションをつけて、親ディレクトリの下に子ディレクトリを同時に作成します。

$ mkdir -p blog3/cat
$ ls -l
drwxrwxr-x 3 ec2-user ec2-user 17 Aug  4 05:55 blog3
$ ls -l blog3
drwxrwxr-x 2 ec2-user ec2-user 6 Aug  4 05:55 cat

深いディレクトリを一気に作成することはできるのか検証。

$ cd blog3/cat
$ mkdir -p cat2/cat3/cat4/cat5

「cat」ディレクトリの下に「cat2~5」までの子ディレクトリを一気に作成できました。
この場合は「cat」の下に「cat2」の下に「cat3」・・・と続きます。

ディレクトリを削除する

rm」コマンドでディレクトリを削除するには、
再帰的にディレクトリツリーを削除する「-r」オプションを指定します。

$ cd cat2/cat3/cat4/
$ rm -r cat5
$ ls -l
total 0

この際、削除対象のディレクトリ「cat5」の中のファイルやディレクトリも一緒に削除されてしまいます。

$ rm -r cat2
$ ls -l
total 0
$ cd cat3
No such file or directory

「cat2」ディレクトリを削除すると、
その下の子ディレクトリ「cat3」も削除されたため「そんなディレクトリは存在しないよ」と言われてしまいます。
なので、不安な場合は、さらに「-i」オプションをつけることで、
削除を実行する前に「本当に削除してもいい?」という確認をしてくれるので
y(yes)」と入力すると削除され、「n(no)」と入力することで削除を中断することができます。便利!

$ rm -ri cat
remove directory ‘cat’?yes

中身が空のディレクトリを削除する場合は「rmdir」コマンドが使用できます。

$ rmdir blog3

rmdir」コマンドは、ディレクトリの中にファイルがある場合、
削除しようとしてもエラーになる
点が「rm」との大きな違いです。
「haruka」ディレクトリの中に「bydcat」というファイルが存在する場合に「rmdir」を実行してみると・・・

$ ls -l haruka
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug  4 06:52 bydcat
$ rmdir haruka
failed to remove ‘haruka’: Directory not empty

「ディレクトリの中にファイルがあるから、削除できないよ」と言われてしまいます。
こういった場合は「rm」コマンドを使う必要があるってことですね!

rmdir」コマンドの利点は、空のディレクトリを削除するコマンドなので、
先頭が「.」で始まる隠しファイルがあった場合に「消せないよ~」ということを言ってます。
そのため、隠しファイルに気づかず消してしまうという事故を防ぐことができます。

まとめ

作業効率を考えると、オプションって大切ですね!
子ディレクトリを1つずつなんて作ってたら、作業効率が悪い。
ブログを書くまで、正直言うと「rm」と「rmdir」の違いがはっきりと分かっていませんでした。
「要するに消すコマンドだよね~」くらいで(;'∀')
積極的にオプションを使うということもやってなかったので、改めて勉強にもなり、復習にもなりました。
こういった感じで、私のようなLinux初心者が「できた!」を実感できるブログを書けたらいいな~~~
ターミナル(真っ黒な画面)にもそろそろ慣れてきましたが、
ついついマウスをポチポチしちゃうCUIペルシャ猫。
今後も、トライアンドエラーとやらを繰り返し、
お気に入りのコマンドや、個人的にもっと理解を深めたいコマンドに関してブログを書いていきます。

日々成長、日々前進。
毎日、私自身をアップデートしていかなければ!!!
最後まで読んでくださって、ありがとうございます。

この記事がお役に立てば【 いいね 】のご協力をお願いいたします!
1
読み込み中...
1 票, 平均: 1.00 / 11
5,502
X facebook はてなブックマーク pocket
[2024.6.30 CentOS support ended] CentOS server migration solution

[2024.6.30 CentOS support ended] CentOS server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

The person who wrote this article

About the author

Akika Inoue

Belongs to the System Solutions Department.
He joined Beyond as a founding member of the Shikoku office.
I jumped into the IT industry with no experience. As an education team, we create curriculum and conduct training for new graduates, mid-career, and existing members.
The main business is server operation and maintenance.
Either way, we value your content.
Also belongs to the Web Content Division and YouTube Team.