【初心者向け】頻繁に使うLinuxコマンド&オプション特集

Linux

「Linuxのコマンドやオプションがたくさんあるけど実際に何がよく使われているんだ?」

とLinuxを勉強している人、特に勉強始めたての人は感じると思います。
そんな方に向けて、私がエンジニアとして8年間働いてきた中で、個人的に使用頻度の高くかつ、初心者向けにこれは覚えておいた方が便利だと思うコマンドやそのオプションを紹介していきます。

ls

ファイル一覧を取得するコマンド

よく使用するオプション

オプション説明
-lディレクトリ内のファイルの情報を表示する
-aドットファイル(隠しファイル)も表示する
-tファイルの更新日が新しい順に表示する
-r並び順を逆にする

使い方

基本的には「ls -l」や、隠しファイルも見たい時に「ls -al」を使用しています。
また個人的には最新のファイルがどれかを確認したい時に、「ls -ltr」を使用しています。

実行例

ファイル一覧を表示

[taro@e3b95837105a ~]$ ls -l
total 0
-rw-rw-r-- 1 taro taro 0 Apr  1 11:52 file1.txt
-rw-rw-r-- 1 taro taro 0 Apr  1 11:52 file2

-aをつけることで、隠しファイルも見える

[taro@e3b95837105a ~]$ ls -al
total 20
drwx------ 2 taro taro 4096 Apr  1 11:52 .
drwxr-xr-x 1 root root 4096 Apr  1 11:51 ..
-rw-r--r-- 1 taro taro   18 Apr  1  2020 .bash_logout
-rw-r--r-- 1 taro taro  193 Apr  1  2020 .bash_profile
-rw-r--r-- 1 taro taro  231 Apr  1  2020 .bashrc
-rw-rw-r-- 1 taro taro    0 Apr  1 11:52 file1.txt
-rw-rw-r-- 1 taro taro    0 Apr  1 11:52 file2
-rw-rw-r-- 1 taro taro    0 Apr  1 11:52 .file3

pwd

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

使い方

「pwd」で自分がどこのディレクトリで作業をしているかを確認する時に使用している。

実行例

自分が今いるディレクトリが「/home/taro/work/linux」とわかる。

[taro@e3b95837105a linux]$ pwd
/home/taro/work/linux

cd

ディレクトリを移動するコマンド

使い方

基本的には「cd ディレクトリ先」でディレクトリ先に移動したり、「cd -」でひとつ前に使用していたディレクトリに移動している。

実行例

「~/work/linux」から~/work/testに移動した後に、「cd -」で「~/work/linux」に戻っている。

[taro@e3b95837105a linux]$ pwd
/home/taro/work/linux
[taro@e3b95837105a linux]$ cd ~/work/test/
[taro@e3b95837105a test]$ pwd
/home/taro/work/test
[taro@e3b95837105a test]$ cd -
/home/taro/work/linux
[taro@e3b95837105a linux]$ pwd
/home/taro/work/linux

mkdir

ディレクトリを作成するコマンド

よく使用するオプション

オプション説明
-p必要に応じて親ディレクトリを作成する

使い方

基本的には「mkdir ディレクトリ名」を使用して、親ディレクトリも一緒に作りたい場合は「mkdir -p ディレクトリ名A/ディレクトリ名B」

実行例

dirという名前でディレクトリ作成。

[taro@e3b95837105a linux]$ mkdir dir
[taro@e3b95837105a linux]$ ls -l
total 4
drwxrwxr-x 2 taro taro 4096 Apr  1 11:55 dir
-rw-rw-r-- 1 taro taro    0 Apr  1 11:52 file1.txt
-rw-rw-r-- 1 taro taro    0 Apr  1 11:52 file2

dir2/dir3というディレクトリを作成する。「mkdir -p dir2/dir3」で親ディレクトリも一緒に作成

[taro@e3b95837105a linux]$ mkdir -p dir2/dir3
[taro@e3b95837105a linux]$ ls -l 
total 8
drwxrwxr-x 2 taro taro 4096 Apr  1 11:55 dir
drwxrwxr-x 3 taro taro 4096 Apr  1 11:56 dir2
-rw-rw-r-- 1 taro taro    0 Apr  1 11:52 file1.txt
-rw-rw-r-- 1 taro taro    0 Apr  1 11:52 file2
[taro@e3b95837105a linux]$ ls -l dir2/
total 4
drwxrwxr-x 2 taro taro 4096 Apr  1 11:56 dir3

cp

ファイルのコピーするコマンド

よく使用するオプション

オプション説明
-rコピー元にディレクトリを指定した場合に再起的にコピーする
-pコピー元のパーミッションや所有者、タイムスタンプが同じファイルとしてコピーする

使用方法

ファイルのバックアップを作るときに「cp -p」を使用して変更されてないファイルとして残しておく。
ディレクトリごとコピーする場合に「cp -r」を使用する。

実行例

「cp file2 file2_bk」の場合は、タイムスタンプがコピーした時の時間になっているが、「cp -p file2 file2_bk2」の場合、コピー元のファイルのタイムスタンプになっている。

[taro@e3b95837105a linux]$ ls -l
total 8
drwxrwxr-x 2 taro taro 4096 Apr  1 11:55 dir
drwxrwxr-x 3 taro taro 4096 Apr  1 11:56 dir2
-rw-rw-r-- 1 taro taro    0 Apr  1 11:52 file1.txt
-rw-rw-r-- 1 taro taro    0 Apr  1 11:52 file2
[taro@e3b95837105a linux]$ cp file2 file2_bk
[taro@e3b95837105a linux]$ cp -p  file2 file2_bk2
[taro@e3b95837105a linux]$ ls -l
total 8
drwxrwxr-x 2 taro taro 4096 Apr  1 11:55 dir
drwxrwxr-x 3 taro taro 4096 Apr  1 11:56 dir2
-rw-rw-r-- 1 taro taro    0 Apr  1 11:52 file1.txt
-rw-rw-r-- 1 taro taro    0 Apr  1 11:52 file2
-rw-rw-r-- 1 taro taro    0 Apr  1 11:57 file2_bk
-rw-rw-r-- 1 taro taro    0 Apr  1 11:52 file2_bk2

mv

ファイル、ディレクトリを移動させたり、名前を変更させる

使い方

「mv ファイルA ファイルB」でファイルの名前をファイルAからファイルBに変更、「mv ファイルA ディレクトリ1」でファイルAをディレクトリ1に移動する。

実行例

ファイル名をfile1.txtからfile3.txtに変更

[taro@e3b95837105a linux]$ mv file1.txt file3.txt
[taro@e3b95837105a linux]$ ls -l
total 8
drwxrwxr-x 2 taro taro 4096 Apr  1 11:55 dir
drwxrwxr-x 3 taro taro 4096 Apr  1 11:56 dir2
-rw-rw-r-- 1 taro taro    0 Apr  1 11:52 file2
-rw-rw-r-- 1 taro taro    0 Apr  1 11:57 file2_bk
-rw-rw-r-- 1 taro taro    0 Apr  1 11:52 file2_bk2
-rw-rw-r-- 1 taro taro    0 Apr  1 11:52 file3.txt

file3.txtをdirに移動

[taro@e3b95837105a linux]$ ls -l
total 8
drwxrwxr-x 2 taro taro 4096 Apr  1 11:55 dir
drwxrwxr-x 3 taro taro 4096 Apr  1 11:56 dir2
-rw-rw-r-- 1 taro taro    0 Apr  1 11:52 file2
-rw-rw-r-- 1 taro taro    0 Apr  1 11:57 file2_bk
-rw-rw-r-- 1 taro taro    0 Apr  1 11:52 file2_bk2
-rw-rw-r-- 1 taro taro    0 Apr  1 11:52 file3.txt
[taro@e3b95837105a linux]$ mv file3.txt dir
[taro@e3b95837105a linux]$ ls -l
total 8
drwxrwxr-x 2 taro taro 4096 Apr  6 08:10 dir
drwxrwxr-x 3 taro taro 4096 Apr  1 11:56 dir2
-rw-rw-r-- 1 taro taro    0 Apr  1 11:52 file2
-rw-rw-r-- 1 taro taro    0 Apr  1 11:57 file2_bk
-rw-rw-r-- 1 taro taro    0 Apr  1 11:52 file2_bk2
[taro@e3b95837105a linux]$ ls -l dir/file3.txt 
-rw-rw-r-- 1 taro taro 0 Apr  1 11:52 dir/file3.txt

