ドットインストールのUNIXコマンド入門 [一般ユーザー編] を一通り真似して実行した備忘録

タイトルの通りです。 私の備忘録ですのでところどころ飛ばしながらかいています。正しい情報を知りたい方はドットインストールをご確認お願いいします。

https://dotinstall.com/lessons/basic_unix_v2


現在のパス

[vagrant@localhost ~]$ pwd
/home/vagrant
  • clear または Ctrl+l

画面表示をリセット

f:id:okumuraa1:20190105232252g:plain

  • cd

ディレクトリ移動

[vagrant@localhost ~]$ cd unix_lessons/
[vagrant@localhost unix_lessons]$ 
[vagrant@localhost ~]$ cd unix_lessons/
[vagrant@localhost unix_lessons]$ cd - # 一つ前のディクトリに戻れる
/home/vagrant
  • mkdir

ディレクトリ作成

[vagrant@localhost unix_lessons]$ mkdir myqpp
[vagrant@localhost unix_lessons]$ ls
myqpp
  • cp

コピー

[vagrant@localhost unix_lessons]$ cp -r myqpp myqpp2 # -r でディクトリコピー
[vagrant@localhost unix_lessons]$ ls
myqpp  myqpp2
[vagrant@localhost unix_lessons]$ mkdir -p myqpp3/config # -p で途中のディレクトリがない場合作成する
[vagrant@localhost unix_lessons]$ ls myqpp3
config
  • mv

移動

$ mv myqpp3 myqpp2

f:id:okumuraa1:20190105233911p:plain

  • rmdir

ディレクトリ削除

[vagrant@localhost unix_lessons]$ rmdir myqpp2/myqpp3/config
[vagrant@localhost unix_lessons]$ ls myqpp2/myqpp3/
[vagrant@localhost unix_lessons]$ 
  • rm

削除

[vagrant@localhost unix_lessons]$ rm -r myqpp2 # -r でフォルダ内も全て削除
[vagrant@localhost unix_lessons]$ ls
myqpp
  • cat

ファイルの中身を見る

$ cat myqpp/hello.txt 
1.

hello. hello. hello. hello. hello. hello. 
(省略)
  • less

長いファイルを表示する時に便利

$ less myqpp/hello.txt

f:id:okumuraa1:20190105234618p:plain

    • スペースCtrl + fで一画面先
    • Ctrl + bで一画面前
    • gでファイルの先頭
    • Shift + gでファイルの末尾
    • qでlessの終了
  • history

コマンドの履歴

[vagrant@localhost unix_lessons]$ history
(省略)
  408  cd ..
  409  cd -
  410  cd home/vagrant/
  411  cd unix_lessons/
  412  history
  413  pwd
  414  history
[vagrant@localhost unix_lessons]$ !413 # historyで表示された番号のコマンドを実行できる
pwd
/home/vagrant/unix_lessons
[vagrant@localhost unix_lessons]$ !! # 直前のコマンドを実行
pwd
/home/vagrant/unix_lessons
[vagrant@localhost unix_lessons]$ !-2 # 2つ前のコマンドを実行
history
(省略)

[vagrant@localhost unix_lessons]$ ls myqpp/
hello.txt
[vagrant@localhost unix_lessons]$ cd !$ # 直前のコマンドの引数を使用できる
cd myqpp/
[vagrant@localhost myqpp]$ cd ..
[vagrant@localhost unix_lessons]$ !pw # 直近でpwから始まるコマンドを実行
pwd
/home/vagrant/unix_lessons
[vagrant@localhost unix_lessons]$ !pw:p # 直近でpwから始まるコマンドを表示だけして実行はしない
pwd
[vagrant@localhost unix_lessons]$ !!
pwd
/home/vagrant/unix_lessons

ヘルプ表示

$ mkdir --help
Usage: mkdir [OPTION]... DIRECTORY...
Create the DIRECTORY(ies), if they do not already exist.

Mandatory arguments to long options are mandatory for short options too.
  -m, --mode=MODE   set file mode (as in chmod), not a=rwx - umask
  -p, --parents     no error if existing, make parent directories as needed
  -v, --verbose     print a message for each created directory
  -Z                   set SELinux security context of each created directory
                         to the default type
      --context[=CTX]  like -Z, or if CTX is specified then set the SELinux
                         or SMACK security context to CTX
      --help     display this help and exit
      --version  output version information and exit

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Report mkdir translation bugs to <http://translationproject.org/team/>
For complete documentation, run: info coreutils 'mkdir invocation'
  • man

ヘルプ表示

$ man ls

f:id:okumuraa1:20190106001035p:plain

  • vi

エディタを起動

$ vi hello.txt

f:id:okumuraa1:20190106001327p:plain

$ pwd
/home/vagrant/unix_lessons/myqpp
$ mkdir -p config/production/database
$ ln -s config/production/database/ dbconfig # シンボリックリンク作成
$ ls -l # シンボリックリンクが作成されていることを確認
total 8
drwxrwxr-x. 3 vagrant vagrant 4096 Jan  5 15:16 config
lrwxrwxrwx. 1 vagrant vagrant   27 Jan  5 15:16 dbconfig -> config/production/database/
-rw-rw-r--. 1 vagrant vagrant 3149 Jan  5 14:44 hello.txt
$ touch dbconfig/commands.sql # リンクしているところにファイルを作成
$ ls dbconfig/
commands.sql
$ ls config/production/database/ # リンク元を見てもファイルが作成されている
commands.sql
$ rm dbconfig/commands.sql # リンクしているところで削除
$ ls config/production/database/ # リンク元を見ても削除されている
$ unlink dbconfig # シンボリックリンク削除
$ ls
config  hello.txt
  • コマンド作成
$ type hi # hiというコマンドがないことを確認
-bash: type: hi: not found
$ type cat # コマンドが存在した場合は情報が表示される
cat is hashed (/usr/bin/cat)
$ vi hi
$ cat hi
#!/bin/bash
echo "hi!"

$ ls -l
total 12
drwxrwxr-x. 3 vagrant vagrant 4096 Jan  5 15:16 config
-rw-rw-r--. 1 vagrant vagrant 3149 Jan  5 15:21 hello.txt
-rw-rw-r--. 1 vagrant vagrant   24 Jan  5 15:27 hi
$ chmod u+x hi # 実行権限を与える
$ ls -l
total 12
drwxrwxr-x. 3 vagrant vagrant 4096 Jan  5 15:16 config
-rw-rw-r--. 1 vagrant vagrant 3149 Jan  5 15:21 hello.txt
-rwxrw-r--. 1 vagrant vagrant   24 Jan  5 15:27 hi # <- 実行権限が付いていることを確認
$ ./hi
hi!
  • PATHを通す
$ echo $PATH # 現在設定されているパスを確認
/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/vagrant/.local/bin:/home/vagrant/bin
$ printenv # 環境変数の一覧をみれる
(省略)
PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/vagrant/.local/bin:/home/vagrant/bin
PWD=/home/vagrant/unix_lessons/myqpp
LANG=en_US.UTF-8
SELINUX_LEVEL_REQUESTED=
HISTCONTROL=ignoredups
SHLVL=1
HOME=/home/vagrant
LOGNAME=vagrant
SSH_CONNECTION=10.0.2.2 59837 10.0.2.15 22
LC_CTYPE=UTF-8
LESSOPEN=||/usr/bin/lesspipe.sh %s
XDG_RUNTIME_DIR=/run/user/1000
_=/usr/bin/printenv
$ pwd
/home/vagrant/unix_lessons/myqpp
$ export PATH=/home/vagrant/unix_lessons/myqpp:$PATH # 現在のパスを環境変数に追加で設定する
$ echo $PATH # パスが追加されたことを確認
/home/vagrant/unix_lessons/myqpp:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/vagrant/.local/bin:/home/vagrant/bin
$ hi # 実行確認
hi!
$ which hi # コマンドの場所を確認
~/unix_lessons/myqpp/hi

※↑のやり方だと追加したパスはログアウトすると無効になる

  • 管理者ユーザー
$ ls -l /var/log/messages 
-rw-------. 1 root root 495634 Jan  5 15:39 /var/log/messages
$ cat !$ # 一般ユーザーだと見れない
cat /var/log/messages
cat: /var/log/messages: Permission denied
$ su - # 管理者ユーザーになる、-lで再ログインする、-lを省略して-でもOK
Password: 
Last login: Sat Jan  5 15:39:21 UTC 2019 on pts/0
# pwd
/root
# cat /var/log/messages 
(省略)
# exit # 管理者権限を終了
logout
$ sudo cat /var/log/messages # これでも実行できる
  • chown
$ cp /var/log/messages .
cp: cannot open '/var/log/messages' for reading: Permission denied
$ sudo !! # ファイルコピー
sudo cp /var/log/messages .
$ ls -l
total 496
-rw-------. 1 root    root    495691 Jan  5 15:49 messages
$ cat messages # 所有者が違うため見れない
cat: messages: Permission denied
$ sudo chown vagrant:vagrant messages # 所有者変更
$ ls -l # 所有者が変更されていることを確認
total 496
-rw-------. 1 vagrant vagrant 495691 Jan  5 15:49 messages
$ cat messages 
(省略)
  • wc
$ wc messages 
  5468  61272 495691 messages
    • 行数 -> 5468
    • 単語数 -> 61272
    • バイト数 -> 495691
    • ファイル名 -> messages
$ wc -l messages # -lで行数だけ出せる
5468 messages
  • head、tail

ファイルの先頭(デフォルトでは10行)を読み込む

$ head messages 
Jan  1 15:40:28 localhost journal: Runtime journal is using 4.0M (max allowed 24.3M, trying to leave 36.5M free of 239.8M available → current limit 24.3M).
Jan  1 15:40:28 localhost kernel: Initializing cgroup subsys cpuset
Jan  1 15:40:28 localhost kernel: Initializing cgroup subsys cpu
Jan  1 15:40:28 localhost kernel: Initializing cgroup subsys cpuacct
Jan  1 15:40:28 localhost kernel: Linux version 3.10.0-862.14.4.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) ) #1 SMP Wed Sep 26 15:12:11 UTC 2018
Jan  1 15:40:28 localhost kernel: Command line: BOOT_IMAGE=/boot/vmlinuz-3.10.0-862.14.4.el7.x86_64 root=UUID=b5b20816-947c-4616-b15a-abaae4afe31b ro no_timer_check console=tty0 console=ttyS0,115200n8 net.ifnames=0 biosdevname=0 elevator=noop crashkernel=auto LANG=en_US.UTF-8
Jan  1 15:40:28 localhost kernel: e820: BIOS-provided physical RAM map:
Jan  1 15:40:28 localhost kernel: BIOS-e820: [mem 0x0000000000000000-0x000000000009fbff] usable
Jan  1 15:40:28 localhost kernel: BIOS-e820: [mem 0x000000000009fc00-0x000000000009ffff] reserved
Jan  1 15:40:28 localhost kernel: BIOS-e820: [mem 0x00000000000f0000-0x00000000000fffff] reserved

$ head -3 messages # 先頭3行を読み込む
Jan  1 15:40:28 localhost journal: Runtime journal is using 4.0M (max allowed 24.3M, trying to leave 36.5M free of 239.8M available → current limit 24.3M).
Jan  1 15:40:28 localhost kernel: Initializing cgroup subsys cpuset
Jan  1 15:40:28 localhost kernel: Initializing cgroup subsys cpu

$ tail -3 messages # 末尾3行を読み込む
Jan  5 15:01:01 localhost systemd: Removed slice User Slice of root.
Jan  5 15:39:21 localhost su: (to root) vagrant on pts/0
Jan  5 15:42:51 localhost su: (to root) vagrant on pts/0
$ grep 'etc' messages # etcが含まれている行を表示してくれる
(省略)
  • リダイレクション
$ echo "date" > cmd.txt # ファイルにdateを書き込む
$ cat cmd.txt 
date
$ echo "free" >> cmd.txt # ファイルに追記する
$ cat cmd.txt 
date
free
$ bash < cmd.txt # 書かれていることを実行もできる
$ bash < cmd.txt 
Sat Jan  5 15:58:46 UTC 2019
              total        used        free      shared  buff/cache   available
Mem:         498892       61616      278768        4588      158508      398680
Swap:       2097148           0     2097148
$ bash < cmd.txt > result.txt
$ cat result.txt
Sat Jan  5 15:58:56 UTC 2019
              total        used        free      shared  buff/cache   available
Mem:         498892       61736      278604        4588      158552      398536
Swap:       2097148           0     2097148
  • パイプ
$ ls -l /etc | grep "yum" # パイプで渡すことができる
drwxr-xr-x.  6 root root     4096 Jan  1 15:45 yum
-rw-r--r--.  1 root root      970 Nov  5 01:53 yum.conf
drwxr-xr-x.  2 root root     4096 Jan  1 16:22 yum.repos.d
$ ls -l /etc | grep "yum" | wc -l # 複数渡すこともできる
3
$ ls /etc/*.conf
/etc/GeoIP.conf   /etc/idmapd.conf    /etc/logrotate.conf    /etc/resolv.conf     /etc/sysctl.conf
/etc/asound.conf  /etc/krb5.conf      /etc/man_db.conf       /etc/rsyncd.conf     /etc/tcsd.conf
/etc/chrony.conf  /etc/ld.so.conf     /etc/nfs.conf          /etc/rsyslog.conf    /etc/vconsole.conf
/etc/dracut.conf  /etc/libaudit.conf  /etc/nfsmount.conf     /etc/sestatus.conf   /etc/yum.conf
/etc/fuse.conf    /etc/libuser.conf   /etc/nsswitch.conf     /etc/sudo-ldap.conf
/etc/host.conf    /etc/locale.conf    /etc/request-key.conf  /etc/sudo.conf
$ ls /etc/c??.*
/etc/csh.cshrc  /etc/csh.login
  • find、xargs
$ sudo find /etc -name "yum*"
/etc/yum.conf
/etc/bash_completion.d/yum-utils.bash
/etc/yum
/etc/yum.repos.d
/etc/logrotate.d/yum
$ sudo find /etc -name "yum*" -type f
/etc/yum.conf # ファイルだけを検索
/etc/bash_completion.d/yum-utils.bash
/etc/logrotate.d/yum
$ sudo find /etc -name "yum*" -type f -exec wc -l {} + # 結果をさらにwcコマンドに渡す場合
   26 /etc/yum.conf
  427 /etc/bash_completion.d/yum-utils.bash
    7 /etc/logrotate.d/yum
  460 total
$ sudo find /etc -name "yum*" -type f | xargs wc -l # ↑と同じ意味
   26 /etc/yum.conf
  427 /etc/bash_completion.d/yum-utils.bash
    7 /etc/logrotate.d/yum
  460 total
  • ブレース展開
$ echo {a,b,c}
a b c
$ echo {1..10}
1 2 3 4 5 6 7 8 9 10
$ echo {1..10}{a..g}
1a 1b 1c 1d 1e 1f 1g 2a 2b 2c 2d 2e 2f 2g 3a 3b 3c 3d 3e 3f 3g 4a 4b 4c 4d 4e 4f 4g 5a 5b 5c 5d 5e 5f 5g 6a 6b 6c 6d 6e 6f 6g 7a 7b 7c 7d 7e 7f 7g 8a 8b 8c 8d 8e 8f 8g 9a 9b 9c 9d 9e 9f 9g 10a 10b 10c 10d 10e 10f 10g
$ mkdir test && cd test
$ pwd
/home/vagrant/unix_lessons/test
$ mkdir app{1..5}
$ ls
app1  app2  app3  app4  app5
$ touch app{1..5}/test{1..3}{.txt,.jpeg,.gif}
$ ls app2
test1.gif  test1.jpeg  test1.txt  test2.gif  test2.jpeg  test2.txt  test3.gif  test3.jpeg  test3.txt
$ rm app{1..5}/test{1..3}{.jpeg,.gif}
$ ls app2
test1.txt  test2.txt  test3.txt