03日目Linuxファイル管理の高度

03日目Linuxファイル管理の高度

1.テキスト処理3つの銃士コマンド

Three Musketeersコマンドは、シェルプログラミングで詳細に説明されます。ここでは、最も基本的な使用法を学習します。

1.sed

主にファイルの編集が得意なストリーミングエディタで、ファイルの編集手順を事前にカスタマイズして、sedにファイルの全体的な編集を自動的に完了させることができます

1.1使用法

sed 选项 '定位+命令' 文件路径

1.2オプション

  • -nデフォルトの出力をキャンセルします

    sed -n '1p;2p;4p' 1.txt # 带 n 只打印需要的部分
    ---------------------------------
    user12312
    23123user
    111asda2323
    
    sed '1p;2p;4p' 1.txt  # 在默认输出基础上附加输出
    ---------------------------------
    user12312
    user12312
    23123user
    23123user
    1123user12323
    111asda2323
    111asda2323
    2131asdasd23423asdas35454dxcz
    00000user9999user8888user
    
  • -rは、拡張された通常のメタ文字をサポートします(通常のメタ文字を学習していないため、ここで一時的に理解します)

  • -私はすぐにファイルを編集します

1.3ポジショニング

行の配置

  1. 最初の行に配置
  2. 1,3は最初の行から3番目の行までを表します
  3. ポジショニングを書かないでください。つまり、すべての行をポジショニングします。

正規表現ターゲティング

  1. /user/含まれるuser
  2. /^user/user行の先頭
  3. /user$/で、user行の終わり

数値+正規表現の配置

  1. 1, 8p 1〜8行の印刷を表します

    sed -n '1,7p' 1.txt # 文本就6行
    ----------------------------------
    user12312
    23123user
    1123user12323
    111asda2323
    2131asdasd23423asdas35454dxcz
    00000user9999user8888user
    
  2. 1, /user/pこれは、最初の行から最初に一致した/user/に移動することを意味します

    sed -n '1, /231/p' 1.txt   # 打印1到以231 开头的行
    ----------------------------------
    user12312
    23123user
    

1.4コマンド

  • d削除

    sed '1d;2d;5d' 1.txt 
    -----------------------------------
    1123user12323
    111asda2323
    00000user9999user8888user
    
    sed '1,4d' 1.txt  # 删除1-4 所以只剩5 6了
    -----------------------------------
    2131asdasd23423asdas35454dxcz
    00000user9999user8888user
    
  • p行数

    sed -n '1p;2p;4p' 1.txt 
    ------------------------------------
    user12312
    23123user
    111asda2323
    
    sed -n '1, /8user$/p' 1.txt   # 输出第一行到 以8user结尾的内容
    ------------------------------------
    user12312
    23123user
    1123user12323
    111asda2323
    2131asdasd23423asdas35454dxcz
    00000user9999user8888user
    
  • s /// g

    sed 's/user/SuperUser/g' 1.txt  # 把所有行的user都替换为SuperUser g是一个声明相当于 global 把行所有的修改,不加只替换没行中的一个
    ----------------------------------
    SuperUser12312
    23123SuperUser
    1123SuperUser12323
    111asda2323
    2131asdasd23423asdas35454dxcz
    00000SuperUser9999SuperUser8888SuperUser
    
    sed 's/user/SuperUser/' 1.txt   # 没有g
    --------------------------------
    SuperUser12312
    23123SuperUser
    1123SuperUser12323
    111asda2323
    2131asdasd23423asdas35454dxcz
    00000SuperUser9999user8888user
    
    sed 's/^user/SuperHero/g' 1.txt  #  以user开头的行中的user换成SuperHero
    -------------------------------
    SuperHero12312
    23123user
    1123user12323
    111asda2323
    2131asdasd23423asdas35454dxcz
    00000user9999user8888user
    
    sed '1, 4s/user/Waibi/g' 1.txt   # 替换1-4行user为waibi
    ------------------------------
    Waibi12312
    23123Waibi
    1123Waibi12323
    111asda2323
    2131asdasd23423asdas35454dxcz
    00000user9999user8888user
    
    
    # 加上-i选项,直接修改文件,通常会在调试完毕确保没有问题后再加-i选项
    sed '1, 4s/user/Waibi/g' 1.txt -i
    cat 1.txt 
    ------------------------------
    Waibi12312
    23123Waibi
    1123Waibi12323
    111asda2323
    2131asdasd23423asdas35454dxcz
    00000user9999user8888user
    

    コマンドを使用できます; 1d、3d、5dなどの多くを接続するための番号は1、3、5行を削除することを意味します

2.awk

Awkは主に/etc/passwdこのようなフォーマットされたテキストを処理するために使用されます。実際、awkは非常に強力な操作を独立して完了できるプログラミング言語です。シェルプログラミングで詳しく紹介します。

1.1使用法

awk 选项 'pattern{action}' 文件路径

1.2オプション

-Fは行区切り文字を指定します

1.3作業プロセス

awk -F: '{print $1,$3}' /etc/passwd

  • Awkはファイルの行を読み取り、それを$ 0に割り当てます。
  • 次にAWK -F、で指定されたセパレータとn個のセグメントに線を分割するまで100個のセグメント、第1のセグメントは$ 1であり、第二セグメントが$ 2であり、そのために
  • printは、行の最初と3番目の段落を出力します。コンマは出力区切り文字を表し、デフォルトは-Fと同じです。
  • ファイルの内容が読み取られるまで、手順1、2、3を繰り返します。

1.4組み込み変数

$0 コンテンツの全行

NR 行番号に相当するレコード番号

NF -F区切り文字で区切られたセグメントの数

1.5パターンは

  • /レギュラー/

    / Regular /#行の内容が正常に一致します

$1 ~      /正则/ # 第一段内容匹配成功正则 
$1 !~     /正则/ # 第一段内容没有匹配成功正则
  • 比較演算:
 NR >= 3 && NR <=5 # 3到5行
$1 == "root" # 第一段内容等于root

1.6アクションは

$ 1、$ 3を印刷

1.7使用

cat 2.txt  # 已编辑好的文本
---------------------------------
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


3.grep

1.1使用法

grep 选项 '正则' 文件路径

1.2オプション

  • -n、-line-numberは、フィルタリングされた各行の前のファイルに相対的な行番号を追加します
  • -i、-ignore-case無視する場合
  • -カラーカラー
  • -l、-files-with-matches一致が成功した場合はファイル名のみが出力され、失敗した場合は出力されません。通常は-rl、grep -rl'root '/ etcと一緒に使用されます。
  • -R、-r、-recursive recursion

1.3例

grep '^root' /etc/passwd  # 过滤在/etc/passwd 文件下以root开头的文件
-------------------------------
root:x:0:0:root:/root:/bin/bash

grep -n '^root' /etc/passwd  # 过滤在/etc/passwd 文件下以root开头的文件
-------------------------------
1:root:x:0:0:root:/root:/bin/bash  # 行数

grep -n 'bash$' /etc/passwd  # 过滤在/etc/passwd 文件下以bashjiewei的文件
-------------------------------
1:root:x:0:0:root:/root:/bin/bash

grep -rl '^root' /etc  # 匹配 etc文件夹下 以root开头的文件 匹配成功打印出来 不成功不打印


# grep 也支持管道,我们可以发现三剑客命令都支持管道
[root@localhost ~]# ps aux |grep ssh
root 968 0.0 0.2 112908 4312 ? Ss 14:05 0:00 /usr/sbin/sshd
-D
root 1305 0.0 0.3 163604 6096 ? Ss 14:05 0:00 sshd:
root@pts/0
root 1406 0.0 0.3 163600 6240 ? Ss 14:05 0:00 sshd:
root@pts/1
root 2308 0.0 0.0 112724 984 pts/1 R+ 15:30 0:00 grep --
color=auto ssh
[root@localhost ~]# ps aux |grep [s]sh
root 968 0.0 0.2 112908 4312 ? Ss 14:05 0:00 /usr/sbin/sshd
-D
root 1305 0.0 0.3 163604 6096 ? Ss 14:05 0:00 sshd:
root@pts/0
root 1406 0.0 0.3 163600 6240 ? Ss 14:05 0:00 sshd:
root@pts/1

2、ファイル管理-ファイル検索

1.コマンドが属するファイルを表示します

[root@hecs-x-medium-2-linux-20201118090009 home]# which ip
/usr/sbin/ip
[root@hecs-x-medium-2-linux-20201118090009 home]# whereis ip
ip: /usr/sbin/ip /usr/share/man/man8/ip.8.gz

2.ファイルを探す

find [options] [path...] [expression]

ファイル名別

[root@localhost ~]# find /etc -name "ifcfg-eth0"
[root@localhost ~]# find /etc -iname "ifcfg-eth0" # -i忽略大小写
[root@localhost ~]# find /etc -iname "ifcfg-eth*"

ファイルサイズ別

[root@localhost ~]# find /etc -size +3M # 大于3M
[root@localhost ~]# find /etc -size 3M
[root@localhost ~]# find /etc -size -3M
[root@localhost ~]# find /etc -size +3M -ls # -ls找到的处理动作

検索ディレクトリの深さを指定します。

-maxdepth levels
[root@localhost ~]# find / -maxdepth 5 -a -name "ifcfg-eth0" # -a并且,-o或者,
不加-a,默认就是-a

時間で検索(atime、mtime、ctime):

変更していないので、基本的に違いはありません

[root@localhost ~]# find /etc -mtime +3 # 修改时间超过3天
[root@localhost ~]# find /etc -mtime 3 # 修改时间等于3天
[root@localhost ~]# find /etc -mtime -3 # 修改时间3天以内

ファイルの所有者とグループで検索:

[root@localhost ~]# find /home -user egon # 属主是egon的文件
[root@localhost ~]# find /home -group it # 属组是it组的文件
[root@localhost ~]# find /home -user egon -group it
[root@localhost ~]# find /home -user egon -a -group it # 同上意思一样
[root@localhost ~]# find /home -user egon -o -group it
[root@localhost ~]# find /home -nouser # 用户还存在,在/etc/passwd中删除了记录
[root@localhost ~]# find /home -nogroup # 用户还存在,在/etc/group中删除了记录
[root@localhost ~]# find /home -nouser -o -nogroup

ファイルタイプ別:

[root@localhost ~]# find /dev -type f # f普通
[root@localhost ~]# find /dev -type d # d目录
[root@localhost ~]# find /dev -type l # l链接
[root@localhost ~]# find /dev -type b # b块设备
[root@localhost ~]# find /dev -type c # c字符设备
[root@localhost ~]# find /dev -type s # s套接字
[root@localhost ~]# find /dev -type p # p管道文件

iノード番号に従って検索します。-inumn

[root@localhost ~]# find / -inum 1811

ファイルのアクセス許可による

[root@localhost ~]# find . -perm 644 -ls
[root@localhost ~]# find . -perm -644 -ls
[root@localhost ~]# find . -perm -600 -ls
[root@localhost ~]# find /sbin -perm -4000 -ls # 包含set uid
[root@localhost ~]# find /sbin -perm -2000 -ls # 包含set gid
[root@localhost ~]# find /sbin -perm -1000 -ls # 包含sticky

後処理アクションを見つけます。

-print
-ls
-delete
-exec
-ok
[root@localhost ~]# find /etc -name "ifcfg*" -print # 必须加引号
[root@localhost ~]# find /etc -name "ifcfg*" -ls
[root@localhost ~]# find /etc -name "ifcfg*" -exec cp -rvf {} /tmp \; # 非交互
[root@localhost ~]# find /etc -name "ifcfg*" -ok cp -rvf {} /tmp \; # 交互 每个都要yes或者y
[root@localhost ~]# find /etc -name "ifcfg*" -exec rm -rf {} \;
[root@localhost ~]# find /etc -name "ifcfg*" -delete # 同上

3.ファイルのアップロードとダウンロード

1.ダウンロード

wgetコマンド

wget -O 本地路径 远程包链接地址 # 将远程包下载到本地,-O指定下载到哪里,可以生路-O 本地路径
# ps:如果wget下载提示无法建立SSL连接,则加上选项--no-check-certificate
wget --no-check-certificate -O 本地路径 远程包链接地址

curlコマンド

#curl命令是一个利用URL规则在命令行下工作的文件传输工具。它支持文件的上传和下载,所以是综合传输工具,但按传统,习惯称curl为下载工具。作为一款强力工具,curl支持包括HTTP、HTTPS、[ftp]等众多协议,还支持POST、cookies、认证、从指定偏移处下载部分文件、用户代理字符串、限速、文件大小、进度条等特征。做网页处理流程和数据检索自动化,curl可以祝一臂之力。


[root@localhost ~]# curl -o 123.png https://www.xxx.com/img/hello.png
# ps: 如果遇到下载提示无法简历SSL链接,使用-k选项或者--insecure

curl -k -o name.jpg https://www.bing.com/th?id=OHR.BernCH_ZH-CN0890742909_1920x1080.jpg&rf=LaDigue_1920x1080.jpg&pid=HpEdgeAn

