45 Efficient Linux Command Combinations to Solve 99% of Daily Operations Needs!

Script HomeSet as “Starred” to receive article updates promptly45 Efficient Linux Command Combinations to Solve 99% of Daily Operations Needs!Source: Haodao Linux (ID: hao_tiyu)Today, I bring you 45 efficient Linux command combinations. By utilizing these commands effectively, you will find that they can solve 99% of daily operational tasks.The following related efficient commands are listed without any particular order, focusing more on providing a better user experience with simple command forms.1. How to quickly create multiple files of the same type;For example, create batch files named haodao1.py, haodao2.py, up to haodao100.py.

touch haodao{1..100}.py

2. How to quickly generate a large file;Sometimes you need to generate a large file on your local machine for testing disk read and write capabilities. You can use the dd command to create a large file.

dd if=/dev/zero of=/root/haodaolinux/test.txt bs=1M count=1024

This command generates a file named test.txt of size 1G in the /root/haodaolinux directory.3. Methods to quickly clear a file, such as clearing the haodao.py file;

cat /dev/null > haodao.py

or

echo -n "" > haodao.py

or

true > haodao.py

or

: > haodao.py

or

truncate -s 0 haodao.py

These five methods for quickly clearing a file can be chosen based on your preference. How many do you know?

4. Find the file named haodao.py in the current directory;

find . -name haodao.py

5. Find files in the current directory that end with .py;

find . -name "*.py"

6. Find files in the current directory that end with .py and specify the file type for the search;

find . -type f -name "*.py"

7. Find directories in the current directory that contain the name haodao;

find . -type d -name "haodao*"

8. Find files in the current directory with permissions set to 755;

find . -type f -perm 755

9. Find all files in the current directory that do not have 755 permissions;

find . -type f ! -perm 755

10. Find all files in the current directory with 777 permissions and change all these permissions to 755;

find . -type f -perm 777 -exec chmod 755 {} \;

11. Find all files in the current directory with sizes between 100MB and 1GB;

find . -type f -size +100M -size -1G

12. Find and delete all files in the current directory that end with .py;

find . -name "*.py" -exec rm -rf {} \;

or

find . -name "*.py" | xargs rm -rf {};

or

rm -rf $(find . -name "*.py")

Which of these three methods for quickly deleting specified file types do you find more useful?

13. Find all files in the current directory modified more than 30 days ago;

find . -mtime 30

14. Find all files in the current directory accessed more than 30 days ago;

find . -atime 30

15. Find all files in the current directory modified within the last hour;

find . -mmin -60

16. Find all files in the current directory accessed within the last hour;

find . -amin -60

17. Find all files in the current directory modified more than 10 days ago but less than 30 days ago;

find . -mtime +10 -mtime -30

18. Find files created more than 7 days ago in the current directory that end with .py and delete them;

find . -mtime +7 -name "*.py" | xargs rm -rf {};

19. Find files larger than 1000M in the current directory and move them to /root/home;

find . -size +1000M -exec mv {} /root/home ;

20. Find files in the current directory created more than 30 days ago, larger than 1000M, that end with .py, and delete them;

find  . -name "*.py" –mtime +30 –type f –size +1000M |xargs rm –rf {};

21. View how many logical CPUs there are, including CPU model;

cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c

22. View how many CPUs there are, and how many cores each has;

cat /proc/cpuinfo | grep physical | uniq -c

23. Run a command in the background, such as running the command ping www.baidu.com in the background;

nohup ping www.baidu.com &

This command runs in the background and outputs to nohup.out.or

nohup ping www.baidu.com > /dev/null &

This command runs in the background without outputting any logs.

or

nohup ping www.baidu.com >out.log 2>&1 &

This command runs in the background and redirects error messages to the log.24. Forcefully close all processes with names containing xxx;

ps aux|grep xxx | grep -v grep | awk '{print $2}' | xargs kill -9

25. Display the top 20 directories or files in the /var directory sorted by size;

du -xB M --max-depth=2 /var | sort -rn | head -n 20

26. List the largest 10 files or directories in the current directory in descending order;

du -s * | sort -n | tail

27. Find the top 20 processes with high memory usage in the current system;

