Linuxの基礎コマンドを備忘録として記載する。
カーソル移動コマンド
コマンド | 内容 |
---|---|
Ctrl + b | 後方に1文字分移動 |
Ctrl + f | 前方に1文字文移動 |
Ctrl + a | 行頭に移動 |
Ctrl + e | 行末に移動 |
lsコマンド
ファイルやディレクトリを一覧表示するコマンド
[wataru@localhost ~]$ ls
#引数なしの場合は、ファイル、ディレクトリのみが出力される
123.gz diary.sh Downloads head.txt Pictures tmp wc.txt
case.sh dir1 echo.sh homesize.sh Public tree.sh
Desktop dir1.tar for.sh if.sh Templates Videos
diary Documents func.sh Music test.sh
[wataru@localhost ~]$ ls -l
#-lオプションを使用すると、ファイル属性等の詳細情報も込みで出力される
total 64
-rw-rw-r--. 1 wataru wataru 97 Jul 3 20:39 123.gz
-rwxrwxr-x. 1 wataru wataru 71 Jun 22 04:52 case.sh
drwxr-xr-x. 2 wataru wataru 6 Jun 6 17:30 Desktop
drwxrwxr-x. 2 wataru wataru 28 Jun 24 04:46 diary
-rwxrwxr-x. 1 wataru wataru 174 Jun 24 04:45 diary.sh
drwxrwxr-x. 2 wataru wataru 60 Jul 3 04:28 dir1
-rw-rw-r--. 1 wataru wataru 10240 Jul 3 21:25 dir1.tar
#-lオプションで出力された詳細情報の意味は以下となる
drwxrwxr-x. 2 wataru wataru 60 Jul 3 04:28 dir1
---------------------------------------------------
d :ファイルタイプ
rwxrwxr-x. :ファイルモード
2 :リンク数
wataru :オーナー
wataru :グループ
60 :ファイルサイズ
Jul 3 04:28 :タイムスタンプ
[wataru@localhost ~]$ ls -a
#-aオプションを使用すると、隠しファイルも合わせて出力される
#ファイル名が「.(ドット)」で始まるファイルを隠しファイルとしている
. .cache dir1.tar head.txt Pictures Videos
.. case.sh Documents homesize.sh .pki .viminfo
123.gz .config Downloads .ICEauthority Public wc.tar.gz
[wataru@localhost ~]$ ls -F
#-Fオプションを使用すると、ファイル種別が出力される
123.gz diary.sh* Downloads/ head.txt Pictures/ tmp/ wc.txt
case.sh* dir1/ echo.sh* homesize.sh* Public/ tree.sh*
Desktop/ dir1.tar for.sh* if.sh* Templates/
-Fオプションの種別
種別 | 記号 |
---|---|
通常ファイル | なし |
ディレクトリ | / |
実行可能ファイル | * |
シンボリックリンク | @ |
pwdコマンド
カレントディレクトリを表示する
[wataru@localhost ~]$ pwd
/home/wataru
cdコマンド
カレントディレクトリを変更する
[wataru@localhost ~]$ cd ..
[wataru@localhost home]$ pwd
/home
[wataru@localhost home]$
mkdirコマンド
ディレクトリを作成する
[wataru@localhost tmp]$ ls -l
total 0
[wataru@localhost tmp]$ mkdir testdir
#mkdirコマンドの後に作成するディレクトリ名を入力する
[wataru@localhost tmp]$ ls -l
total 0
drwxrwxr-x. 2 wataru wataru 6 Jul 3 22:12 testdir
[wataru@localhost tmp]$ mkdir -p work/2020/07
#-pオプションを使用すると、深いディレクトリを一気に作成できる
#今回の場合、work ⇒ 2020 ⇒ 07と作成されている
[wataru@localhost tmp]$ ls -l
total 0
drwxrwxr-x. 2 wataru wataru 6 Jul 3 22:12 testdir
drwxrwxr-x. 3 wataru wataru 18 Jul 3 22:16 work
[wataru@localhost tmp]$ cd work
[wataru@localhost work]$ ls -l
total 0
drwxrwxr-x. 3 wataru wataru 16 Jul 3 22:16 2020
[wataru@localhost work]$ cd 2020
[wataru@localhost 2020]$ ls -l
total 0
drwxrwxr-x. 2 wataru wataru 6 Jul 3 22:16 07
touchコマンド
ファイルを作成する
[wataru@localhost tmp]$ touch ./testdir/work2020.txt
#touchでwork2020.txtを作成した
[wataru@localhost tmp]$ ls -lF ./testdir/work2020.txt
-rw-rw-r--. 1 wataru wataru 0 Jul 3 22:21 ./testdir/work2020.txt
[wataru@localhost testdir]$ touch test.{01..10}
#一気に複数ファイルを作成することができる
#{01..10}はブレース展開という機能
#連番リストを作成できる
[wataru@localhost testdir]$ ls -l
total 0
-rw-rw-r--. 1 wataru wataru 0 Jul 3 22:24 test.01
-rw-rw-r--. 1 wataru wataru 0 Jul 3 22:24 test.02
-rw-rw-r--. 1 wataru wataru 0 Jul 3 22:24 test.03
-rw-rw-r--. 1 wataru wataru 0 Jul 3 22:24 test.04
-rw-rw-r--. 1 wataru wataru 0 Jul 3 22:24 test.05
-rw-rw-r--. 1 wataru wataru 0 Jul 3 22:24 test.06
-rw-rw-r--. 1 wataru wataru 0 Jul 3 22:24 test.07
-rw-rw-r--. 1 wataru wataru 0 Jul 3 22:24 test.08
-rw-rw-r--. 1 wataru wataru 0 Jul 3 22:24 test.09
-rw-rw-r--. 1 wataru wataru 0 Jul 3 22:24 test.10
-rw-rw-r--. 1 wataru wataru 0 Jul 3 22:21 work2020.txt
rmコマンド
ファイル・ディレクトリを削除する
[wataru@localhost testdir]$ ls -l
total 0
-rw-rw-r--. 1 wataru wataru 0 Jul 3 22:24 test.01
-rw-rw-r--. 1 wataru wataru 0 Jul 3 22:24 test.02
-rw-rw-r--. 1 wataru wataru 0 Jul 3 22:24 test.03
-rw-rw-r--. 1 wataru wataru 0 Jul 3 22:24 test.04
-rw-rw-r--. 1 wataru wataru 0 Jul 3 22:24 test.05
-rw-rw-r--. 1 wataru wataru 0 Jul 3 22:24 test.06
-rw-rw-r--. 1 wataru wataru 0 Jul 3 22:24 test.07
-rw-rw-r--. 1 wataru wataru 0 Jul 3 22:24 test.08
-rw-rw-r--. 1 wataru wataru 0 Jul 3 22:24 test.09
-rw-rw-r--. 1 wataru wataru 0 Jul 3 22:24 test.10
-rw-rw-r--. 1 wataru wataru 0 Jul 3 22:21 work2020.txt
[wataru@localhost testdir]$ rm test.01 test.10
#複数ファイル削除することが出来る
#今回の例だと、test.01 test.10を削除した
[wataru@localhost testdir]$ ls -l
total 0
-rw-rw-r--. 1 wataru wataru 0 Jul 3 22:24 test.02
-rw-rw-r--. 1 wataru wataru 0 Jul 3 22:24 test.03
-rw-rw-r--. 1 wataru wataru 0 Jul 3 22:24 test.04
-rw-rw-r--. 1 wataru wataru 0 Jul 3 22:24 test.05
-rw-rw-r--. 1 wataru wataru 0 Jul 3 22:24 test.06
-rw-rw-r--. 1 wataru wataru 0 Jul 3 22:24 test.07
-rw-rw-r--. 1 wataru wataru 0 Jul 3 22:24 test.08
-rw-rw-r--. 1 wataru wataru 0 Jul 3 22:24 test.09
-rw-rw-r--. 1 wataru wataru 0 Jul 3 22:21 work2020.txt
[wataru@localhost tmp]$ ls -l
total 0
drwxrwxr-x. 2 wataru wataru 146 Jul 5 03:59 testdir
drwxrwxr-x. 3 wataru wataru 18 Jul 3 22:16 work
[wataru@localhost tmp]$ rm testdir
#rmコマンドでディレクトリを削除すると、エラーが出る
#ただ、-rオプションを使用すると、ディレクトリを削除できる
#削除対象のディレクトリの中のファイルやディレクトリもまとめて削除される
rm: cannot remove 'testdir': Is a directory
[wataru@localhost tmp]$ rm -r testdir/
[wataru@localhost tmp]$ ls -l
total 0
drwxrwxr-x. 3 wataru wataru 18 Jul 3 22:16 work
[wataru@localhost work]$ rm -i work.01.txt
#-iオプションを使用すると、削除前の確認をすることができる
rm: remove regular empty file 'work.01.txt'? y
[wataru@localhost work]$ ls
2020 work.03.txt work.05.txt work.07.txt work.09.txt
work.02.txt work.04.txt work.06.txt work.08.txt
wataru@localhost tmp]$ ls
testGo work
[wataru@localhost tmp]$ rmdir testGo/
#rmdirコマンドは空のディレクトリを削除する
[wataru@localhost tmp]$ ls
work
catコマンド
ファイルを表示する
[wataru@localhost work]$ cat work.02.txt
#ファイルの中身が表示される
2020/07/05
test?cat
123445
[wataru@localhost work]$ cat -n work.02.txt
#-nオプションを使用すると、行番号を付けて内容が表示される
1 2020/07/05
2 test?cat
3 123445
4
cpコマンド
ファイルやディレクトリをコピーする
[wataru@localhost work]$ ls
testgo work.03.txt work.05.txt work.07.txt work.09.txt
work.02.txt work.04.txt work.06.txt work.08.txt
[wataru@localhost work]$ cp work.0* testgo
#cp <コピー元> <コピー先>でファイルをコピーすることができる
#「work.0*」のように「*」指定すると複数のファイルが一気にコピーできる
[wataru@localhost work]$ cd testgo/
[wataru@localhost testgo]$ ls
work.02.txt work.04.txt work.06.txt work.08.txt
work.03.txt work.05.txt work.07.txt work.09.txt
[wataru@localhost work]$ cp -i work.0* testgo
#-iオプションを使用すると、rmコマンド同様に実行前に確認することができる
wataru@localhost work]$ cp gogodur testgo
#ディレクトリをコピーすると、エラーとなる
cp: -r not specified; omitting directory 'gogodur'
[wataru@localhost work]$ cp -r gogodur testgo
#-rオプションを使用すると、コピーすることができる
[wataru@localhost testgo]$ ls
gogodur work.03.txt work.05.txt work.07.txt work.09.txt
work.02.txt work.04.txt work.06.txt work.08.txt
mvコマンド
ファイルを移動する
[wataru@localhost testgo]$ mv work.03.txt gogodur
[wataru@localhost testgo]$ ls gogodur/
work.03.txt
[wataru@localhost testgo]$ ls
#mvコマンドで移動したファイルは移動元からはなくなる
gogodur work.04.txt work.06.txt work.08.txt
work.02.txt work.05.txt work.07.txt work.09.txt
wataru@localhost testgo]$ ls
gogodur work.04.txt work.06.txt work.08.txt
work.02.txt work.05.txt work.07.txt work.09.txt
[wataru@localhost testgo]$ mv work.04.txt mvfile.txt
#mvコマンドで移動元、移動先にファイルを指定すると、
#ファイル名が書き換わる
[wataru@localhost testgo]$ ls
gogodur work.02.txt work.06.txt work.08.txt
mvfile.txt work.05.txt work.07.txt work.09.txt#
lnコマンド
リンクを張る(ファイルに別名を付ける)
長いパス名を省略することもできる
wataru@localhost testgo]$ ln -s gogodur go
#-sオプションを使用すると、シンボリックリンクになる
[wataru@localhost testgo]$ ls -l
total 4
lrwxrwxrwx. 1 wataru wataru 7 Jul 6 05:01 go -> gogodur
#矢印でどのファイルを指定しているか分かる
drwxrwxr-x. 2 wataru wataru 25 Jul 6 04:46 gogodur
-rw-rw-r--. 1 wataru wataru 0 Jul 5 04:26 testtest.txt
-rw-rw-r--. 1 wataru wataru 28 Jul 5 04:26 work.02.txt