rm

ファイルやディレクトリを削除する

よく使用するオプション

オプション説明
-i削除前に確認
-rディレクトリを再起的に削除

使い方

重要なファイルを消す前に、「rm -i ファイル」でファイルを確認してから消す。
ディレクトリを消すには「rm -r ディレクトリ」を使用する。

実行例

file2_bkを削除

[taro@e3b95837105a linux]$ ls -l
total 8
drwxrwxr-x 2 taro taro 4096 Apr  6 08:10 dir
drwxrwxr-x 3 taro taro 4096 Apr  1 11:56 dir2
-rw-rw-r-- 1 taro taro    0 Apr  1 11:57 file2_bk
-rw-rw-r-- 1 taro taro    0 Apr  1 11:52 file2_bk2
[taro@e3b95837105a linux]$ rm -i file2_bk
rm: remove regular empty file ‘file2_bk’? y
[taro@e3b95837105a linux]$ ls -l
total 8
drwxrwxr-x 2 taro taro 4096 Apr  6 08:10 dir
drwxrwxr-x 3 taro taro 4096 Apr  1 11:56 dir2
-rw-rw-r-- 1 taro taro    0 Apr  1 11:52 file2_bk2

ディレクトリを削除、rm dirだと削除に失敗する

[taro@e3b95837105a linux]$ ls -l
total 8
drwxrwxr-x 2 taro taro 4096 Apr  6 08:10 dir
drwxrwxr-x 3 taro taro 4096 Apr  1 11:56 dir2
-rw-rw-r-- 1 taro taro    0 Apr  1 11:52 file2_bk2
[taro@e3b95837105a linux]$ rm dir
rm: cannot remove ‘dir’: Is a directory
[taro@e3b95837105a linux]$ rm -ir dir
rm: descend into directory ‘dir’? y
rm: remove regular empty file ‘dir/file3.txt’? y
rm: remove directory ‘dir’? y
[taro@e3b95837105a linux]$ ls -l
total 4
drwxrwxr-x 3 taro taro 4096 Apr  1 11:56 dir2
-rw-rw-r-- 1 taro taro    0 Apr  1 11:52 file2_bk2

curl

サーバーにリクエストを送信、レスポンスを受信するコマンド

よく使用するオプション

オプション説明
-mタイムアウトを設定

使い方

ほとんど「curl http://~~~」でサーバーにリクエストを送るという使い方をしている。
よく自分のIPを調べる時に、「curl httpbin.org/ip」をしている。
サーバーにhttpリクエスト投げられているかという確認で「curl -m 秒数 http://~~~」で実行して確認している。

実行例

curlを使って、自分のIPを取得

[taro@e3b95837105a linux]$ curl httpbin.org/ip
{
  "origin": "xxx.xxx.xxx.xxx"
}

grep

ファイルの中であるパターンの文字列が含まれている行を表示するコマンド

よく使用するオプション

オプション説明
-rディクトリ指定した場合はその中のディレクトリも含めて検索
-i大文字と小文字を区別しない
-n行番号を表示させる
-vパターンに一致しない行を表示

使い方

ある文字列を含んだファイルを探したい時には、「grep -rin “文字列” ディレクトリ名」を実行している。
「grep -v “文字列”」はパイプで繋いで結果を出力するときに使用している

実行例

testディレクトリ配下にhelloという文字列が入っているファイルを検索している

[taro@e3b95837105a linux]$ ls -l
total 12
drwxrwxr-x 3 taro taro 4096 Apr  1 11:56 dir2
-rw-rw-r-- 1 taro taro    0 Apr  1 11:52 file2_bk2
drwxrwxr-x 3 taro taro 4096 Apr  6 08:19 test
-rw-rw-r-- 1 taro taro   17 Apr  6 08:16 test.txt
[taro@e3b95837105a linux]$ grep -rin "hello" test
test/test2/text2.txt:1:hello
test/text.txt:1:hello

