11. headコマンドとtailコマンド
head コマンド: ファイルの先頭の内容を表示するために使用されます。デフォルトでは、ファイルの最初の 10 行が表示されます。
-
コマンド形式:head[オプション]ファイル
-
一般的な操作:
-
-
<行数> は表示される行数を指定します。
-
[root@localhost ~]# head /etc/passwd [root@localhost ~]# head /etc/fstab [root@localhost ~]# head /etc/group [root@localhost ~]# head /etc/hostname [root @localhost ~]# head /etc/hosts [root@localhost ~]# head /etc/sysconfig/network-scripts/ifcfg-ens32 # 保存されている DNS 設定ファイル情報 を表示 [root@localhost ~]# head /etc/resolv .conf # ファイルの前に表示する行数を指定するには -n を使用します [root@localhost ~]# head -n 5 /etc/passwd [root@localhost ~]# head -n 6 /etc/passwd [root@ localhost ~] # head -n 15 /etc/passwd [root@localhost ~]# head -n 20 /etc/passwd
-
tail コマンド: ファイルの末尾の内容を表示するために使用されます。デフォルトでは、ファイルの末尾に 10 行の内容が表示されます。
-
コマンド形式:tail [オプション]パラメータ
-
一般的な操作:
-
-
<行数> 指定した行数を表示します -
一般的なオプション:
-
-f 動的表示
-
[root@localhost ~]# tail /etc/passwd # ファイルの最後に表示する行数を指定するには、「-n」 を使用します [root@localhost ~]# tail -n 5 /etc/passwd [root@ localhost ~]# tail - n 5 /etc/sysconfig/network-scripts/ifcfg-ens32 IPADDR="192.168.0.50" PREFIX="24" GATEWAY="192.168.0.254" DNS1="114.114.114.114" IPV6_PRIVACY="no " #ファイルの内容を動的に 表示 [root@localhost ~]# touch t1 root@localhost ~]# tail -f t1 # 別の端末 を開いて [root@localhost ~]# echo 123 > t1
12.rm削除コマンド
rm(英語表記:remove)コマンドは、ファイルまたはディレクトリを削除するために使用されます。
-
コマンド形式: rm [-option...] ディレクトリまたはファイル...
-
共通オプション
-
-f 強制削除
-
-r ディレクトリの削除
-
*
特殊文字: システム内で一般的に使用される記号。あらゆる文字を表すために使用されます。
-
[root@localhost ~]# ls /opt abc abc1 abc2 abc3 anaconda-ks.cfg hello.txt home rh students t1 t2 t3 t4 xx xxoo [ root@localhost ~]# ls /mnt hello home oooo students1 t1 t2 t3 t4 test test1 test2 test3 # 指定したディレクトリのファイル を削除 [root@localhost ~]# rm /opt/anaconda-ks.cfg rm: 通常のファイル「/opt/anaconda-ks.cfg」を削除しますか? y #デフォルトの確認が必要です (y|n) # ファイルが正常に削除されたかどうかを 確認します [root@localhost ~]# ls /opt abc abc1 abc2 abc3 hello.txt home rh Student t1 t2 t3 t4 xx xxoo [ root @localhost ~ ]# rm /opt/hello.txt rm: 通常の空のファイル「/opt/hello.txt」を削除しますか? y # ディレクトリ内の指定したファイルも同時に 削除 [root@localhost ~]# rm /opt/t1 /opt/t2 /opt/t3 /opt/t4 rm: プレーンな空のファイル「/opt/t1」を削除しますか? y rm: かどうかディレクトリ「/opt /abc」を削除するには? y [ root@localhost ~]# ls /opt rm: 通常の空ファイル「/opt/t2」を削除するかどうか rm: プレーンな空のファイル「/opt/t3」を削除しますか? y rm: プレーンな空のファイル「/opt/t4」を削除しますか? y # ファイルが正常に削除されたかどうかを確認します [root@localhost ~]# ls /opt abc abc1 abc2 abc3 home rh Student xx xxoo # ファイルを強制的に削除するには、「-f」を 使用します [root @localhost ~ ]# rm -f /mnt/hello [root@localhost ~]# ls /mnt home oooo students1 t1 t2 t3 t4 test test1 test2 test3 # 複数のファイルを同時に 強制 [root@localhost ~]# rm -f /mnt /t1 /mnt/t2 /mnt/t3 /mnt/t4 [root@localhost ~]# ls /mnt #ディレクトリ を削除 [root@localhost ~]# rm -r /opt/abc abc1 abc2 abc3 home rh Student xx xxoo # 複数のディレクトリを同時に 削除 [root@localhost ~]# rm -r /opt/abc1 /opt/abc2 /opt/abc3 rm: ディレクトリを削除しますか "/オプト/abc1"? y rm: ディレクトリ「/opt/abc2」を削除しますか? y rm: ディレクトリ「/opt/abc3」を削除しますか? y [ root@localhost ~]# ls /opt home rh Student xx xxoo # 複数のディレクトリを同時に 強制的 [root@localhost ~]# rm -rf /opt/home /opt/student /opt/xx / opt/ xxoo [root@localhost ~]# ls /opt rh #ディレクトリとファイル を作成 [root@localhost ~]# touch /opt/t1 [root@localhost ~]# mkdir /opt/test [root@localhost ~] # ls /opt rh t1 test # rm コマンドでディレクトリを削除すると、変更されたディレクトリとそのディレクトリを含むすべてのデータが削除されます [root@localhost ~]# rm -rf /opt/ [root@localhost ~]# ls / [ root@localhost ~]# ls /mnt ホーム oooo Student1 test test1 test2 test3 すべての文字をワイルドカードとして使用するには「*」 を使用し [root@localhost ~]# rm -rf /mnt/* [root@localhost ~]# ls /mnt
13. ソフトリンクとハードリンク
Linux のリンクされたファイルは Windows のショートカットに似ています
ソフトリンク機能: パーティションを越えてディレクトリをリンクでき、ソースファイルが削除されると、リンクされたファイルは使用できなくなります。
-
ソフトリンクコマンド形式: ln -s ソースファイルパス ターゲットパス
注: リンク ファイルを作成するときは、絶対パスを記述する必要があります。現在のパス内にある場合でも、絶対パスも記述する必要があります。
[root@localhost ~]# touch hello.soft [root@localhost ~]# ls #ソフト接続 を作成します [root@localhost ~]# ln -s /root/hello.soft /opt [ root@localhost ~]# ls /opt #接続ファイルの詳細なプロパティ を表示 [root@localhost ~]# ls -l /opt/hello.soft lrwxrwxrwx. 1 root root 16 March 21 14:28 /opt/ hello.soft -> /root/hello.soft #ヒント: リンク ファイルの権限は、最終的にはソース ファイルの権限に依存します # 一般 ユーザー認証 [lisi@localhost ~]$ ls /opt hello.soft [lisi@localhost ~]$ ls -l /opt/hello.soft lrwxrwxrwx. 1 root root 16 Mar 21 14:28 /opt/hello.soft -> /root/hello.soft [lisi@localhost ~]$ cat /opt/hello. Soft cat: /opt /hello.soft: 権限が不十分です #プロンプト: ソース ファイルは /root ディレクトリに保存されており、一般ユーザーには /root ディレクトリに対する権限がないため、一般ユーザーはソース ファイルを参照できません [ #ディレクトリへのソフト接続 ソースファイルを削除します [root@localhost ~]# rm -f /root/hello.soft [root@localhost ~]# ls # 山の厚いソースファイル の後 [root@localhost ~]# ls -l /opt/ hello.soft lrwxrwxrwx. 1 root root 16 March 21 14:28 /opt/hello.soft -> /root/hello.soft # ファイルを作成し、ソフト接続を作成 [root@localhost ~]# touch hello .soft [root @localhost ~]# ln -s /root/hello.soft /opt [ root@localhost ~]# ls -l /opt/hello.soft lrwxrwxrwx. 1 root root 16 March 21 14:39 /opt /hello.soft -> /root/hello.soft # リンク ファイルを削除しても、ソース ファイルはまだ利用可能です [root@localhost ~]# rm -f /opt/hello.soft [root@localhost ~]# ls [root@localhost ~] # cat hello.soft [ root@localhost ~]# ln -s /root/test1 /opt/ [ root@localhost ~]# ls -ld /opt/test1 lrwxrwxrwx. 1 root root 11 March 21 14:44 /opt/test1 -> / root/test1 3 リンクを作成するときは、ディレクトリまたはファイルの絶対パスを記述する必要があります。現在のパスにある場合でも、絶対パス [ root@localhost ~]# ln -s hello. も記述する必要があります。 Soft /opt [root@localhost ~]# ls /opt hello.soft test1 [root@localhost ~]# ls -l /opt/hello.soft lrwxrwxrwx.1 root root 10 Mar 21 14:47 /opt/hello.soft -> hello.soft
ハード リンク機能: ハード リンクはパーティションをまたがることはできず、ディレクトリをリンクすることもできません。ソース ファイルが削除された後も、リンクされたファイルは引き続き使用できます (ファイルのバックアップの作成と同様)
-
ハードリンクコマンド形式: ln ソースファイルパス ターゲットパス
# ファイルを作成し、ハード接続を作成します [root@localhost ~]# touch hello.hard [root@localhost ~]# ln /root/hello.hard /opt/ [ root@localhost ~]# ls /opt hello.hard hello .soft test1 #ハードリンクされたソース ファイルにコンテンツ を書き込む root@localhost ~]# echo 123 > /root/hello.hard # ソース ファイルのコンテンツ を表示 [root@localhost ~]# cat /root/hello.hard 123 #同期的に更新するリンク ファイルの内容を 表示する [root@localhost ~]# cat /opt/hello.hard 123 # コンテンツをリンク ファイルに書き込み、ソース ファイルを表示して同期的に更新する [root@localhost ~]# echo xx > > /opt/hello.hard #ソースファイル をワイプして [root@localhost ~]# cat /root/hello.hard 123 xx # ハードリンクファイルの特性により、ファイル属性を維持できます変更なし root@localhost ~]# ls -l /root /こんにちは。ハード -rw-r--r--. 2 root root 7 3月21日 14:55 /root/hello.hard [root@localhost ~]# ls - l /opt/hello.hard -rw-r--r--. 2 root root 7 March 21 14:55 /opt/hello.hard #また、 ハードリンクファイルの i ノード番号は同じです [root @localhost ~ ]# ls -i /root/hello.hard 33711090 /root/hello.hard [root@localhost ~]# ls -i /opt/hello.hard 33711090 /opt/hello.hard #ハード 接続は行われませんディレクトリを許可 root@localhost に接続します ~]# ln /root/test1 /opt ln: "/root/test1": ハード リンクはディレクトリを指すことを許可されません # ハード リンクのソース ファイルが削除されると、リンク ファイルはまだ利用可能 [root@localhost ~]# rm -f /root/hello.hard [root@localhost ~]# cat /opt/hello.hard 123 xx #コンテンツをハードリンクファイルに書き込みます [root@localhost ~]# echo abc >> /opt/hello.hard [root@localhost ~]# cat /opt/hello.hard 123 xx abc #ハード 接続ではパーティションをまたがることはできません [root@localhost ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 20G 0 ディスク §─sda1 8:1 0 1G 0 パート /boot └─sda2 8:2 0 19G 0 パート §─centos-root 253:0 0 17G 0 lvm / └─centos-swap 253:1 0 2G 0 lvm [ SWAP] sr0 11:0 1 4.3G 0 rom [root@localhost ~]# ln /root/hello.soft /boot ln: ハード リンク "/boot/hello.soft" => "/root/hello.ソフト": 無効なクロスデバイス接続