Linuxコマンドの基礎3です。
リダイレクト
標準入出力先を変更する機能
[wataru@localhost testgo]$ ls -l > lstext.txt
# >記号を使用して、リダイレクトを行う
# 今回の例でいうと、lsの結果をlstext.txtに保存する
[wataru@localhost testgo]$ ls -l
total 4
drwxrwxr-x. 2 wataru wataru 25 Jul 7 16:25 2020dir
-rw-rw-r--. 1 wataru wataru 225 Jul 15 04:47 lstext.txt
-rw-rw----. 1 wataru wataru 0 Jul 5 04:26 testtest.txt
-rw-rw-r--. 1 wataru wataru 0 Jul 11 01:55 work.txt
[wataru@localhost testgo]$ cat lstext.txt
total 0
drwxrwxr-x. 2 wataru wataru 25 Jul 7 16:25 2020dir
-rw-rw-r--. 1 wataru wataru 0 Jul 15 04:47 lstext.txt
-rw-rw----. 1 wataru wataru 0 Jul 5 04:26 testtest.txt
-rw-rw-r--. 1 wataru wataru 0 Jul 11 01:55 work.txt
[wataru@localhost testgo]$ ls xxxxx 2> error.txt
# 標準エラー出力のリダイレクトは2>記号を使用する
[wataru@localhost testgo]$ cat error.txt
ls: cannot access 'xxxxx': No such file or directory
[wataru@localhost testgo]$ ls xxx >error2.txt 2>&1
# 標準出力と標準エラー出力をまとめるには、2>&1を使用する
# 結果とエラーメッセージを合わせて1つのログファイルとしておくときに使用する
[wataru@localhost testgo]$ ls > co.txt
[wataru@localhost testgo]$ ls -F >> co.txt
# >>記号を使用することで、ファイルに追記することが出来る
[wataru@localhost testgo]$ cat co.txt
2020dir
co.txt
error2.txt
error.txt
lstext.txt
testtest.txt
work.txt
2020dir/
co.txt
error2.txt
error.txt
lstext.txt
testtest.txt
work.txt
パイプライン
コマンドの標準出力を別のコマンドの標準入力につなぐ
[wataru@localhost testgo]$ history | wc -l
# コマンド履歴を出力するhistoryコマンドの結果を
# 行数を数えるwcコマンドで行数を出力する
729
[wataru@localhost testgo]$ history | head -n 10
# headコマンドで上から10行目を出力する
1 date
2 exit
3 date
4 bash
5 exit
6 pwd
7 ls -l
8 cd Music
9 ls -l
10 date
[wataru@localhost testgo]$ history | tail -n 5
# tailコマンドで末尾から5行出力する
727 hostory
728 hostory | wc -l
729 history | wc -l
730 history | head -n 10
731 history | tail -n 5
[wataru@localhost testgo]$ history | tac
# tacコマンドで逆順に出力する
732 history | tac
731 history | tail -n 5
730 history | head -n 10
729 history | wc -l
728 hostory | wc -l
727 hostory
wcコマンド
バイト数、単語数、行数を数える
[wataru@localhost testgo]$ ls | wc
7 7 69
# 左から「行数」、「単語数」、「バイト数」を表している
# -l -w -cオプションで指定可能
[wataru@localhost testgo]$ ls
2020dir co.txt error2.txt error.txt lstext.txt testtest.txt work.txt
sortコマンド
行を並び替える
[wataru@localhost testgo]$ cat word.txt
wan
aiueo
fgh
cde
[wataru@localhost testgo]$ sort word.txt
# アルファベット順に並び替えられる
aiueo
cde
fgh
wan
[wataru@localhost testgo]$ cat sort.txt
99999
4321
564
67
684
681
20
[wataru@localhost testgo]$ sort -n sort.txt
# -nオプションを使用すると、数値順にソートする
20
67
564
681
684
4321
99999
[wataru@localhost testgo]$ sort -r word.txt
# -rオプションを使用すると、逆順にソートする
wan
fgh
cde
aiueo
uniqコマンド
重複行を取り除く(連続した同じ内容の行を省く)
[wataru@localhost testgo]$ cat word.txt
akira
tanaka
tanaka
sasaki
tanaka
akira
akira
yamamoto
[wataru@localhost testgo]$ uniq word.txt
# 連続した「tanaka」「akira」の重複行が省かれている
# 一番先頭行の「akira」は省かれていない
akira
tanaka
sasaki
tanaka
akira
yamamoto
wataru@localhost testgo]$ sort word.txt | uniq
# sortコマンドをしてから、uniqコマンド実行すると全体から重複行が省かれる
akira
sasaki
tanaka
yamamoto
wataru@localhost testgo]$ sort word.txt | uniq -c
# -cオプションを使用すると、重複行をカウントする
1
3 akira
1 sasaki
3 tanaka
1 yamamoto
cutコマンド
入力の一部分を切り出す
cut -d <区切り文字> -f <フィールド番号> <ファイル名>
[wataru@localhost testgo]$ cat word.txt
akira,124,459,root
tanaka,523,756,akira
tanaka,789,465,tanaka
sasaki,100,200,sasaki
tanaka,200,600,aida
[wataru@localhost testgo]$ cut -d , -f 2 word.txt
# ,(カンマ)で区切り、フィールド番号2を切り出している
124
523
789
100
200
[wataru@localhost testgo]$ cut -d , -f 2,4 word.txt
# フィールド番号を複数指定することも可能
124,root
523,akira
789,tanaka
100,sasaki
200,aida
trコマンド
文字を変換、削除する
1文字単位の文字置換
wataru@localhost work]$ ls
gogodur work.02.txt work.04.txt work.06.txt work.08.txt
testgo work.03.txt work.05.txt work.07.txt work.09.txt
[wataru@localhost work]$ ls | tr a-z A-Z
# 小文字a-zを大文字A-Zに変換
GOGODUR
TESTGO
WORK.02.TXT
WORK.03.TXT
WORK.04.TXT
WORK.05.TXT
WORK.06.TXT
WORK.07.TXT
WORK.08.TXT
WORK.09.TXT
[wataru@localhost work]$ ls | tr -d txt
# -dオプションを使用すると、文字の削除ができる
# 今回では、txtを削除している
gogodur
esgo
work.02.
work.03.
work.04.
work.05.
work.06.
work.07.
work.08.
work.09.
tailコマンド
末尾部分を表示する
[wataru@localhost work]$ ls | tail -n 3
# -nオプションを使用すると、行数指定する
# headコマンドも同様
work.07.txt
work.08.txt
work.09.txt
diffコマンド
差分を表示する
[wataru@localhost work]$ diff work.02.txt work.04.txt
# diff <比較元ファイル> <比較先ファイル>
3,4c3
# 「3,4」1つ目のファイルの3、4行目を
# 「3」2つ目のファイルの3行目に
# 「c」変更
< 123445
<
# < は削除された行
---
> 2345
# > は追加された行