test.txtの内容からaの文字列が入ってないものを出力する

[taro@e3b95837105a linux]$ cat test.txt 
test
aaaaa
math

[taro@e3b95837105a linux]$ cat test.txt | grep -v a
test

date

日付を出力するコマンド

使い方

日付を確認する時に使用

実行例

コマンド実行時の時間が出力される

[taro@e3b95837105a linux]$ date
Thu Apr  6 08:14:23 JST 2023

cat

ファイルの中身を出力するコマンド

使い方

設定ファイルの中身を見たい時に使用している。
ログから特定の文字列が存在する行を見たい時に、grepとcatをパイプで繋いで、「cat ファイル | grep “検索したい文字列”」を使っている。

実行例

testの中身を出力

[taro@e3dd6ce60087 ~]$ cat test 
aaaa
vvvvv
ss

testから「a」の文字が含まれている行を出力

[taro@e3dd6ce60087 ~]$ cat test | grep a
aaaa

tail

ファイルの末尾10行(デフォルト)を出力するコマンド

よく使用するオプション

オプション説明
-fファイルを監視して、内容が追加されるたびに表示する
-n 数字ファイルの末尾の指定した行数出力される。

使い方

ログの監視に使用している。「tail -f ファイル名」で更新を確認する。
アプリが無事に起動したかをログから見たい時に、ログの内容が10行以上の時もあるので、「tail -n 数字 ファイル名」で確認している。

実行例

デフォルトは末尾10行しか出力されないが、-n 13をオプションで設定して13行まで出力される

[taro@e3b95837105a linux]$ tail file.txt 
4行目
5行目
6行目
7行目
8行目
9行目
10行目
11行目
12行目
13行目
[taro@e3b95837105a linux]$ tail -n 13 file.txt 
1行目
2行目
3行目
4行目
5行目
6行目
7行目
8行目
9行目
10行目
11行目
12行目
13行目

-f のオプションを追加してファイルの監視をしている。file.txtに「huppp」が追加されたら、こちらでも「huppp」が出力される。

[taro@e3b95837105a linux]$ tail -f file.txt 
6行目
7行目
8行目
9行目
10行目
11行目
12行目
13行目
aaaa
nuooo
hai!

huppp

su

ログインをし直さずに他のユーザーに切り替えるコマンド

使い方

「su ユーザー」でユーザーに切り替える

実行例

taroユーザーに切り替える。プロンプトのrootの所がtaroになっている。

[root@e3b95837105a linux]# su taro
[taro@e3b95837105a linux]$ 

ps

実行されているプロセスの確認

よく使用するオプション

オプション説明
-A全てのプロセスを表示
-a端末を持つ全てのプロセスを表示
-u読みやすいフォーマットで出力する
-x端末を持たない全てのプロセスを表示する
–sort並び替え

使い方

「ps -aux」を使って全てのプロセスを確認したり「ps -aux | grep “プロセス名”」を使って、特定のプロセスが実行されているかを確認する。
「ps -aux –sort rss」でRSS(メモリの使用量)でソートをしたりしている。

実行例

taroユーザーに切り替えているプロセスを確認している

[root@e3b95837105a linux]# ps -aux | grep "su taro"
root      1264  0.0  0.0  85436  4428 ?        S    08:08   0:00 su taro
root     34809  0.0  0.0  85436  4400 ?        S    08:26   0:00 su taro
root     50703  0.0  0.0  12548  2260 pts/3    S+   08:36   0:00 grep --color=auto su taro

「ps -aux –sort +rss」でrssの昇順で出力(降順にする場合は-rssにする)

[root@e3b95837105a linux]# ps -aux --sort +rss
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root     59863  0.0  0.0   4380   656 ?        S    08:45   0:00 sleep 1
root     59858  0.0  0.0   4380   680 ?        S    08:45   0:00 sleep 1
......

kill

プロセスを終了させるコマンド

よく使用するオプション

オプション説明
-番号シグナルの番号を指定する

使い方

削除をしたいプロセスを「ps -aux | grep “プロセス名”」で調べた後、そのプロセスのIDを指定して「kill プロセスID」でプロセスを終了させる。
たまにプロセスを強制終了したい時に、「kill -9 プロセスID」

