LoginSignup
3
3

More than 3 years have passed since last update.

UNIX(Linux)コマンドまとめ

Last updated at Posted at 2019-10-27

忘れないようにUNIXコマンドをまとめてみました(随時更新)。
オプションはもっとあるけど、自分が使ったことがあるものを載せる。
[参考サイト1]http://unix.oskp.net/linux_command/index.html
[参考サイト2]http://www.linux-beginner.com/linux_command.html
[ドットインストール]https://dotinstall.com/lessons/basic_unix_v2

用語

  • ディレクトリ
    ファイルをまとめるためのフォルダ

  • カレントディレクトリ
    現在いるフォルダ

  • i-node
    作成者、グループ、作成日時、サイズ等々の属性的な情報
    詳細→https://kazmax.zpp.jp/linux_beginner/inode.html

  • i-node番号
    ファイルやディレクトリに割り振られる、重複しない番号

コマンド

*-何とか*はオプション。複数組み合わせることもできる。

pwd

print working directoryの略。
現在のディレクトリを絶対パスで表示するためのコマンド。

[vagrant@localhost ~]$ pwd
/home/vagrant

clear / Ctrl+l

画面をクリアするコマンド。
実際には、操作している行を画面の一番上に表示するようにスクロールしているだけ

ls

list segmentsの略。今いるディレクトリのファイルの一覧を表示する。

説明 コマンド
ディレクトリのファイルの一覧を表示 ls
詳細情報も表示 ls -l (= llも可)
隠しファイルも表示 ls -a
i-nodeを表示 ls -i
[vagrant@localhost unix_lessons]$ ls
myapp
[vagrant@localhost unix_lessons]$ ls -l
合計 4
drwxrwxr-x 3 vagrant vagrant 4096 10月 22 13:46 2019 myapp
[vagrant@localhost unix_lessons]$ ls -a
.  ..  myapp
[vagrant@localhost unix_lessons]$

今回は隠しファイルがないので表示されず。

history

コマンドの履歴を表示する。

説明 コマンド
コマンドの履歴を表示 history
historyで表示された番号のコマンドを使う ![historyで表示された番号]
直前のコマンドを使う !!
n個前のコマンドを使う !-n
[vagrant@localhost unix_lessons]$ history

~途中省略~

  253  ls
  254  cd ..
  255  touch fileA.txt
  256  ls
  257  rm fileA.txt
  258  ls
  259  history
[vagrant@localhost unix_lessons]$ !258
ls
myapp
[vagrant@localhost unix_lessons]$ !!
ls
myapp
[vagrant@localhost unix_lessons]$ !-2
history

~途中省略~

  257  rm fileA.txt
  258  ls
  259  history
  260  ls
  261  history
[vagrant@localhost unix_lessons]$

type

コマンドに関する情報を表示する。
存在しないコマンドだったらnot foundとか出る

説明 コマンド
コマンドの情報を表示 type [コマンド名]
指定したコマンド名に関するすべてのタイプを表示する type -a [コマンド名]
指定したコマンド名のタイプのみを表示する type -t [コマンド名]
[vagrant@localhost unix_lessons]$ type pwd
pwd is a shell builtin
[vagrant@localhost unix_lessons]$ type aaa
-bash: type: aaa: not found
[vagrant@localhost unix_lessons]$

cd

change directoryの略。
指定したディレクトリへ移動する。

説明 コマンド
ホームディレクトリへ移動 cd
dirA(指定したディレクトリ)へ移動 cd dirA
今いるディレクトリの親ディレクトリ(上のディレクトリ)へ移動 cd ..
直前にいたディレクトリへ移動 cd -

指定したディレクトリへ移動

ここではunix_lessonsへ移動してみる

[vagrant@localhost ~]$ ls
bingo_php  centos6  php_lessons  unix_lessons
[vagrant@localhost ~]$ cd unix_lessons/
[vagrant@localhost unix_lessons]$

今いるディレクトリの親ディレクトリ(上のディレクトリ)へ移動

[vagrant@localhost unix_lessons]$ cd ..
[vagrant@localhost ~]$

直前にいたディレクトリへ移動

ここでは直前にいたのはunix_lessonsだったので、cd - と入力するとunix_lessonsに移動する。

[vagrant@localhost ~]$ cd -
/home/vagrant/unix_lessons
[vagrant@localhost unix_lessons]$

mkdir

make directoryの略。ディレクトリを新規作成する。

説明 コマンド
dirAを新規作成 mkdir dirA
親ディレクトリが無ければ同時に作成する mkdir -p dirB/dirBB

ディレクトリを新規作成

ここではmyappディレクトリを新規作成

[vagrant@localhost unix_lessons]$ mkdir myapp
[vagrant@localhost unix_lessons]$ ls
myapp

親ディレクトリが無ければ同時に作成する

ここではdirBディレクトリを作成し、その中にdirBBディレクトリを作成

