LoginSignup
1
2

More than 3 years have passed since last update.

Linuxのお勉強メモ

Last updated at Posted at 2020-01-18

この記事 is 何?

  • エンジニア歴13年で何故か避けて通れて来てしまったLinuxの知識が必要な時が来てしまった。
  • 現場だけだとキャッチアップしきれない(というか、知らずにコマンドミスってインシデント発生が怖すぎる)ので、自主学習する。
  • やったことをメモに残しておく。アウトプット大事。
  • 記事を分けるのが面倒くさいので、何も気にせず追記や修正をします。

(いちおう)編集履歴

  • 2020/01/18:新規作成
  • 2020/01/19:ディレクトリ操作とファイル操作を追記
  • 2020/01/20:権限変更について追記

1. 事前準備

特に理由もなくCentOSを使うことにした。
コマンドが気軽に試せればいいし。
また、いつぶっ壊してもいいようにDockerを利用することにした。
Dockerのお勉強にもなるので一石二鳥。やったぜ。

(1) やったこと

以下のサイトを参考にして環境構築。
https://www.orangeitems.com/entry/2018/06/18/153510

自分のPCはwindows10HomeだったのでHyper-Vが使えなかった。
さらに以下のサイトも参考にして環境構築した。
https://qiita.com/idani/items/fb7681d79eeb48c05144


Windowsのコンソールに戻りたい時もあるかもしれないので、バックグラウンドで動かしとく。

$ docker run -i -d -t centos bin/bash
5e2e721950e466cfd1583d8db34e72908a848209ff7f0aef71357b3884a257d6

起動でけた。

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
5e2e721950e4        centos              "bin/bash"          7 seconds ago       Up 6 seconds                            fervent_jones

centOSちゃんコンニチハ。

$ docker attach 5e2e721950e4 
[root@5e2e721950e4 /]#

2. お勉強メモ

(1) とりあえず、どんなディレクトリがあるのか見てみよう。

[root@5e2e721950e4 /]# pwd
/
[root@5e2e721950e4 /]# ls -la
total 56
drwxr-xr-x   1 root root 4096 Jan 18 07:46 .
drwxr-xr-x   1 root root 4096 Jan 18 07:46 ..
-rwxr-xr-x   1 root root    0 Jan 18 07:46 .dockerenv
lrwxrwxrwx   1 root root    7 May 11  2019 bin -> usr/bin
drwxr-xr-x   5 root root  360 Jan 18 07:46 dev
drwxr-xr-x   1 root root 4096 Jan 18 07:46 etc
drwxr-xr-x   2 root root 4096 May 11  2019 home
lrwxrwxrwx   1 root root    7 May 11  2019 lib -> usr/lib
lrwxrwxrwx   1 root root    9 May 11  2019 lib64 -> usr/lib64
drwx------   2 root root 4096 Jan 13 21:48 lost+found
drwxr-xr-x   2 root root 4096 May 11  2019 media
drwxr-xr-x   2 root root 4096 May 11  2019 mnt
drwxr-xr-x   2 root root 4096 May 11  2019 opt
dr-xr-xr-x 131 root root    0 Jan 18 07:46 proc
dr-xr-x---   2 root root 4096 Jan 13 21:49 root
drwxr-xr-x  11 root root 4096 Jan 13 21:49 run
lrwxrwxrwx   1 root root    8 May 11  2019 sbin -> usr/sbin
drwxr-xr-x   2 root root 4096 May 11  2019 srv
dr-xr-xr-x  13 root root    0 Jan 18 07:46 sys
drwxrwxrwt   7 root root 4096 Jan 13 21:49 tmp
drwxr-xr-x  12 root root 4096 Jan 13 21:49 usr
drwxr-xr-x  20 root root 4096 Jan 13 21:49 var

内容はこんな感じらしい。先人に超感謝。
https://qiita.com/twipg/items/d8d7a0797a392bc4b75e

ここまでの疑問

  • # is 何?

    • コマンドの先頭についてる#は今はrootユーザーだとという印。
    • 一般ユーザーの場合は$になる。
  • -> is 何?

    • シンボリックリンクが張られている状態。
    • ハードリンクと言うものもあるようだ。
    • ファイル操作のお勉強するときに実際に試そう。
    • ググったら分かったので割愛。

