20220111のLinuxに関する記事は4件です。

Linux入門 3-6 フィルタを使ったテキストストリームの処理

[Linux入門 3-7]: はじめに Linux初学者である自分向け用の記事です。Udemyの動画講座を参考にしました。 僕の勉強法は動画を見る→実際に動かしてみる→問題演習という流れです。 前回まで:Linux入門 3-5 3. LinuC 101 Ver .10.0(問題、テスト、演習) フィルタを使ったテキストストリームの処理 catコマンド ファイルの中身を標準出力に表示するコマンド [root@52793acd2b0f test]# cat test.txt apple lemon grape ABCDE [root@52793acd2b0f test]# cat -n test.txt 1 apple 2 lemon 3 4 grape 5 6 7 ABCDE [root@52793acd2b0f test]# cat -b test.txt 1 apple 2 lemon 3 grape 4 ABCDE cutコマンド ファイルから一部を抽出して出力するコマンド [root@52793acd2b0f test]# cat test.txt 1,apple 2,lemon 3,grape [root@52793acd2b0f test]# cut -c 1 test.txt 1 2 3 [root@52793acd2b0f test]# cut -c 1-3 test.txt 1,a 2,l 3,g [root@52793acd2b0f test]# cut -d , -f 2 test.txt apple lemon grape [root@52793acd2b0f test]# cat /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin [root@52793acd2b0f test]# cut -d : -f 1-4 /etc/passwd root:x:0:0 bin:x:1:1 daemon:x:2:2 adm:x:3:4 lp:x:4:7 sync:x:5:0 shutdown:x:6:0 halt:x:7:0 mail:x:8:12 operator:x:11:0 games:x:12:100 ftp:x:14:50 nobody:x:99:99 systemd-network:x:192:192 dbus:x:81:81 expandコマンド, unexpandコマンド テキスト内のタブをスペースに変換するコマンド 行頭のスペースをタブに変換するコマンド [root@52793acd2b0f test]# cat test.txt 1 apple 2 lemon 3 grape [root@52793acd2b0f test]# expand -t 1 test.txt 1 apple 2 lemon 3 grape [root@52793acd2b0f test]# unexpand -t 8 test1.txt 1 apple 2 lemon 3 grape lessコマンド ファイルの中身を閲覧するコマンド [root@52793acd2b0f test]# less test1.txt 1 apple 2 lemon 3 grape test1.txt (END) fmtコマンド テキストを決められた桁に整形する [root@52793acd2b0f test]# cat test.txt I Like an apple . [root@52793acd2b0f test]# fmt -u test.txt I Like an apple . prコマンド 印刷用にファイルの書式を整形する [root@52793acd2b0f test]# pr -h "////PRINT////" /etc/passwd | less 2020-11-13 01:55 ////PRINT//// Page 1 root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin headコマンド, tailコマンド ファイルを先頭から表示する 指定したファイルの末尾から表示 [root@52793acd2b0f test]# head -n 2 /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin [root@52793acd2b0f test]# tail -n 2 /etc/passwd systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin sedコマンド ファイルの編集を行うコマンド [root@52793acd2b0f test]# cat test.txt 1,apple 2,lemon 3,grape 4,apple 5,orange [root@52793acd2b0f test]# sed s/a/AAA/ test.txt 1,AAApple 2,lemon 3,grAAApe 4,AAApple 5,orAAAnge sortコマンド ファイルの行の並び替えをする [root@52793acd2b0f test]# cat test.txt 2,apple 11,lemon 4,grape 1,apple 9,orange [root@52793acd2b0f test]# sort test.txt 1,apple 11,lemon 2,apple 4,grape 9,orange [root@52793acd2b0f test]# sort -n test.txt 1,apple 2,apple 4,grape 9,orange 11,lemon uniqコマンド sortコマンドと併用することで効果発動!! ソート済みの文字列から重複行を調べて、削除して取り出す [root@52793acd2b0f test]# cat test.txt 111 P@ssw0rd 222 p@ss 333 pass 111 p@ss 222 [root@52793acd2b0f test]# uniq test.txt 111 P@ssw0rd 222 p@ss 333 pass 111 p@ss 222 [root@52793acd2b0f test]# sort test.txt | uniq 111 222 333 P@ssw0rd p@ss pass splitコマンド ファイルを分割するコマンド [root@52793acd2b0f split]# for i in `seq 1000` ;do echo $i; done 1 2 3 . . . 999 1000 [root@52793acd2b0f split]# for i in `seq 1000` ;do echo $i >> aaa.txt; done [root@52793acd2b0f split]# mkdir bk [root@52793acd2b0f split]# split -100 aaa.txt bk/bbb. [root@52793acd2b0f split]# ls bk bbb.aa bbb.ab bbb.ac bbb.ad bbb.ae bbb.af bbb.ag bbb.ah bbb.ai bbb.aj diffコマンド [root@52793acd2b0f diff]# cat a.txt apple lemon grape orange pen [root@52793acd2b0f diff]# cat b.txt apple lemon grape orange test [root@52793acd2b0f diff]# diff a.txt b.txt 5c5 < pen --- > test ストリーム、パイプ、リダイレクトの使用 ストリーム: キーボードやファイルからプログラムへのデータの入力、プログラムからディスプレイやファイルへのデータの出力などのデータの流れのこと 標準入力: プログラムへの入力ストリーム 標準出力: プログラムからの出力ストリーム 標準エラー出力: プログラムからのエラーメッセージの出力 番号 入出力名 デフォルト 0 標準入力 キーボード 1 標準出力 画面(端末) 2 標準エラー出力 画面(端末) パイプ(|) 1つ目のコマンドの標準出力を2つ目のコマンドに渡す(コマンドA | コマンドB) [root@c6140de26466 3]# cat /etc/passwd | grep root root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin /dev/null 簡単にいうと、ゴミ箱のこと。 スペシャルファイルの1つで、そこに書き込まれたデータを全て捨て(writeシステムコールは成功する)、読み出してもどんなプロセスに対してもデータを返さない tee 標準出力を画面表示とファイルへの書き込み両方行う [root@c6140de26466 3]# cat /etc/passwd | grep root root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [root@c6140de26466 3]# cat /etc/passwd | grep root | tee root.txt root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [root@c6140de26466 3]# cat root.txt root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin コマンド > ファイル名 標準出力をファイルに上書きして書き込み(リダイレクト) [root@c6140de26466 3]# echo ABC > test.txt [root@c6140de26466 3]# cat test.txt ABC [root@c6140de26466 3]# echo DEF > test.txt [root@c6140de26466 3]# cat test.txt DEF コマンド >> ファイル名 標準出力をファイルに追記(リダイレクト) [root@c6140de26466 3]# echo DEF >> test.txt [root@c6140de26466 3]# cat test.txt ABC DEF コマンド < ファイル名 ファイルを入力としてコマンドに渡す [root@c6140de26466 3]# cat test.txt taro jiro saburo hanako [root@c6140de26466 3]# grep jiro < test.txt jiro [root@c6140de26466 3]# grep -v jiro < test.txt taro saburo hanako コマンド << 文字列 指定した文字列が入力されるまで、標準入力を行う [root@c6140de26466 3]# grep hello << EOF > a > aaa > goodbye > thankyou > hello > bye > hello world > konnichiwa > EOF hello hello world コマンド 2> ファイル名 標準エラー出力をファイルに書き込む [root@c6140de26466 3]# echo ABC ABC [root@c6140de26466 3]# ech ABC bash: ech: command not found [root@c6140de26466 3]# ech ABC > error.txt bash: ech: command not found [root@c6140de26466 3]# cat error.txt [root@c6140de26466 3]# ech ABC 2> error.txt [root@c6140de26466 3]# cat error.txt bash: ech: command not found コマンド 2>> ファイル名 標準エラー出力をファイルに追記 [root@c6140de26466 3]# ech hello bash: ech: command not found [root@c6140de26466 3]# ech hello 2>> error.txt [root@c6140de26466 3]# cat error.txt bash: ech: command not found bash: ech: command not found 1>&2 標準出力を標準エラー出力に渡す(あんまり使わない) [root@c6140de26466 3]# ./sample.sh > log.txt 1>&2 /home/3 ./sample.sh: line 4: echoo: command not found [root@c6140de26466 3]# cat log.txt コマンド > ファイル 2>&1 標準出力と標準エラー出力をファイルに書き込む [root@c6140de26466 3]# cat sample.sh #!/bin/bash echo `pwd` #標準出力 echoo `pwd` #標準エラー出力 [root@c6140de26466 3]# ./sample.sh /home/3 ./sample.sh: line 4: echoo: command not found [root@c6140de26466 3]# ./sample.sh > log.txt 2>&1 [root@c6140de26466 3]# cat log.txt /home/3 ./sample.sh: line 4: echoo: command not found コマンドA | コマンドB コマンドAの標準出力をコマンドBの入力にする [root@c6140de26466 3]# cat /etc/passwd | grep root root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin xargs 標準入力から読み込んだ内容をコマンドの引数に変換して渡す |xargs –I○○ コマンド ○○ # -Iですると引数を別名で渡すことができる [root@c6140de26466 3]# touch ex1 ex2 ex3 [root@c6140de26466 3]# vi rm_file.txt [root@c6140de26466 3]# cat rm_file.txt ex1 ex2 [root@c6140de26466 3]# cat rm_file.txt | rm rm: missing operand Try 'rm --help' for more information. [root@c6140de26466 3]# cat rm_file.txt | xargs rm [root@c6140de26466 3]# ll total 4 -rw-r--r-- 1 root root 0 Jan 1 13:12 ex3 -rw-r--r-- 1 root root 8 Jan 1 13:13 rm_file.txt To Be Continued... [Linux入門 3-7] へ
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

SSHポートフォワードによるアクセス

RemoteからアクセスできるTargetのJavaScriptなWEBページにLocalからアクセスしたい。 Localポートの8081を割り当てるとすると、Localで下記のsshコマンドを実行する。 $ ssh -NL 8081:192.168.1.120:80 192.168.32.55 Localのブラウザから、http://localhost:8081 でTargetのWEBページにアクセスできるようになる。 参考 sshポートフォワーディング
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

Linuxファイルシステムの管理

Linuxで使用されるファイルシステム ファイルシステムの種類 説明 ext2 Linuxで標準的に使われていた ext3 ext2の後継、ジャーナリング機能が加わった ext4 ext3の後継、大容量のファイルシステムをサポート btrfs サブボリュームを構成しスナップショットなどの機能がある xfs 古くからある、ジャーナリング機能あり iso9660 CD-ROMで使われる ntfs 近年のWindowsで使用される nfs ネットワークファイルシステム tmpfs メモリ上に配置(RAMディスク) swap 物理メモリのスワップ先 ext3/ext4 にあるジャーナリング機能とは、ファイルシステムの整合性チェック、および修復を助ける機能である ファイルシステムのチェックを行うコマンド fcsk.ext4、またはe2fcsk(ext4) fcsk.xfs、またはxfs_check(xfs) fcsk.btrfs(btrfs) fcskコマンドに-tオプションに続けてチェックするファイルシステムの種類(ext4やxfsなど)を指定すると、指定したファイルシステムをチェックする個別のコマンドを呼び出してくれる 「-t ext4」とした場合は fsck.ext4(e2fsck)が呼び出される ファイルシステム設定関連コマンド コマンド 説明 tune2fs ext2-4の設定を行う e2label ext2-4のラベルを操作 dumpe2fs ext2-4の詳細を表示 dump ext2-4をバックアップ restore dumpで作成したバックアップから復元 xfs_admin xfsの設定を行う xfs_info xfsの情報を表示 xfs_check xfsをチェック xfs_repair xfsを検査、修復 xfsdump xfsをバックアップ xfsrestore xfs復元 tune2fsコマンドは、ext2/ext3/ext4 の設定を後から行うことができるコマンド オプション 説明 -c 自動fcskが行われるまでのマウント回数を設定(カウント) -i 自動fcskが行われるまでの時間を設定(インターバル) -j ジャーナリング機能を付与 -l スーパーブロックの内容を出力 -m 続けてroot用予備領域の割合を設定 -L 続けて指定した名前でラベルを付ける smartctlコマンドの主なオプション オプション 説明 -a、--all 全ての情報を表示 -i、--info 対応状況やデバイスの情報を表示 -c テストの対応状況を表示 -t、--test 自己診断テストの実行 -l、--log テストログを表示 -H、--health デバイスのヘルスステータス(正常性)を表示 --scan 認識されているデバイスを表示
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

ブートプロセスとGRUB

Linuxカーネルの主な起動時オプション 起動オプション 説明 init=<PATH> initの代わりにコマンドを実行(例:init=/bin/bash) root=<DEVICE> ルートパーティションを実行 systemd.unit=<TAEGET> 指定したターゲットで起動 single シングルユーザーモードで起動(sと指定しても同じ) quiet 起動中のカーネルからの情報出力を抑制 UEFI(Unified Extensible Firmware Interface)について UEFIは、BIOS(レガシーBIOS)に替わる新しい基本ファームウエア ESP(EFI System Partition) UEFIシステムにおいて、物理的なマシンを起動し、ファームウエアが読み込まれた後、その後の起動シーケンスで最初にアクセスされる領域になります。 ESPは「/boot/efi」にマウントされる フォーマットはFAT16またはFAT32 efibootmgr efibootmgrはUEFIブートマネージャーの起動エントリをOS上から操作するコマンド UEFI shell UEFIでは、UEFIシェルと呼ばれるシェル環境を持っており、OSを起動せずにハードウエア周りの設定を行うことができ、デバイスドライバや、TCP/IPスタックも利用できる
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む