[vagrant@localhost unix_lessons]$ mkdir myappB/myappBB
mkdir: ディレクトリ `myappB/myappBB' を作成できません: そのようなファイルやディレクトリはありません
[vagrant@localhost unix_lessons]$ mkdir -p myappB/myappBB
[vagrant@localhost unix_lessons]$ ls
myapp  myappB
[vagrant@localhost unix_lessons]$ ls myappB
myappBB
[vagrant@localhost unix_lessons]$

[エラーが出た原因]https://qiita.com/a-takano/items/5ea647bc2cd137e4026f

rmdir

remove directoryの略。ディレクトリを削除する。
削除したものは復活させられないので慎重に!!!!!!

説明 コマンド 備考
dirAを削除 rmdir dirA
親ディレクトリも同時に削除する rmdir -p dirB/dirBB ディレクトリ内にファイルがあると削除出来ない
親ディレクトリも同時に削除する rm -r dirB/dirBB ディレクトリ内にファイルがあるときはこっちの記述

ディレクトリを削除

ここではmyappBBディレクトリを削除

[vagrant@localhost unix_lessons]$ ls myappB
myappBB
[vagrant@localhost unix_lessons]$ rmdir myappB/myappBB/
[vagrant@localhost unix_lessons]$

親ディレクトリも同時に削除

ここではmyappBディレクトリとmyappBBディレクトリを削除
myappBBディレクトリにはtest.txtファイルがある。

[vagrant@localhost unix_lessons]$ ls
myapp  myappB
[vagrant@localhost unix_lessons]$ ls myappB
myappBB
[vagrant@localhost unix_lessons]$ rmdir myappB/myappBB/
rmdir: failed to remove `myappB/myappBB/': ディレクトリは空ではありません
[vagrant@localhost unix_lessons]$ rm -r myappB
[vagrant@localhost unix_lessons]$ ls
myapp
[vagrant@localhost unix_lessons]$

[エラーが出た原因]https://qiita.com/a-takano/items/5ea647bc2cd137e4026f

rm

removeのこと。ディレクトリやファイルを削除する。

説明 コマンド
fileAを削除 rm fileA
dirAを再帰的に削除 rm -r dirA
存在しないファイルを削除しようとしてもエラーメッセージを表示しない rm -f fileA
削除前に確認する rm -i fileA
経過を表示する rm -v fileA

ファイルを削除

ここではfileA.txtを削除してみる

[vagrant@localhost unix_lessons]$ ls
fileA.txt  myapp
[vagrant@localhost unix_lessons]$ rm fileA.txt
[vagrant@localhost unix_lessons]$ ls
myapp
[vagrant@localhost unix_lessons]$

「削除しますか?」と聞かれることもあるので、削除する場合はy(yesの意)+ Enter

ディレクトリを再帰的に削除

rmdirの親ディレクトリも同時に削除に記述

cp

copyのこと。ディレクトリやファイルをコピーする。
https://www.atmarkit.co.jp/ait/articles/1606/02/news021.html

説明 コマンド
ディレクトリを再帰的にコピー cp -r dirA dirB
コピー先に同じ名前のファイルが既にある場合、ファイルを上書きするか確認 cp -i dirA dirB
「パーミッション」「所有者」「タイムスタンプ」を保持したままコピー cp -p dirA dirB

ディレクトリを再帰的にコピー

ここではmyappディレクトリをコピーしてmyapp2ディレクトリを作成

