技術者になりたい何か

技術者になりたい何かの覚書的な何かです

Linuxコマンドを学んでいこう(基礎その1)

基本的に状況確認とかするだけのコマンド

実行してもファイルシステムに変更を加えたり、ファイル作ったり削除したり、
プロセス立ち上げたり落としたり。。。みたいなことが無いコマンドたち。

オプション次第で便利な使い方もある。パイプで渡すと便利なものもある。
けどここではほぼデフォルトの動作を。

pwd

現在の自分のいるディレクトリを表示する

$ pwd
/home/tmin


cat

ファイルの中身を端末上に表示する

$ cat /etc/crontab
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user command
17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#

ls

ディレクトリの中身を一覧表示する

$ ls
ビデオ css images robots.txt squid.conf.bac VMwareTools-10.0.10-4301679.tar.gz 画像
テンプレート esuteru.com index.html server.crt squid.conf.bac~ vmware-tools-distrib 音楽
デスクトップ favicon.ico js server.csr test wordpress
ダウンロード gnome named.ca server.key test.txt wordpress-4.6.1-ja.tar.gz
ドキュメント image nginx_signing.key #squid.conf.bac# update.log 公開
ps

実行中のプロセスを表示する

$ ps
PID TTY TIME CMD
7142 pts/0 00:00:00 bash
9447 pts/0 00:00:00 ps

grep

ファイルの中を検索する

$ grep "httpd" test1.txt
root 2381 0.0 0.5 405368 15536 ? Ss Feb16 0:36 /usr/sbin/httpd
apache 4065 0.0 0.4 424696 13776 ? S 16:10 0:00 /usr/sbin/httpd
apache 5568 0.0 0.4 424684 13780 ? S 16:41 0:00 /usr/sbin/httpd
tmin 8469 0.0 0.0 103324 900 pts/1 S+ 17:39 0:00 grep httpd
apache 27437 0.0 0.4 424684 13820 ? S 13:04 0:01 /usr/sbin/httpd
apache 27669 0.0 0.5 424756 14256 ? S 13:09 0:01 /usr/sbin/httpd
apache 27674 0.0 0.5 425996 15908 ? S 13:09 0:01 /usr/sbin/httpd
apache 28457 0.0 0.5 425160 14984 ? S 13:26 0:01 /usr/sbin/httpd
apache 28460 0.0 0.5 424748 14064 ? S 13:26 0:01 /usr/sbin/httpd
apache 30619 0.0 0.5 425152 15000 ? S 14:10 0:01 /usr/sbin/httpd
apache 30744 0.0 0.4 424688 13808 ? S 14:13 0:01 /usr/sbin/httpd
apache 32665 0.0 0.7 429692 21120 ? S 14:53 0:01 /usr/sbin/httpd

 

top

負荷の状態をリアルタイムで表示する

$ top

top - 04:21:12 up 6 days, 1:13, 2 users, load average: 0.24, 0.15, 0.06
Tasks: 299 total, 1 running, 298 sleeping, 0 stopped, 0 zombie
Cpu(s): 3.1%us, 0.7%sy, 0.0%ni, 96.1%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 2773756k total, 2310184k used, 463572k free, 169260k buffers
Swap: 2916348k total, 140276k used, 2776072k free, 925304k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
9575 tmin 20 0 15160 1276 824 R 2.0 0.0 0:00.01 top
1 root 20 0 19356 1272 1052 S 0.0 0.0 0:00.76 init
2 root 20 0 0 0 0 S 0.0 0.0 0:00.04 kthreadd
3 root RT 0 0 0 0 S 0.0 0.0 0:01.50 migration/0
4 root 20 0 0 0 0 S 0.0 0.0 0:40.48 ksoftirqd/0
5 root RT 0 0 0 0 S 0.0 0.0 0:00.00 stopper/0
6 root RT 0 0 0 0 S 0.0 0.0 0:00.36 watchdog/0
7 root RT 0 0 0 0 S 0.0 0.0 0:00.90 migration/1

 

df

ディスクの使用状況を表示する。

$ df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda1 9480420 6197208 2778588 70% /
udev 10240 0 10240 0% /dev
tmpfs 202128 21788 180340 11% /run
tmpfs 505312 88 505224 1% /dev/shm
tmpfs 5120 0 5120 0% /run/lock
tmpfs 505312 0 505312 0% /sys/fs/cgroup
/dev/sda2 14287344 386408 13152136 3% /home
tmpfs 101064 16 101048 1% /run/user/1000
/dev/sr0 644096 644096 0 100% /media/cdrom0

 

find

ディレクトリの中を再帰的にたどってファイルを検索

$ find -name "redmine"
./test/unit/lib/redmine
./test/integration/lib/redmine
./lib/redmine
./plugins/redmine_task_board/lib/scrum_alliance/redmine
./redminebac/20160211/redmine2.6/test/unit/lib/redmine
./redminebac/20160211/redmine2.6/test/integration/lib/redmine
./redminebac/20160211/redmine2.6/lib/redmine
./redminebac/20160211/redmine

 

tail

ファイルの末尾を表示。デフォルトでは10行。

$ tail /etc/apache2/apache2.conf
# see README.Debian for details.

# Include generic snippets of statements
IncludeOptional conf-enabled/*.conf

# Include the virtual host configurations:
IncludeOptional sites-enabled/*.conf

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
AddHandler cgi-script .cgi .pl

 

head

ファイルの先頭を表示。デフォルトでは10行。

$ head //etc/apache2/apache2.conf
# This is the main Apache server configuration file. It contains the
# configuration directives that give the server its instructions.
# See http://httpd.apache.org/docs/2.4/ for detailed information about
# the directives and /usr/share/doc/apache2/README.Debian about Debian specific
# hints.
#
#
# Summary of how the Apache 2 configuration works in Debian:
# The Apache 2 web server configuration in Debian is quite different to
# upstream's suggested way to configure the web server. This is because Debian's

 

man

オンラインマニュアルページを参照する。
コマンドの使い方に迷ったとき便利。

$ man man

man(1) man(1)

名前
man - オンラインマニュアルページを整形し表示する。
manpath - ユーザー個々のマニュアルページの検索パスを決める。

書式
man [-adfhktwW] [-m system] [-p string] [-C config_file] [-M path] [-P pager] [-S section_list] [section] name ...
(以下略)

more

1ページ内に収まらないファイルの中身を1ページごとに表示。

$ more number.txt
20090902-001
20090902-002
20090904-001
20090907-01
20090907-02
20090910-001
20090912-001
20090915-001
20090916-001
(中略)
-続ける--(0%)

Spaceで1ページ、Enterで一行進んでqで終了。

 less

moreの改良版。

$ less number.txt
20090902-001
20090902-002
20090904-001
20090907-01
20090907-02
20090910-001
20090912-001
20090915-001
20090916-001
(略)

Space or fでページ進める、e or jで行進める、qで終了。

 

uname

システム情報の表示

$ uname
Linux

$ uname -a
Linux tmin-serv 2.6.32-573.18.1.el6.x86_64 #1 SMP Tue Feb 9 22:46:17 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

 

dmesg

カーネルログの出力。たいてい長くなりすぎるのでgrepとかと組み合わせるのが吉。

$ dmesg
[ 0.000000] ACPI: LAPIC_NMI (acpi_id[0x2e] high edge lint[0x1])
[ 0.000000] ACPI: LAPIC_NMI (acpi_id[0x2f] high edge lint[0x1])
[ 0.000000] ACPI: LAPIC_NMI (acpi_id[0x30] high edge lint[0x1])
[ 0.000000] ACPI: LAPIC_NMI (acpi_id[0x31] high edge lint[0x1])
[ 0.000000] ACPI: LAPIC_NMI (acpi_id[0x32] high edge lint[0x1])
〜〜中略〜〜
[387233.268687] UDF-fs: Rescanning with blocksize 2048
[387233.268744] UDF-fs: error (device sda3): udf_read_tagged: read failed, block=256, location=256
[387233.268747] UDF-fs: error (device sda3): udf_read_tagged: read failed, block=512, location=512
[387233.268748] UDF-fs: warning (device sda3): udf_load_vrs: No anchor found
[387233.268748] UDF-fs: warning (device sda3): udf_fill_super: No partition found (1)
[387233.270586] isofs_fill_super: bread failed, dev=sda3, iso_blknum=16, block=32
[387233.277974] qnx4: no qnx4 filesystem (no root dir).

 

$ dmesg | grep 'kernel'
[ 0.000000] Booting paravirtualized kernel on bare hardware
[ 0.000000] Memory: 993628K/1048052K available (5237K kernel code, 947K rwdata, 1836K rodata, 1204K init, 840K bss, 54424K reserved)
[ 0.010348] Yama: disabled by default; enable with sysctl kernel.yama.*
[ 4.001744] Freeing unused kernel memory: 1204K (ffffffff818ee000 - ffffffff81a1b000)
[ 4.001746] Write protecting the kernel read-only data: 8192k
[ 4.002150] Freeing unused kernel memory: 896K (ffff880001520000 - ffff880001600000)
[ 4.002546] Freeing unused kernel memory: 212K (ffff8800017cb000 - ffff880001800000)
[ 7.526140] systemd[1]: Starting Create list of required static device nodes for the current kernel...
[ 7.542943] systemd[1]: Started Create list of required static device nodes for the current kernel.
[ 8.043435] [TTM] Zone kernel: Available graphics memory: 505312 kiB

 

cd

カレントディレクトリを移動する。
相対パスで指定したとき。

$ pwd

/home/tmin
$ cd files/
$ pwd
/home/tmin/files

 

絶対パスで指定。

$ pwd
/home/tmin
$ cd /var/log
$ pwd
/var/log
$ cd ~
$ pwd
/home/tmin 

他にもあると思うけど今回は一旦ここまで。

 

Sponsored Link