(2) ディレクトリやファイル操作をしてみよう。

ディレクトリやファイルを作ったり消したりしてみる。

ディレクトリの操作

試したコマンド

# ディレクトリの新規作成
mkdir 作りたいディレクトリ名

# 親子階層のディレクトリを新規作成
mkdir -p 作りたいディレクトリ名/作りたいディレクトリ名

# ディレクトリのコピー
cp -r コピー元ディレクトリ コピー先ディレクトリ

# 中身が空のディレクトリの削除
rmdir 削除したいディレクトリ

# 中身があるディレクトリの削除
rm -rf 削除したいディレクトリ

カレントディレクトリに新しいディレクトリを作ってみる。

[root@6b65b4917285 tmp]# pwd
/tmp
[root@6b65b4917285 tmp]# mkdir directory1

directory1が出来てた。

[root@6b65b4917285 tmp]# ls -l
total 12
drwxr-xr-x 2 root root 4096 Jan 19 05:47 directory1

親子階層のディレクトリを作ってみようとしたら怒られた。
親ディレクトリが無いと作れないらしい。

[root@b12193fa42d0 tmp]# mkdir dir1/subdir1
mkdir: cannot create directory 'dir1/subdir1': No such file or directory

-pオプションを指定すると、親子ディレクトリを同時に作れる。

[root@b12193fa42d0 tmp]# mkdir -p dir1/subdir1
[root@b12193fa42d0 tmp]# ls -l dir1/
total 4
drwxr-xr-x 2 root root 4096 Jan 19 07:25 subdir1

ディレクトリをコピーしてみようとしたら、-rオプションつけろって怒られた。

[root@b12193fa42d0 tmp]# cp directory1 directory_copy
cp: -r not specified; omitting directory 'directory1'

-rオプションをつけたらコピーできた。

[root@b12193fa42d0 tmp]# cp -r directory1 directory_copy
[root@b12193fa42d0 tmp]# ls -l
total 24
drwxr-xr-x 3 root root 4096 Jan 19 07:25  dir1
drwxr-xr-x 2 root root 4096 Jan 19 07:35  directory1
drwxr-xr-x 2 root root 4096 Jan 19 07:42  directory_copy    # ディレクトリがコピーされた。

ディレクトリを削除してみる。

[root@b12193fa42d0 tmp]# ls -l
total 24
drwxr-xr-x 3 root root 4096 Jan 19 07:25  dir1
drwxr-xr-x 2 root root 4096 Jan 19 07:35  directory1    # こいつを消してみる。
drwxr-xr-x 2 root root 4096 Jan 19 07:42  directory_copy
[root@b12193fa42d0 tmp]# rmdir directory1
# 消えた。
[root@b12193fa42d0 tmp]# ls -l
total 20
drwxr-xr-x 3 root root 4096 Jan 19 07:25  dir1
drwxr-xr-x 2 root root 4096 Jan 19 07:42  directory_copy

中身があるディレクトリを消そうとしたら怒られた。

[root@b12193fa42d0 tmp]# rmdir dir1
rmdir: failed to remove 'dir1': Directory not empty

rm -rで消せた。(ただし、いろいろ確認される。)

[root@b12193fa42d0 tmp]# rm -r dir1
rm: descend into directory 'dir1'? y
rm: remove directory 'dir1/subdir1'? y
rm: remove directory 'dir1'? y
[root@b12193fa42d0 tmp]# ls -l
total 16
drwxr-xr-x 2 root root 4096 Jan 19 07:42  directory_copy

rm -rfだと確認無しで消える。

[root@b12193fa42d0 tmp]# rm -rf dir1
[root@b12193fa42d0 tmp]# ls -l
total 16
drwxr-xr-x 2 root root 4096 Jan 19 07:42  directory_copy

ファイル操作

試したコマンド

# ファイルの新規作成
touch 作りたいファイル名

# ファイル作ってそのまま編集(viで開く)
vi 作りたいファイル名

# ファイルのコピー
cp コピー元ファイル コピー先ファイル

# ファイルの削除
rm -rf 削除したいディレクトリ

カレントディレクトリに新しいファイルを作ってみる。