[vagrant@localhost unix_lessons]$ cp myapp myapp2
cp: omitting directory `myapp'
[vagrant@localhost unix_lessons]$ ls
myapp
[vagrant@localhost unix_lessons]$ cp -r myapp myapp2
[vagrant@localhost unix_lessons]$ ls
myapp  myapp2
[vagrant@localhost unix_lessons]$

[エラーが出た原因]https://qiita.com/a-takano/items/5ea647bc2cd137e4026f

scp

ローカルホストとリモートホスト間でファイルを転送する。
https://uxmilk.jp/50946

説明 コマンド
コピー元ファイルとディレクトリの「パーミッション」「所有者」「タイムスタンプ」を保持したまま転送 scp -p user@ホスト名 or IPアドレス:/XXX/YYY/ZZZ/fileA

mv

moveのこと。ファイル名の変更・ファイルやディレクトリの移動。

説明 コマンド
ファイル名をfileAからfileBに変更 mv fileA fileB
fileAをdirAに移動 mv fileA dirA/fileA

ファイル名の変更

[vagrant@localhost unix_lessons]$ ls
TEST.txt  myapp
[vagrant@localhost unix_lessons]$ mv TEST.txt TEST2.txt
[vagrant@localhost unix_lessons]$ ls
TEST2.txt  myapp
[vagrant@localhost unix_lessons]$

ファイルやディレクトリの移動

[vagrant@localhost unix_lessons]$ ls
TEST2.txt  myapp
[vagrant@localhost unix_lessons]$ mv TEST2.txt myapp/TEST2.txt
[vagrant@localhost unix_lessons]$ ls myapp/
TEST2.txt  config  hello.txt
[vagrant@localhost unix_lessons]$

touch

空ファイルを作成する・タイムスタンプの変更をする。

説明 コマンド
空ファイルを作成 touch fileA.txt(拡張子は自分で判断)
日時・時間を指定して修正 touch **-t [CC]YYMMDDhhmm[.SS] fileA.txt

空ファイルを作成

[vagrant@localhost unix_lessons]$ ls
myapp
[vagrant@localhost unix_lessons]$ touch TEST.txt
[vagrant@localhost unix_lessons]$ ls
TEST.txt  myapp

日時・時間を指定して修正

[vagrant@localhost unix_lessons]$ ll
合計 4
drwxrwxr-x 3 vagrant vagrant 4096 10月 22 17:35 2019 myapp
[vagrant@localhost unix_lessons]$ cd myapp/
[vagrant@localhost myapp]$ ll
合計 8
-rw-rw-r-- 1 vagrant vagrant    0 10月 22 17:33 2019 TEST2.txt
drwxrwxr-x 3 vagrant vagrant 4096 10月 22 13:07 2019 config
-rw-rw-r-- 1 vagrant vagrant 1800 10月 22 12:46 2019 hello.txt
[vagrant@localhost myapp]$ touch -t 201911101430 TEST2.txt
[vagrant@localhost myapp]$ ll
合計 8
-rw-rw-r-- 1 vagrant vagrant    0 11月 10 14:30 2019 TEST2.txt
drwxrwxr-x 3 vagrant vagrant 4096 10月 22 13:07 2019 config
-rw-rw-r-- 1 vagrant vagrant 1800 10月 22 12:46 2019 hello.txt
[vagrant@localhost myapp]$

TEST.txtの日付が2019年10月22日17時33分から2019年11月10日14時30分に変わりました。

CC:年の上位2桁
YY:年の下位2桁
MM:月
DD:日
hh:時間
mm:分
ss:秒(省略時は0)

cat

連結を意味するconcatenateの略。ファイルの中身を出力したり、ファイルを結合する。
リダイレクションも可能(別途記載)。

説明 コマンド
fileAの中身を出力 cat fileA
fileAとfileBを連結して表示 cat fileA fileB

中身を出力

[vagrant@localhost myapp]$ ls
TEST2.txt  config  hello.txt
[vagrant@localhost myapp]$ cat TEST2.txt
[vagrant@localhost myapp]$ echo "これはテストです。" > TEST2.txt
[vagrant@localhost myapp]$ cat TEST2.txt
これはテストです。
[vagrant@localhost myapp]$

リダイレクションしてcatでTEST.txtの中を確認した。
ちゃんと【これはテストです。】がTEST.txtに書き込まれている。

連結して表示

[vagrant@localhost myapp]$ ls
TEST2.txt  config  hello.txt
[vagrant@localhost myapp]$ cat TEST2.txt hello.txt
これはテストです。
1.

hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello.

~途中省略~

5.

hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello.

END
[vagrant@localhost myapp]$

less

ファイルの中身を表示するページャー。
ページャーとは
[参考サイト]http://e-words.jp/w/%E3%83%9A%E3%83%BC%E3%82%B8%E3%83%A3.html

説明 コマンド
fileAの内容を見る less fileA.txt
[vagrant@localhost myapp]$ less hello.txt

lessコマンドを使うとそのファイルの中に入る。
lessを終了(抜ける)場合はqを押す。

su

ユーザーの切り替え。

説明 コマンド
ユーザーの切り替え su ユーザー名
[vagrant@localhost myapp]$ su user_name

diff

differenceの略。2つのファイルを比較し、異なる箇所を出力する。

説明 コマンド
違いのある箇所を1つにまとめて、-記号と+記号で変更箇所を示す diff -u fileA fileB
空行の有無を無視 diff -B fileA fileB
スペースの数だけが異なる場合は違いを無視する diff -b fileA fileB

nkf

Network Kanji Filterの略。文字コードの出力や判別をする。

説明 コマンド
自動判別の結果を出力する nkf -g fileA

vi(vim)

Linux標準のテキストエディタ。
以下vi起動時のコマンド↓

説明 コマンド
起動時、指定行から表示 vi +行数 fileA
読み込み専用で起動 vi -R fileA
デバッグ????? vi -d fileA

-dの意味が検索しても出てこないので、分かる方いませんか?:sob:

find

ファイルやディレクトリを検索。
他のコマンドに引数として渡すことも可能

説明 コマンド
名前を指定して検索 find ~ -name "*検索する名前*"
名前を指定して検索(大文字・小文字の区別無し) find ~ -iname "*検索する名前*"

文字化けで消せなくなっちゃった!!:scream:ってときも

日本語のファイル名やディレクトリ名が文字化けして、rmコマンドで消せなくなってもfindコマンドと組み合わせれば大丈夫!
https://centos.bungu-do.jp/archives/42
https://www.atmarkit.co.jp/ait/articles/1607/12/news021.html

3
3
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
3
3