利用頻度の高いLinuxコマンドを紹介します。オプションなどは全て説明していません。よく使う「オプション」や「活用方法」のみ解説します。
(利用方法が多岐にわたるコマンドは別ページで取り上げています。)
目次
pwd
( カレントディレクトリの名前表示 )
$ pwd
/home/vagrantcd
( カレントディレクトリの変更 )
# 目的のディレクトリに移動
cd 移動先ディレクトリ
# ホームディレクトリに移動
cd
# 直前いたディレクトリに移動
cd ~-ls
( ディレクトリの中身をリスト表示 )
| オプション | 説明 | 
|---|---|
| -a | .で始まるファイル(隠しファイル)も表示。 | 
| -l | ロングフォーマットで表示。 | 
| -h | ファイル容量を単位をつけて表示。( -lと併用 ) | 
| -R | サブディレクトリも再帰的にリスト表示。 | 
| -t | タイム・スタンプ順にソートして表示。 | 
| -S | ファイルの容量順にソートして表示。 | 
| -r | 逆順にソートして表示。 | 
$ ls -lrt
total 8
-rw-r--r--. 1 root root 113 Nov 10  2015 0hourly
-rw-------. 1 root root 108 Dec 11  2015 raid-checkcp
( ファイルやディレクトリをコピー )
cp [option] コピー元.. コピー先| オプション | 説明 | 
|---|---|
| -p | 元のファイルの属性(所有者、アクセス権、タイムスタンプ等)を保持。 | 
| -r | ディレクトリを中身ごとコピー。 | 
| -f | 同じ名前のファイルも強制上書き。 | 
mv
( ファイルの移動・ファイル名の変更 )
mv [option] 移動元.. 移動先touch
( 空のファイルを作成 )
touch ファイル名$ ls
$
$ touch test.txt
$
$ ls
test.txtrm
( ファイルやディレクトリを削除 )
rm [option] ファイル名| オプション | 説明 | 
|---|---|
| -f | 存在しないファイルがあってもエラーを返さない。 | 
| -r | ディレクトリを再帰的に削除。 | 
chmod
( ファイルのパーミッションを変更 )
chmod [option] mode file…| オプション | 説明 | 
|---|---|
| -R | ディレクトリやそこに含まれるもののアクセス権を再帰的に変更。 | 
$ ls -l
total 0
-rw-rw-r--. 1 vagrant vagrant 0 Dec  7 11:12 test1.txt
-rw-rw-r--. 1 vagrant vagrant 0 Dec  7 11:08 test2.txt
$ 
$ 
$ 
$ chmod 777 test1.txt
$ chmod u+x test2.txt 
$ 
$ ls -l
total 0
-rwxrwxrwx. 1 vagrant vagrant 0 Dec  7 11:12 test1.txt
-rwxrw-r--. 1 vagrant vagrant 0 Dec  7 11:08 test2.txtu+x とオプションを指定すると ファイルの所有者 に 実行権限 が付与されます。
- 対象
- u|所有者
- g|グループ
- o|その他
- a|上の3つ全て
 
- 権限
- r|読み込み
- w|書き込み
- x|実行
 
chown
( ファイルの所有者とグループを変更 )
$ ls -l
total 0
-rwxrwxrwx. 1 vagrant vagrant 0 Dec  7 11:12 test1.txt
$ 
$ 
$ sudo chown root:vagrant test1.txt 
$ 
$ ls -l
total 0
-rwxrwxrwx. 1 root    vagrant 0 Dec  7 11:12 test1.txtcat
( ファイルを連結して出力 )
cat ファイル名...$ cat test1.txt
abcdef
$ cat test2.txt
12345
$ cat test1.txt test2.txt 
abcdef
12345sort
( 並び替え )
sort ファイル名| オプション | 説明 | 
|---|---|
| -n | 行先頭の文字列を数値文字列として比較する | 
| -r | 降順 | 
$ cat test1.txt 
99
82
34
121
34
324
76
$ 
$ sort test1.txt 
121
324
34
34
76
82
99
$ sort -nr test1.txt 
324
121
99
82
76
34
34uniq
( 重複行を削除 )
uniq ファイル名| オプション | 説明 | 
|---|---|
| -n | 行先頭の文字列を数値文字列として比較する | 
| -r | 降順 | 
$ sort -r test1.txt
99
82
76
34
34
324
121
$ sort -r test1.txt | uniq
99
82
76
34
324
121
$ sort -r test1.txt | uniq -c
      1 99
      1 82
      1 76
      2 34
      1 324
      1 121
      2 重複行数が多い順にしたい場合、以下のようにします。