[root@b12193fa42d0 tmp]# touch file.txt
[root@b12193fa42d0 tmp]# ls -l file.txt
-rw-r--r-- 1 root root 0 Jan 19 08:04 file.txt

viでも作れる。

[root@b12193fa42d0 tmp]# vi file2.txt
# viが開くので、適当に編集して保存。
# 保存しないとファイルは作られない。
[root@b12193fa42d0 tmp]# ls -l file2.txt
-rw-r--r-- 1 root root 5 Jan 19 08:09 file2.txt

同じディレクトリ内でファイルをコピーしてみる

[root@b12193fa42d0 tmp]# ls -l
total 16
drwxr-xr-x 2 root root 4096 Jan 19 07:42 directory_copy
-rw-r--r-- 1 root root    0 Jan 19 08:04 file.txt    # こいつをコピーする。
-rw-r--r-- 1 root root    5 Jan 19 08:09 file2.txt
-rwx------ 1 root root  671 Jan 13 21:49 ks-script-_srt3u3c
-rwx------ 1 root root 1379 Jan 13 21:49 ks-script-gpqu_kuo
[root@b12193fa42d0 tmp]# cp file.txt file_copy.txt
[root@b12193fa42d0 tmp]# ls -l
total 16
drwxr-xr-x 2 root root 4096 Jan 19 07:42 directory_copy
-rw-r--r-- 1 root root    0 Jan 19 08:04 file.txt
-rw-r--r-- 1 root root    5 Jan 19 08:09 file2.txt
-rw-r--r-- 1 root root    0 Jan 19 08:14 file_copy.txt    # コピーできてる。

別のディレクトリにコピーしてみる。

[root@b12193fa42d0 tmp]# ls -l
total 20
drwxr-xr-x 2 root root 4096 Jan 19 08:17 dir
drwxr-xr-x 2 root root 4096 Jan 19 07:42 directory_copy
-rw-r--r-- 1 root root    0 Jan 19 08:04 file.txt    # こいつをdir/配下にコピーする。
-rw-r--r-- 1 root root    5 Jan 19 08:09 file2.txt
-rw-r--r-- 1 root root    0 Jan 19 08:14 file_copy.txt
[root@b12193fa42d0 tmp]# cp file.txt  dir/copy.txt
[root@b12193fa42d0 tmp]# ls -l dir/copy.txt
-rw-r--r-- 1 root root 0 Jan 19 08:17 dir/copy.txt    # コピーできてる。

ファイルを削除してみる

[root@b12193fa42d0 tmp]# ls -l
total 20
drwxr-xr-x 2 root root 4096 Jan 19 08:17 dir
drwxr-xr-x 2 root root 4096 Jan 19 07:42 directory_copy
-rw-r--r-- 1 root root    0 Jan 19 08:04 file.txt
-rw-r--r-- 1 root root    5 Jan 19 08:09 file2.txt
-rw-r--r-- 1 root root    0 Jan 19 08:14 file_copy.txt
[root@b12193fa42d0 tmp]# rm file2.txt
rm: remove regular file 'file2.txt'? y    # 消していいか確認される。
# 消えた。
[root@b12193fa42d0 tmp]# ls -l
total 16
drwxr-xr-x 2 root root 4096 Jan 19 08:17 dir
drwxr-xr-x 2 root root 4096 Jan 19 07:42 directory_copy
-rw-r--r-- 1 root root    0 Jan 19 08:04 file.txt
-rw-r--r-- 1 root root    0 Jan 19 08:14 file_copy.txt

-rfオプションを付けると確認無しで消せる。

[root@b12193fa42d0 tmp]# rm -rf file_copy.txt
[root@b12193fa42d0 tmp]# ls -l
total 16
drwxr-xr-x 2 root root 4096 Jan 19 08:17 dir
drwxr-xr-x 2 root root 4096 Jan 19 07:42 directory_copy
-rw-r--r-- 1 root root    0 Jan 19 08:04 file.txt

(3) ファイルの権限をいじってみよう。

ls -lしたときの謎の英文字羅列ってなんぞ?

-rw-r--r--ってなによ?

-rw-r--r-- 1 root root    5 Jan 20 05:59 file.txt

