實用命令

本部分內容爲日常工作實用命令,會長期更新。

1. 去掉註釋行和空行

1    grep -Ev "^#|^$" file

有時候需要查看某個配置文件的內容,直接查看往往包含很多默認的註釋信息,信息混亂翻頁麻煩,使用上述命令可去掉行首爲#和空行,非#號開始的註釋,可自行修改,一般情況夠用了。

-E 意思爲擴展正則表達式,-v爲方向匹配,"^#|^$" 意思匹配#開頭或者空行

示例:

1    ➜ grep -Ev "^#|^$" /etc/dnsmasq.conf
2    resolv-file=/etc/resolv.dnsmasq
3    listen-address=127.0.0.1
4    no-hosts
5    hostsdir=/etc/hosts.dnsmasq
6    cache-size=1024

2. 一次性創建多個文件或者目錄

1    touch {part1,part2,part3}

mkdir 類似,就是用大括號把東西括起來,在逗號分割待創建的內容,如果是數字或者字母還有變種

請看示例:

 1test touch {one,two,three,four,five}.py
 2test ls
 3    example_dir  five.py  four.py  one.py  three.py  two.py
 4test touch {tony,mike,eric,json,joe}
 5test ls
 6    eric  example_dir  five.py  four.py  joe  json  mike  one.py  three.py  tony  two.py
 7test touch {1..5}.txt
 8test ls -al *.txt
 9    -rw-rw-r-- 1 mephisto mephisto 0 Nov 17 14:57 1.txt
10    -rw-rw-r-- 1 mephisto mephisto 0 Nov 17 14:57 2.txt
11    -rw-rw-r-- 1 mephisto mephisto 0 Nov 17 14:57 3.txt
12    -rw-rw-r-- 1 mephisto mephisto 0 Nov 17 14:57 4.txt
13    -rw-rw-r-- 1 mephisto mephisto 0 Nov 17 14:57 5.txt
14test touch {a..f}.md
15test ls -al *.md
16    -rw-rw-r-- 1 mephisto mephisto 0 Nov 17 15:00 a.md
17    -rw-rw-r-- 1 mephisto mephisto 0 Nov 17 15:00 b.md
18    -rw-rw-r-- 1 mephisto mephisto 0 Nov 17 15:00 c.md
19    -rw-rw-r-- 1 mephisto mephisto 0 Nov 17 15:00 d.md
20    -rw-rw-r-- 1 mephisto mephisto 0 Nov 17 15:00 e.md
21    -rw-rw-r-- 1 mephisto mephisto 0 Nov 17 15:00 f.md

3. 查找匹配某種文件後,進一步處理

這些操作當然可以用find + xarg完成,但是fd命令真的很好用,速度非常快。所以我推薦:

1    fd -e zip -x your_next_command

上面的意思爲匹配zip結尾,-x 後接你要的命令

示例,先創建10個txt文件,再追加.md結尾,接着把文件複製到example_dir目錄

Here, {} is a placeholder for the search result. {.} is the same, without the file extension. {}表示每個匹配的結果,{.}表示每個不帶文件後綴名的文件,有點怪,可以看看fd的文檔解釋。

 1test touch {1..10}.txt
 2test fd . -e txt
 3    1.txt
 4    10.txt
 5    2.txt
 6    3.txt
 7    4.txt
 8    5.txt
 9    6.txt