[外部リンク画像の転送に失敗しました。元のサイトにヒル防止リンクメカニズムがある可能性があります。画像を保存して直接アップロードすることをお勧めします(img-dEeFs3BR-1606126022318)(https://i.loli.net/2020/ 11/23 / 9qFoasYSEmQjcLi.png)]

szコマンドこのコマンドはlrzszをインストールする必要があります

システムにはデフォルトでこのコマンドがありません。ダウンロードする必要があります。yuminstalllrzsz-y#サーバー上の選択したファイルをダウンロード/マシンに送信します。

画像-20201123165528305

2アップロード

rzコマンドはデフォルトでインストールする必要はありません

# 系统默认没有该命令,需要下载:yum install lrzsz -y
# 运行该命令会弹出一个文件选择窗口,从本地选择文件上传到服务器。
[root@localhost opt]# rz # 如果文件已经存,则上传失败,可以用-E选项解决
[root@localhost opt]# rz -E # -E如果目标文件名已经存在,则重命名传入文件。新文件名将添加一
个点和一个数字(0..999)

[外部リンク画像の転送に失敗しました。ソースサイトにヒル防止リンクメカニズムがある可能性があります。画像を保存して直接アップロードすることをお勧めします(img-SjBUIXCp-1606126022322)(https://i.loli.net/2020/ 11/23 / V4LzBTuXAECFIHc.png)]

4つのファイル管理:入力とリダイレクト

5つのファイル管理:文字処理コマンド

1.sortコマンド

ファイルの内容を並べ替えるために使用されます

  • -n#値で並べ替え
  • -r#逆順で並べ替え
  • -k#列で並べ替え
  • -t#区切り文字を指定します。デフォルトでは、区切り文字としてスペースを使用します

ファイルを準備し、不要なコンテンツのセクションを書き込みます

cat >> test.txt  <<GG  # 打开test.txt文件 追加写入(>>)内容 出现 GG时 结束写入
> b:3
> c:2
> a:4
> e:5
> d:1
> f:11
> GG

基本的な並べ替え

sort test.txt  # 默认是以第一个排序的  不区分大小写
---------------------------
a:4
b:3
c:2
d:1
e:5
f:11
Z:77

sort test.txt -r  # 倒序 == sort -r test.txt
Z:77
f:11
e:5
d:1
c:2
b:3
a:4

コンテンツの並べ替えを選択

sort -t ':' -n -k2 test.txt  # 以':'作为分隔(-t) 用分隔后的第二个(k2)数字(-n)元素 正向排序
-------------------------------------
d:1
c:2
b:3
a:4
e:5
f:11
Z:77

# 逆序同理

2.Uniqコマンドはユニークです

テキストファイル内の繰り返される行と列をチェックおよび削除するために使用されます。通常、sortコマンドと組み合わせて使用​​されます。

  • -c#各列の横にある行の繰り返し数を表示します。
  • -d#繰り返される行と列のみを表示します。
  • -u#ランクを1回だけ表示します。

ファイルを準備し、順序付けられていないコンテンツを書き込みます

cat >> 2.txt <<GG
> hello
> 123
> hello
> 123
> func
> gg
> GG

基本的な使い方

sort 2.txt 
-----------------------------
123
123
func
gg
hello
hello

sort 2.txt |uniq   # 不重复排序
----------------------------
123
func
gg
hello

-cカウント数を使用

sort 2.txt | uniq -c  # 不重复排序 并显示出现次数
---------------------------
      2 123
      1 func
      1 gg
      2 hello

-dの使用は複数回表示されますか?デュオ?

sort 2.txt | uniq -d
--------------------------
123
hello

-uuniqueの使用は一度現れます

sort 2.txt | uniq -u
-------------------------
func
gg

3.カットコマンド

cutコマンドは、行の指定された部分を表示し、ファイル内の指定されたフィールドを削除するために使用されます

  • -d#フィールド区切り文字を指定します。デフォルトのフィールド区切り文字は「TAB」です。
  • -f#指定されたフィールドの内容を表示します。
head -1 /etc/passwd
------------------------------
1    2 3 4  5     6     7 
root:x:0:0:root:/root:/bin/bash

head -1 /etc/passwd | cut -d ':' -f1,2,4,6,7  # 上条显示的内容 以':'分隔(-d) 显示(-f)1,2,4,6,7这几个内容
------------------------------
root:x:0:/root:/bin/bash

5.4trコマンド

コマンドの置換または削除

  • -d#文字を削除します
[root@localhost ~]# head -1 /etc/passwd |tr "root" "ROOT"
ROOT:x:0:0:ROOT:/ROOT:/bin/bash
[root@localhost ~]#
[root@localhost ~]# head -1 /etc/passwd |tr -d "root"  # 删除 root字符
:x:0:0::/:/bin/bash
[root@localhost ~]# echo "hello egon qq:378533872" > a.txt
[root@localhost ~]# tr "egon" "EGON" < a.txt
hEllO EGON qq:378533872  # 吧egon替换为大写

5.5wcコマンド

統計

  • -c#ファイルのバイト数を数えます。
  • -l#ファイルの行数を数えます。
  • -w#ファイル内の単語数をカウントします。デフォルトでは、空白文字が区切り文字として使用されます。
[root@localhost ~]# ll file.txt
-rw-r--r--. 1 root root 25 8月 12 20:09 file.txt
[root@localhost ~]# wc -c file.txt
25 file.txt
[root@localhost ~]# cat file.txt
hello
123
hello
123
func
[root@localhost ~]# wc -l file.txt
5 file.txt
[root@localhost ~]# grep "hello" file.txt |wc -l
2
[root@localhost ~]# cat file.txt
hello
123
hello
123
func
[root@localhost ~]# wc -w file.txt
5 file.txt

おすすめ

転載: blog.csdn.net/A1L__/article/details/110004002