LoginSignup
9
5

More than 5 years have passed since last update.

Linuxコマンドに関する便利な使い方やテクニックをここで紹介したいと思います。
コマンドのほかのオプションも知りたいと思いますのでmanコマンドまたはgoogleを利用して調べてください。

1.便利なコマンド

grep

特定の文字列が含まれるファイル名を探す

# CMD
grep -rl 'string' /path/of/directory

# 例
> grep -rl ssl /etc/httpd/conf.d
./a.conf
./b.conf
...

ファイルにある特定の文字列を表示する

# CMD
grep -Po 'regex_string' /path/of/file

# 例
> grep -Po 'ServerName.*' /etc/httpd/conf.d/a.conf
ServerName     a.jp

> grep -Po 'ServerName.*' /etc/httpd/conf.d/*
/etc/httpd/conf.d/a.conf:ServerName     a.jp
/etc/httpd/conf.d/b.conf:ServerName     b.jp
...

tree

ディレクトリだけの構造表示

# CMD
tree -d -L x /path/of/directory

# 例
> tree -d -L 2 /etc/httpd/
/etc/httpd/
|-- conf
|-- conf.d
|   `-- ssl
|-- logs -> ../../var/log/httpd
|-- modules -> ../../usr/lib64/httpd/modules
`-- run -> ../../var/run/httpd

readlink

リンク先実質のファイルを探す

# CMD
readlink -f /path/of/link

# 例
> ls -l
a.sql -> testsql.sql
b.sql -> a.sql
testsql.sql   <-- これは実質のファイルです

> readlink b.sql
a.sql         <-- これはリンクです

> readlink -f b.sql
/root/testsql.sql  <-- 実質のファイルが分かります

ps

プロセスの起動日時と稼働時間を表示する

# CMD
ps axfo start,etime,cmd

# 例
> ps axfo start,etime,cmd
 STARTED     ELAPSED CMD
  Jun 20 896-15:21:20 [kthreadd]
  Jun 20 896-15:21:20  \_ [migration/0]
  Jun 20 896-15:21:20  \_ [ksoftirqd/0]
  Jun 20 896-15:21:20  \_ [stopper/0]
...
  Nov 07 26-19:06:53 /usr/sbin/httpd
13:00:51       08:01  \_ /usr/sbin/httpd
13:00:52       08:00  \_ /usr/sbin/httpd
13:05:12       03:40  \_ /usr/sbin/httpd
13:06:59       01:53  \_ /usr/sbin/httpd
13:07:00       01:52  \_ /usr/sbin/httpd
13:07:00       01:52  \_ /usr/sbin/httpd
...

top

CPUコア別でCPU使用率を表示する

# CMD
top -c -d 1
topに入ったら1を押します

# 例
top - 13:12:44 up 896 days, 15:25,  1 user,  load average: 0.22, 0.37, 0.46
Tasks: 149 total,   1 running, 148 sleeping,   0 stopped,   0 zombie
Cpu0  :  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu1  :  0.0%us,  0.0%sy,  0.0%ni, 99.0%id,  0.0%wa,  0.0%hi,  0.0%si,  1.0%st
Cpu2  :  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
...

rpm

パッケージの名前、バージョン、サイズ、説明を表示する

# CMD
rpm -qa --queryformat '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH} \t%{SIZE} \t%{SUMMARY} \n'| sort

# 例
> rpm -qa --queryformat '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH} \t%{SIZE} \t%{SUMMARY} \n'| sort
ConsoleKit-0.4.1-6.el6.x86_64   273094  System daemon for tracking users, sessions and seats
ConsoleKit-libs-0.4.1-6.el6.x86_64      22061   ConsoleKit libraries
MAKEDEV-3.24-6.el6.x86_64       227290  A program used for creating device files in /dev
abrt-2.0.8-40.el6.centos.x86_64         771483  Automatic bug detection and reporting tool
...

パッケージの中身を表示する

# CMD
rpm -ql installed_package
rpm -qpl rpm_package

# 例
> rpm -ql vim-common-7.4.629-5.el6.x86_64
/etc/vimrc
/usr/bin/xxd
/usr/share/doc/vim-common-7.4.629
/usr/share/doc/vim-common-7.4.629/Changelog.rpm
...

> rpm -qpl iotop-0.6-2.el7.noarch.rpm
/usr/lib/python2.7/site-packages/iotop
/usr/lib/python2.7/site-packages/iotop-0.6-py2.7.egg-info
/usr/lib/python2.7/site-packages/iotop/__init__.py
/usr/lib/python2.7/site-packages/iotop/__init__.pyc
/usr/lib/python2.7/site-packages/iotop/__init__.pyo
...

パッケージの依存関係を表示する

# CMD
rpm -qR installed_package
rpm -qpR rpm_package

# 例
> rpm -qR python-2.6.6-64.el6.x86_64
/usr/bin/python2.6
libc.so.6()(64bit)
libc.so.6(GLIBC_2.2.5)(64bit)
libdl.so.2()(64bit)
libm.so.6()(64bit)
libpthread.so.0()(64bit)
libpython2.6.so.1.0()(64bit)
libutil.so.1()(64bit)
python-libs(x86-64) = 2.6.6-64.el6
rpmlib(CompressedFileNames) <= 3.0.4-1
rpmlib(FileDigests) <= 4.6.0-1
rpmlib(PartialHardlinkSets) <= 4.0.4-1
rpmlib(PayloadFilesHavePrefix) <= 4.0-1
rpmlib(VersionedDependencies) <= 3.0.3-1
rtld(GNU_HASH)
rpmlib(PayloadIsXz) <= 5.2-1

> rpm -qpR iotop-0.6-2.el7.noarch.rpm
/usr/bin/python
python
python(abi) = 2.7
rpmlib(CompressedFileNames) <= 3.0.4-1
rpmlib(FileDigests) <= 4.6.0-1
rpmlib(PartialHardlinkSets) <= 4.0.4-1
rpmlib(PayloadFilesHavePrefix) <= 4.0-1
rpmlib(PayloadIsXz) <= 5.2-1

ファイルの属するパッケージを探す

# CMD
rpm -qf /path/of/file

# 例
> rpm -qf /usr/bin/python
python-2.6.6-64.el6.x86_64

2.テクニック

cpコマンドを短くする

# CMD
cp src{,.xxx}  ==  cp src src.xxx

# 例
> cp a{,.bak}
> ls
a  a.bak

linkに対してsedを使わない

# 説明
# linkに対してsedを使ったらファイルになります。sedの使用の際にはご注意ください。

# 例
> ls -l
a.txt
b.txt -> a.txt

> cat a.txt
a

> cat b.txt
a

> sed -i '/a/a b' b.txt
> ls -l
a.txt
b.txt

> cat a.txt
a

> cat b.txt
a
b

# 上記の例でa.txtの内容を変更しようとしていましたが、実際変更したのはb.txtでした。
# しかもb.txtがファイルタイプになってしまいました。
# これは要注意です。
9
5
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
9
5