実行例

test.shを実行しているプロセスを見つけて、そのプロセスIDでkillをして、test.shを終了させている。

[taro@e3b95837105a ~]$ ps -aux | grep test.sh
taro     67442  0.0  0.0  13016  2624 pts/3    S+   08:53   0:00 bash test.sh
taro     69812  0.0  0.0  12544  2324 pts/4    S+   08:53   0:00 grep --color=auto test.sh
[taro@e3b95837105a ~]$ kill 67442
[taro@e3b95837105a ~]$ ps -aux | grep test.sh
taro     71678  0.0  0.0  12544  2248 pts/4    S+   08:54   0:00 grep --color=auto test.sh

chmod

ファイルやディレクトリのパーミッションを変更する。

使い方

使用頻度が高いのは鍵のパーミンションを変更時に、「chmod 600 鍵ファイル名」を実行する。

実行例

chmod 777 test.txtを実行して、全てのユーザーから読み書き実行が可能なファイルに変更

[taro@e3b95837105a linux]$ ls -l test.txt 
-rw-rw-r-- 1 taro taro 17 Apr  6 08:16 test.txt
[taro@e3b95837105a linux]$ chmod 777 test.txt 
[taro@e3b95837105a linux]$ ls -l test.txt 
-rwxrwxrwx 1 taro taro 17 Apr  6 08:16 test.txt

chown

ファイルの所有者と所有グループを変更する。

よく使用するオプション

オプション説明
-Rファイルとディレクトリを再起的に変更する

使い方

「chown -R ユーザー名:グループ名 対象ディレクトリ」で対象ディレクトリ内の所有者と所有グループを変更している。

実行例

所有者をrootからtaroに変更

[root@e3b95837105a linux]# ls -l root_file 
-rw-r--r-- 1 root root 0 Apr  6 08:59 root_file
[root@e3b95837105a linux]# chown taro:taro root_file 
[root@e3b95837105a linux]# ls -l root_file 
-rw-r--r-- 1 taro taro 0 Apr  6 08:59 root_file

history

実行したコマンドの履歴を見る

使い方

過去に実行したコマンドを実行したい時に、「history | grep 文字列」を実行して、過去に実行したコマンドを探してコピペして再実行するのに使用している。

実行例

過去にroot_fileも文字列で操作したコマンドを出力

[root@e3b95837105a linux]# history | grep root_file 
   19  touch root_file
   21  ls -l root_file 
   22  chown taro:taro root_file 
   23  ls -l root_file 
   25  history | grep root_file 

まとめ

ここで紹介したコマンドを実際に自分で動かしてもらって理解を深めてもらえたらと思います。
Linux以外にもGo言語の記事を書いてますので、そちらも見てもらえたらと思います。

【おすすめ記事のリンク】

コメント

タイトルとURLをコピーしました

Fatal error: Uncaught JSMin_UnterminatedStringException: JSMin: Unterminated String at byte 814: "Linuxについて学び始めた人にとって、コマンドや各コマンドのオプションが多くて全部覚えるのが大変だと思います。 in /home/c5287456/public_html/engineer-want-to-grow.com/wp-content/plugins/autoptimize/classes/external/php/jsmin.php:214 Stack trace: #0 /home/c5287456/public_html/engineer-want-to-grow.com/wp-content/plugins/autoptimize/classes/external/php/jsmin.php(152): JSMin->action(1) #1 /home/c5287456/public_html/engineer-want-to-grow.com/wp-content/plugins/autoptimize/classes/external/php/jsmin.php(86): JSMin->min() #2 /home/c5287456/public_html/engineer-want-to-grow.com/wp-content/plugins/autoptimize/classes/external/php/ao-minify-html.php(257): JSMin::minify('{"@context":"ht...') #3 [internal function]: AO_Minify_HTML->_removeScriptCB(Array) #4 /home/c5287456/public_html/engineer-want-to-grow.com/wp-content/plugins/autoptimize/classes/external/php/ao-minify-html.php(108): pre in /home/c5287456/public_html/engineer-want-to-grow.com/wp-content/plugins/autoptimize/classes/external/php/jsmin.php on line 214