Practical Commands

This section contains useful commands for daily work and will be updated frequently.

1. Remove comments and blank lines

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

Sometimes you need to view the contents of a configuration file. Directly viewing often contains a lot of default comments, which can be confusing and difficult to scroll through. Using the above command removes lines starting with # and blank lines. Comments that do not begin with a # sign can be modified manually, and this is generally sufficient.

-E means extended regular expression, -v is a directional match, and "^#|^$" matches a line starting with # or a blank line.

Example:

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

2. Creating Multiple Files or Directories at Once

1touch {part1,part2,part3}

mkdir is similar, using curly braces to enclose the contents and commas to separate the contents to be created. There are also variations for numbers or letters.

See example:

 1test touch {one,two,three,four,five}.py
 2test ls 
 3example_dir five.py four.py one.py three.py two.py 
 4test touch {tony,mike,eric,json,joe} 
 5test ls 
 6eric 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. After finding matching files, perform further processing

These operations can certainly be accomplished with find + xarg, but the fd command is really useful and very fast. So I recommend:

1fd -e zip -x your_next_command

The above means to match the suffix "zip", and -x is followed by the command you want.

For example, first create 10 txt files, append the suffix ".md", and then copy the files to the "example_dir" directory.

Here, {} is a placeholder for the search result. {.} is the same, without the file extension. {} represents each matching result, and {.} represents each file without the file extension. This is a bit strange, so you can refer to the fd documentation for an explanation.

 1test touch {1..10}.txt 
 2test fd . -e txt 
 31.txt 
 410.txt 
 52.txt 
 63.txt 
 74.txt 
 85.txt 
 96.txt 
107.txt 
118.txt 
129.txt 
13test fd . -e txt -x md5sum 
14d41d8cd98f00b204e9800998ecf8427e ./4.txt 
15d41d8cd98f00b204e9800998ecf8427e ./2.txt 
16d41d8cd98f00b204e9800998ecf8427e ./3.txt d41d8cd98f00b204e9800998ecf8427e ./10.txt 
17d41d8cd98f00b204e9800998ecf8427e ./6.txt 
18d41d8cd98f00b204e9800998ecf8427e ./9.txt 
19d41d8cd98f00b204e9800998ecf8427e ./7.txt 
20d41d8cd98f00b204e9800998ecf8427e ./8.txt 
21d41d8cd98f00b204e9800998ecf8427e ./1.txt d41d8cd98f00b204e9800998ecf8427e ./5.txt 
22test fd . -e txt -x mv {} {}.md 
23test ls -al 
24total 68 
25drwxrwxr-x 2 mephisto mephisto 12 Nov 17 14:36 . 
26drwxrwxrwt 35 root root 49 Nov 17 14:33 .. 
27-rw-rw-r-- 1 mephisto mephisto 0 Nov 17 14:34 10.txt.md 
28-rw-rw-r-- 1 mephisto mephisto 0 Nov 17 14:34 1.txt.md 
29-rw-rw-r-- 1 mephisto mephisto 0 Nov 17 14:34 2.txt.md 
30-rw-rw-r-- 1 mephisto mephisto 0 Nov 17 14:34 3.txt.md 
31-rw-rw-r-- 1 mephisto mephisto 0 Nov 17 14:34 4.txt.md 
32-rw-rw-r-- 1 mephisto mephisto 0 Nov 17 14:34 5.txt.md 
33-rw-rw-r-- 1 mephisto mephisto 0 Nov 17 14:34 6.txt.md 
34-rw-rw-r-- 1 mephisto mephisto 0 Nov 17 14:34 7.txt.md 
35-rw-rw-r-- 1 mephisto mephisto 0 Nov 17 14:34 8.txt.md 
36-rw-rw-r-- 1 mephisto mephisto 0 Nov 17 14:34 9.txt.md 
37test mkdir example_dir 
38test fd . -e md -x cp {} example_dir 
39test ls -al example_dir 
40total 28 
41drwxrwxr-x 2 mephisto mephisto 12 Nov 17 14:37 . 
42drwxrwxr-x 3 mephisto mephisto 13 Nov 17 14:37 .. 
43-rw-rw-r-- 1 mephisto mephisto 0 Nov 17 14:37 10.txt.md 
44-rw-rw-r-- 1 mephisto mephisto 0 Nov 17 14:37 1.txt.md 
45-rw-rw-r-- 1 mephisto mephisto 0 Nov 17 14:37 2.txt.md 
46-rw-rw-r-- 1 mephisto mephisto 0 Nov 17 14:37 3.txt.md 
47-rw-rw-r-- 1 mephisto mephisto 0 Nov 17 14:37 4.txt.md 
48-rw-rw-r-- 1 mephisto mephisto 0 Nov 17 14:37 5.txt.md 
49-rw-rw-r-- 1 mephisto mephisto 0 Nov 17 14:37 6.txt.md 
50-rw-rw-r-- 1 mephisto mephisto 0 Nov 17 14:37 7.txt.md
51-rw-rw-r-- 1 mephisto mephisto 0 Nov 17 14:37 8.txt.md
52-rw-rw-r-- 1 mephisto mephisto 0 Nov 17 14:37 9.txt.md
53test ls
5410.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. Sorting and Duplicate Removal

1sort -u file

Simple, not too complex -u removes duplicates, -o outputs the results to another file

See the example directly:

 1test cat haha.txt 
 2hello 
 3192.168.1.8 
 4192.168.1.9 
 5192.168.1.9 
 6192.168.1.9 
 7192.168.1.9 
 8192.168.1.9 
 9192.168.1.5 
10192.168.1.1 
11192.168.1.3 
12192.168.1.1 
13192.168.1.22 
14world 
15test sort -u haha.txt 
16192.168.1.1 
17192.168.1.22 
18192.168.1.3
19192.168.1.5
20192.168.1.8
21192.168.1.9
22hello
23world
24test sort -u haha.txt -o result.txt
25test cat result.txt
26192.168.1.1
27192.168.1.22
28192.168.1.3
29192.168.1.5
30192.168.1.8
31192.168.1.9
32hello
33world
34test

To be continued...

Lastmod: Saturday, August 9, 2025

See Also:

Translations: