36
37

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

今更聞けないよ...UNIXコマンド入門  

Posted at

ことのはじまり

UNIXコマンドとか使えて当たり前っしょ。というこのご時世。
でも、「うーん、なんで使えるといいんだっけ?ツールとかでうまくいけないっけ?」と思ってしまったのでまとめる。
いや、でも、これなんかまとめてる途中で上げるの恥ずかしくなるやつや、、、。
教材:https://dotinstall.com/lessons/basic_unix_v2

なぜUnixコマンドなのか

ファイルとか手動でダダダだってやったがよくないすか?
Q.フォルダを大量に作らないと行けないときは?
あ、だる、、。効率的にやろうぜってことでのUnixコマンド

そもそもUNIXとは

OSの一種らしい

macOSとかと同様に、OSらしい。主にサーバー用に使われている。
無料配布されていたこともあり、互換OSが沢山誕生している。
それらで各々にカスタマイズされてはいるものの
POSIXという共通規格に守られているため、共通して使える。

プロンプトの見方

プロンプト

[vagrant@localhost unixPractice] $

こんな感じ。

プロンプトのパーツ 説明
vagrant ユーザーの名前
@localhost マシーン名(ローカルホストってマシンにいるよ)
unixPractice ディレクトリ
$ 一般ユーザー
管理ユーザー

pwd

pwd (Print Working Directory)
現在のディレクトリを教えてくれます。

cp

文字通りコピーします。

//ディレクトリの場合(再帰的にコピーするという意味で -r )
$ cp -r hoge1 hoge2

//ファイルもコピーできる
$cp hoge1.txt hoge2.txt

mkdir

ディレクトリを作りましょう

//hogehogeディレクトリを作る
$ mkdir hogehoge

//階層のあるディレクトリも作れる
$mkdir -p hogehoge/fugafuga
*-pを指定することにより、hogehogeディレクトリが存在しない場合はそのフォルダごと作る。

rmdir

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

$ rmdir hogehoge

rm -rf

rmdirは空専門なので、中身があるときにはこの子
いつもお世話になっております。

$ rm -rf hogehoge

cat

ファイルの中身を連結して表示する。
一画面分しか見えない
後ほど紹介するパイプで、grepなどで検索したワードを表示させるのに便利

$ cat huga.txt | grep 'chage'
chage

lessとの使い分けについての参考:https://uxmilk.jp/12235

less

スクリプトや設定ファイルなどを閲覧のみしたい場合は'less'がよい。
viとかだと誤って書き換えるリスクもあるしね。

ここにいい感じでまとまってました。
http://d.hatena.ne.jp/midori_kasugano/20100121/1264032849

history

履歴だします。

$ history
1033  vi hoge.txt
1034  cat hoge.txt
1035  vi huga.txt
1036  cat huga.txt
1037  vi huga.txt
1038  cat huga.txt
1039  vi huga.txt
1040  cat huga.txt
1041  cat huga.txt | grep 'chage'

上記の番号で呼び出すこともできるんです。

$ !1041
cat huga.txt | grep 'chage'
chage

1041で実行したcat huga.txt | grep 'chage'を再び呼び起こしています。

!!で直前のコマンドを実行できる

$ !!
cat huga.txt | grep 'chage'
chage

直前に実行した関連コマンドを実行

//途中まででいい
!pw

$ !pw
pwd
/home/vagrant/unixPractice/myapp/test

//:pで検索を出す
!pw:p

$ !pw:p
pwd

--help

コマンドの簡単な説明を表示する 

$ mkdir --help
Usage: mkdir [OPTION]... DIRECTORY...
ディレクトリを作成する。ただし既にディレクトリがあれば何もしない。

長いオプションに必須の引数は短いオプションにも必須です.
  -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, --context=CTX  set the SELinux security context of each created
                      directory to CTX
      When COREUTILS_CHILD_DEFAULT_ACLS environment variable is set, -p/--parents
      option respects default umask and ACLs, as it does in Red Hat Enterprise Linux 7 by default
            --help     この使い方を表示して終了
      --version  バージョン情報を表示して終了

Report mkdir bugs to bug-coreutils@gnu.org
GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
Report mkdir translation bugs to <http://translationproject.org/team/>
For complete documentation, run: info coreutils 'mkdir invocation'

man

マニュアルを表示する

MKDIR(1)                         User Commands                        MKDIR(1)

NAME
       mkdir - make directories

SYNOPSIS
       mkdir [OPTION]... DIRECTORY...

DESCRIPTION
       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, --context=CTX
              set the SELinux security context of each created directory to CTX

              When  COREUTILS_CHILD_DEFAULT_ACLS  environment  variable is set,
              -p/--parents option respects default umask and ACLs, as  it  does
              in Red Hat Enterprise Linux 7 by default

       --help display this help and exit

       --version
              output version information and exit

AUTHOR
       Written by David MacKenzie.

REPORTING BUGS
       Report mkdir bugs to bug-coreutils@gnu.org
       GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
       General help using GNU software: <http://www.gnu.org/gethelp/>
       Report mkdir translation bugs to <http://translationproject.org/team/>

COPYRIGHT
       Copyright © 2010 Free Software Foundation, Inc.  License GPLv3+: GNU GPL
       version 3 or later <http://gnu.org/licenses/gpl.html>.
       This is free software: you are  free  to  change  and  redistribute  it.
:

ln シンボリックリンク

ln -s config/production/database/ dbconfig

ls -l

リンク一覧をみることができる

$ ls -l
合計 28
drwxrwxr-x. 2 vagrant vagrant 4096 10月  7 19:59 2018 app1
drwxrwxr-x. 2 vagrant vagrant 4096 10月  7 19:59 2018 app2
drwxrwxr-x. 2 vagrant vagrant 4096 10月  7 19:59 2018 app3
drwxrwxr-x. 2 vagrant vagrant 4096 10月  7 19:59 2018 app4
drwxrwxr-x. 2 vagrant vagrant 4096 10月  7 19:59 2018 app5
lrwxrwxrwx. 1 vagrant vagrant   27 10月  8 15:45 2018 dbconfig -> config/production/database/
-rw-rw-r--. 1 vagrant vagrant  325 10月  8 01:09 2018 hoge.txt
-rw-rw-r--. 1 vagrant vagrant  207 10月  8 01:13 2018 huga.txt

頭文字のみかた

1 2
d ディレクトリ
l リンク
- ファイル

権限の見方

1 2
r 読み込み可能か
w 書き込み可能か
x 実行可能か

touch

空のファイルを作る

$ touch hogehoge.txt

cat /etc/passwd

システムに設定されているユーザー一覧を確認
ユーザー名/パスワード設定済みか/id/グループid/コメント/実行するファイル

$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt

cat /etc/group

グループ一覧

$ cat /etc/group
root:x:0:
bin:x:1:bin,daemon
daemon:x:2:bin,daemon
sys:x:3:bin,adm
adm:x:4:adm,daemon
tty:x:5:
disk:x:6:
lp:x:7:daemon
mem:x:8:

groups

自分が属しているグループ一覧

$ groups
vagrant

su -l

サブスチチューションユーザー
ユーザーを切り替える。

//オーナーに切り替える $ => #
$ su -l
パスワード:
#

chown

change owner
所有権を変更する

//ユーザーとグループをvagrantに変える
sudo chown vagrant:vagrant messages 

wc

行数や文字数をカウントしてくれる
行数 単語数 バイト数の順

$ wc  huga.txt
 53  40 207 huga.txt

//行数だけのときは -l
$ wc -l  huga.txt
53 huga.txt

head

頭から数行を見ることができる。

$ head huga.txt
1.
hoge

2.
huga

3.
tyoge

4.

-nで行数指定も可能

$ head -n 3 huga.txt
1.
hoge

tail

後ろから見たいときはこちら。
もちろん行数指定も可能

$ tail huga.txt
song

16.
sing

17.
sing sing sing

18.
yaha yaha yaha

grep

単語を検索する

$ grep 'ASUKA' huga.txt
ASUKA

> (リダイレクション)

コマンドの結果でファイルを上書きする

$ echo "date" > cmd.txt

応用して、bashに追加することも可能

bash < cmd.txt 
bash < cmd.txt > result.txt

>> (リダイレクション)

コマンドの結果でファイルを末尾に追加する

$ echo "date" >> cmd.txt

| (パイプ)

コマンドの結果をファイルにではなく、別のコマンドに渡すことができる

ls -l /etc/ コマンドを表示するコマンド、それにPHP関連のものだけを抽出するようにする。

$ $ ls -l /etc/ | grep "php"
drwxr-xr-x.  2 root root   4096  6月 21 06:56 2018 php-zts.d
drwxr-xr-x.  2 root root   4096  6月 21 06:56 2018 php.d
-rw-r--r--.  1 root root  67183  1月 14 06:59 2018 php.ini
-rw-r--r--.  1 root root  67145  1月  3 21:14 2018 php.ini.15952.2018-01-14@06:59:58~
-rw-r--r--.  1 root root  62394  5月 24 15:18 2018 php.ini.rpmnew

find

ファイルの検索を行う
権限がないためsudoをつけて行う
コマンド 起点 検索条件 検索ワードの順で表記

$ sudo find /etc -name "http*"
/etc/httpd
/etc/httpd/conf/httpd.conf.14958.2018-01-14@06:57:43~
/etc/httpd/conf/httpd.conf
/etc/logrotate.d/httpd
/etc/sysconfig/httpd
/etc/rc.d/init.d/httpd

別コマンドを実行したい際には

$ sudo find /etc -name "http*" -type f -exec wc -l {} +
 1009 /etc/httpd/conf/httpd.conf.14958.2018-01-14@06:57:43~
 1009 /etc/httpd/conf/httpd.conf
    9 /etc/logrotate.d/httpd
   31 /etc/sysconfig/httpd
  131 /etc/rc.d/init.d/httpd
 2189 合計
hogehohgegohoegheogheogheoghegoehgeoghegoehgoelgheoghegoehgoehgeogheoghegoehgoegheogheog
1.

このどちらかを実行

$ sudo find /etc -name "http*" -type f | xargs wc -l
 1009 /etc/httpd/conf/httpd.conf.14958.2018-01-14@06:57:43~
 1009 /etc/httpd/conf/httpd.conf
    9 /etc/logrotate.d/httpd
1.
   31 /etc/sysconfig/httpd
  131 /etc/rc.d/init.d/httpd
 2189 合計

感想

意外と使いこなすのは大変そうだけど、極めると一気に実用的になりそう!!
次は上級編だっ、、、!

参考

https://dotinstall.com/lessons/basic_unix_v2
https://uxmilk.jp/12235
http://d.hatena.ne.jp/midori_kasugano/20100121/1264032849

36
37
1

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
36
37

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?