ps -aux | sort -rnk 4 | head -20

You can see that the 4th column of the output is the percentage of memory usage. The last column is the corresponding process.

28. Find the top 20 processes with high CPU usage in the current system;

ps -aux | sort -rnk 3 | head -20

You can see that the 3rd column of the output is the percentage of CPU usage, and the last column is the corresponding process.

29. Continuously ping in the background and log the results;

ping www.baidu.com | awk '{ print $0 " " strftime("%Y-%m-%d %H:%M:%S",systime()) }' >> /root/haodaoping.log &

This command automatically pings www.baidu.com every second in the background and logs the results to/root/haodaoping.log.

30. Find the top 15 IPs with the highest request counts on port 80;

netstat -anlp|grep 80|grep tcp|awk '{print $5}' |awk -F: '{print $1}'|sort|uniq -c|sort -nr|head -n 15

31. View the 10 most commonly used commands on your local system;

cat /root/.bash_history |grep -v ^# |awk '{print $1}' |sort |uniq -c |sort -nr |head -10

32. View how many IPs accessed your Tomcat server at 09:00 on December 11, 2022;

awk '{print $4,$1}' access.log | grep 11/Dec/2022:09 | awk '{print $2}'| sort | uniq | wc -l

Just provide the access log file, and you can calculate the number of IPs accessing at a specific time.

33. View the top 20 IP addresses accessing your Tomcat server;

cat access.log|awk '{print $1}'|sort|uniq -c|sort -nr|head -20

34. View which pages a specific IP address accessed on your Tomcat server;

grep ^192.168.30.200 access.log| awk '{print $1,$7}'

35. View how many times a specific page resource was accessed on your Tomcat server;

grep "/portal/index.html" access.log | wc -l

36. Use the packet capture tool tcpdump to view access volume on port 8080;

tcpdump -i ens33 -tnn dst port 8080 -c 1000 | awk -F"." '{print $1"."$2"."$3"."$4}' | sort | uniq -c | sort -nr |head -10

37. How to use the sed command to replace all occurrences of haodao in the file test.py with HAODAO;

sed -i "s/haodao/HAODAO/g" test.py

38. Replace the directory in the haodao.py file;

sed -i "s:/etc/dhcp:/home:g" haodao.py

This means replacing the /etc/dhcp directory in the haodao.py file with the /home directory.

39. Common sed command techniques illustrated with the haodao.py file;

1) Remove the # character at the beginning of the line, command:

sed -i "s/^#//g" haodao.py

2) Add the linux character at the beginning of the line, command:

sed -i "s/^/linux/g" haodao.py

Add a study character at the end of the line, command:

sed -i "s/$/study/" haodao.py

4) Add a you character after the line containing we love, command:

sed -i "/we love/ayou" haodao.py

5) Add a where character before the line containing haodao, command:

sed -i "/haodao/iwhere" haodao.py

40. Capture packets on interface ens33 without size limit and save to file haodao.cap;

tcpdump -i ens33 -s 0 -w haodao.cap

41. Capture packets on interface ens33, filtering for icmp packets with source IP address 192.168.20.231;

tcpdump icmp and src 192.168.20.231 -i ens33 -n

42. Capture packets on interface ens33, filtering for packets with source IP address 192.168.20.231;

tcpdump src host 192.168.20.231 -i ens33 -n -c 5

43. Capture packets on interface ens33, filtering for packets with destination IP address 192.168.20.231;

tcpdump dst host 192.168.20.231 -i ens33 -n -c 5

44. Capture packets on interface ens33, filtering for packets with port number 8080;

tcpdump port 8080 -i ens33 -n -c 5

45. Capture packets on interface ens33, filtering for packets from port 80 to port 443;

tcpdump portrange 80-443 -i ens33 -n -c 8

  Recommended Reading:

  1. Why do some domestic Linux systems’ UIs feel like toys?
  2. For a computer novice wanting to get into Linux, which Linux distribution(s) would you recommend?
  3. What are the benefits of learning Linux?
  4. There are steam locomotives in Linux that appeared in Doraemon.
  5. Linux environment variables, knowledge summary

Leave a Comment