$ sort test1.txt | uniq -c | sort -nr
      2 34
      2 
      1 99
      1 82
      1 76
      1 324
      1 121head
( ファイルの最初の部分を表示 )
| オプション | 説明 | 
|---|---|
| -n LINES | 最初の LINES 行を表示。 | 
$ head -n 2 test1.txt 
99
82
$ head -n 3 test1.txt 
99
82
34tail
( ファイルを監視 )
ログ監視などで活用します。
( lessコマンド でも出来ます)
tail -n 0 --follow=name --retry ファイル名diff
( 差分ファイル作成 )
diff ファイルA ファイルB > diff_file$ cat test1_1.txt 
99
82
d4
121
34
a24
76
$ cat test1_2.txt 
99
82
34
121
34
324
76
$ diff test1_1.txt test1_2.txt 
3c3
< d4
---
> 34
7c7
< a24
---
> 324patch
( 差分ファイル適用 )
patch < パッチファイルtar
( ファイルの圧縮、解凍 )
tar cvzf buckup.tar.gz buckup/tar xzf buckup.tar.gz| オプション | 説明 | 
|---|---|
| c | create | 
| x | extract(ファイル展開) | 
| z | 圧縮/解凍 | 
| f | ファイル名を指定 | 
| v | 詳細表示(処理したファイル表示) | 
wc
( ファイルの行数、単語数、文字数を表示 )
$ cat wc.txt 
I go to scool.
I'd like to introduce someone.
$
$ wc wc.txt 
 3  9 47 wc.txt左から 行数 単語数 文字数 が表示されています。
file
( ファイルの種類を調べる )
$ file test1.txt 
test1.txt: ASCII texttree
( ツリー上にディレクトリを表示 )
# 隠しファイルも表示
tree -a
# ディレクトリのみ表示
tree -d
# 2階層まで表示
tree -L 2
# ディレクトリを先に表示
tree --dirsfirstman
( コマンドのマニュアルを表示 )
man 調べたいコマンドマニュアルの章番号を絞ることもできます。
man マニュアルの章番号 調べたいコマンドマニュアルの章構成は以下のようになっています。
- 1章:一般コマンド
- 2章:UNIXのシステムコール
- 3章:ライブラリ関数
- 4章:周辺装置関連
- 5章:ファイル形式
- 7章:様々な事柄の概要
which
( コマンドの絶対パスを表示 )
$ which php
/usr/bin/php
$
$ which composer
/usr/local/bin/composerwhereis
( コマンドのバイナリ・ソース、manページの場所を表示 )
$ whereis php
php: /usr/bin/php /etc/php.d /etc/php.ini /usr/lib64/php /usr/include/php /usr/share/php /usr/share/man/man1/php.1.gz
$
$ whereis composer
composer: /usr/local/bin/composerhistory
( コマンドの履歴を表示 )
$ history | tail -n 5
 1136  whereis php
 1137  whereis composer
 1138  history
 1139  history | tail 5
 1140  history | tail -n 5コマンドの実行履歴をすべて消去したい場合、以下実行します。
history -cmount
( ファイル・システムをマウントする )
mount device mountpointunmount
( ファイル・システムをアンマウントする )
unmount devicewget または curl
( ファイルをダウンロードする )
URLを指定してファイルをダウンロードします。
wget URL
curl URLsystemctl
( サービスの起動設定 )
CentOS 6では、システム起動にSysVinit系が利用されているためserviceとchkconfigを利用してました。
CentOS 7 では、システム起動にsystemdが利用されているためsystemctlを利用します。
# 起動状態確認
systemctl status サービス名
  
# 起動
systemctl start サービス名
  
# 停止
systemctl stop サービス名
  
# 自動起動設定状態確認
systemctl is-enabled サービス名
  
# 自動起動設定
systemctl enable サービス名
  
# 自動起動設定解除
systemctl disable サービス名
 
			 
			 
			 
			 
			 
			 
			