こういう意味。

文字数 意味
1文字目 ファイル種別 - : ファイル
d:ディレクトリ
l:シンボリックリンク
2~4文字目 所有者の権限 r:読み取り
w:書き込み
x:実行
5~7文字目 所有グループの権限 r:読み取り
w:書き込み
x:実行
8~10文字目 その他の人の権限 r:読み取り
w:書き込み
x:実行

なので-rw-r--r--であれば、
- ファイル
- 所有者は「読み込みと書き込み」の権限を所有
- 所有グループは「読み込み」の権限を所有
- その他の人は「読み込み」の権限を所有

権限の付け外しをしてみる。

やり方は以下の2種類。

  1. 数字で指定する。
  2. アルファベットで指定する。
chmod 付けたい権限 ファイルパス

# 付けたい権限で指定する数値は以下の通り。
#   4:r
#   2:w
#   1:x
# rとwを付けたければ、「4+2」で6を指定する。

# アルファベットの場合は
# 1文字目:変更対象(u:ユーザー g:グループ o:その他 a:全て)
# 2文字目:変更方法(=:指定した権限に置き換える +:指定した権限を付与する -:指定した権限を消す)
# 3文字目:変更内容(r:読み込み w:書き込み x:実行)

ユーザーに全ての権限を権限を付けてみる。

# 数字で指定。
[root@2e524500bb5e tmp]# ls -l file.txt
-rw-r--r-- 1 root root 5 Jan 20 05:59 file.txt
[root@2e524500bb5e tmp]# chmod 744 file.txt
[root@2e524500bb5e tmp]# ls -l file.txt
-rwxr--r-- 1 root root 5 Jan 20 05:59 file.txt

# アルファベットで指定。
[root@2e524500bb5e tmp]# ls -l file.txt
-rw-r--r-- 1 root root 5 Jan 20 05:59 file.txt
[root@2e524500bb5e tmp]# chmod u+x file.txt
[root@2e524500bb5e tmp]# ls -l file.txt
-rwxr--r-- 1 root root 5 Jan 20 05:59 file.txt

ユーザーから実行権限を外してみる。

# 数字で指定。
[root@2e524500bb5e tmp]# ls -l file.txt
-rwxr--r-- 1 root root 5 Jan 20 05:59 file.txt
[root@2e524500bb5e tmp]# chmod 644 file.txt
[root@2e524500bb5e tmp]# ls -l file.txt
-rw-r--r-- 1 root root 5 Jan 20 05:59 file.txt

# アルファベットで指定。
[root@2e524500bb5e tmp]# ls -l file.txt
-rwxr--r-- 1 root root 5 Jan 20 05:59 file.txt
[root@2e524500bb5e tmp]# chmod u-x file.txt
[root@2e524500bb5e tmp]# ls -l file.txt
-rw-r--r-- 1 root root 5 Jan 20 05:59 file.txt

複数の権限を同時につけてみる。
その他の人に最強の権限(読み書き実行)を付けてみる。

# 数字で指定。
[root@2e524500bb5e tmp]# ls -l file.txt
-rw-r--r-- 1 root root 5 Jan 20 05:59 file.txt
[root@2e524500bb5e tmp]# chmod 647 file.txt
[root@2e524500bb5e tmp]# ls -l file.txt
-rw-r--rwx 1 root root 5 Jan 20 05:59 file.txt

# アルファベットで指定。
[root@2e524500bb5e tmp]# ls -l file.txt
-rw-r--r-- 1 root root 5 Jan 20 05:59 file.txt
[root@2e524500bb5e tmp]# chmod o+wx file.txt
[root@2e524500bb5e tmp]# ls -l file.txt
-rw-r--rwx 1 root root 5 Jan 20 05:59 file.txt

# これでもいける。(=指定して置き換え)
[root@2e524500bb5e tmp]# ls -l file.txt
-rw-r--r-- 1 root root 5 Jan 20 05:59 file.txt
[root@2e524500bb5e tmp]# chmod o=rwx file.txt
[root@2e524500bb5e tmp]# ls -l file.txt
-rw-r--rwx 1 root root 5 Jan 20 05:59 file.txt

個人的にはアルファベット指定の方が解りやすいかなー

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