10    7.txt
11    8.txt
12    9.txt
13test fd . -e txt -x md5sum
14    d41d8cd98f00b204e9800998ecf8427e  ./4.txt
15    d41d8cd98f00b204e9800998ecf8427e  ./2.txt
16    d41d8cd98f00b204e9800998ecf8427e  ./3.txt
17    d41d8cd98f00b204e9800998ecf8427e  ./10.txt
18    d41d8cd98f00b204e9800998ecf8427e  ./6.txt
19    d41d8cd98f00b204e9800998ecf8427e  ./9.txt
20    d41d8cd98f00b204e9800998ecf8427e  ./7.txt
21    d41d8cd98f00b204e9800998ecf8427e  ./8.txt
22    d41d8cd98f00b204e9800998ecf8427e  ./1.txt
23    d41d8cd98f00b204e9800998ecf8427e  ./5.txt
24test fd . -e txt -x mv {} {}.md
25test ls -al
26    total 68
27    drwxrwxr-x  2 mephisto mephisto 12 Nov 17 14:36 .
28    drwxrwxrwt 35 root     root     49 Nov 17 14:33 ..
29    -rw-rw-r--  1 mephisto mephisto  0 Nov 17 14:34 10.txt.md
30    -rw-rw-r--  1 mephisto mephisto  0 Nov 17 14:34 1.txt.md
31    -rw-rw-r--  1 mephisto mephisto  0 Nov 17 14:34 2.txt.md
32    -rw-rw-r--  1 mephisto mephisto  0 Nov 17 14:34 3.txt.md
33    -rw-rw-r--  1 mephisto mephisto  0 Nov 17 14:34 4.txt.md
34    -rw-rw-r--  1 mephisto mephisto  0 Nov 17 14:34 5.txt.md
35    -rw-rw-r--  1 mephisto mephisto  0 Nov 17 14:34 6.txt.md
36    -rw-rw-r--  1 mephisto mephisto  0 Nov 17 14:34 7.txt.md
37    -rw-rw-r--  1 mephisto mephisto  0 Nov 17 14:34 8.txt.md
38    -rw-rw-r--  1 mephisto mephisto  0 Nov 17 14:34 9.txt.md
39test mkdir example_dir
40test fd . -e md -x cp {} example_dir
41test ls -al example_dir
42    total 28
43    drwxrwxr-x 2 mephisto mephisto 12 Nov 17 14:37 .
44    drwxrwxr-x 3 mephisto mephisto 13 Nov 17 14:37 ..
45    -rw-rw-r-- 1 mephisto mephisto  0 Nov 17 14:37 10.txt.md
46    -rw-rw-r-- 1 mephisto mephisto  0 Nov 17 14:37 1.txt.md
47    -rw-rw-r-- 1 mephisto mephisto  0 Nov 17 14:37 2.txt.md
48    -rw-rw-r-- 1 mephisto mephisto  0 Nov 17 14:37 3.txt.md
49    -rw-rw-r-- 1 mephisto mephisto  0 Nov 17 14:37 4.txt.md
50    -rw-rw-r-- 1 mephisto mephisto  0 Nov 17 14:37 5.txt.md
51    -rw-rw-r-- 1 mephisto mephisto  0 Nov 17 14:37 6.txt.md
52    -rw-rw-r-- 1 mephisto mephisto  0 Nov 17 14:37 7.txt.md
53    -rw-rw-r-- 1 mephisto mephisto  0 Nov 17 14:37 8.txt.md
54    -rw-rw-r-- 1 mephisto mephisto  0 Nov 17 14:37 9.txt.md
55test ls
56    10.txt.md  1.txt.md  2.txt.md  3.txt.md  4.txt.md  5.txt.md  6.txt.md  7.txt.md  8.txt.md  9.txt.md  example_dir

4. 排序去重

1    sort -u file

沒有那麼多操作很簡單 -u 去重,-o 結果輸出到另外一個文件

直接看示例:

 1test cat haha.txt
 2    hello
 3    192.168.1.8
 4    192.168.1.9
 5    192.168.1.9
 6    192.168.1.9
 7    192.168.1.9
 8    192.168.1.9
 9    192.168.1.5
10    192.168.1.1
11    192.168.1.3
12    192.168.1.1
13    192.168.1.22
14    world
15test sort -u haha.txt
16    192.168.1.1
17    192.168.1.22
18    192.168.1.3
19    192.168.1.5
20    192.168.1.8
21    192.168.1.9
22    hello
23    world
24test sort -u haha.txt -o result.txt
25test cat result.txt
26    192.168.1.1
27    192.168.1.22
28    192.168.1.3
29    192.168.1.5
30    192.168.1.8
31    192.168.1.9
32    hello
33    world
34test

未完待續...

最後修改於: Monday, August 28, 2023

相關文章:

翻譯: