Home System Tutorial LINUX Master these 17 Linux operating skills, and sometimes you can increase your salary!

Master these 17 Linux operating skills, and sometimes you can increase your salary!

Feb 14, 2024 pm 08:15 PM
linux linux tutorial linux system Firewall configuration linux command shell script overflow embeddedlinux Getting started with linux linux learning

本篇文章重点为大家讲解一下linux中的17个操作技巧,有需要的小伙伴可以参考一下。

Master these 17 Linux operating skills, and sometimes you can increase your salary!

1、查找当前目录下所有以.tar结尾的文件然后移动到指定目录:

find . -name “*.tar” -exec mv {}./backup/ ;
Copy after login

注解:find –name 主要用于查找某个文件名字,-exec 、xargs 可以用来承接前面的结果,然后将要执行的动作,一般跟 find 在一起用的很多,find 使用我们可以延伸 -mtime 查找修改时间、-type 是指定对象类型(常见包括 f 代表文件、d代表目录),-size 指定大小,例如经常用到的:查找当前目录30天以前大于100M的LOG文件并删除。

find . -name "*.log" –mtime +30 –type f –size +100M | xargs rm –rf {};
Copy after login

2、批量解压当前目录下以 .zip 结尾的所有文件到指定目录:

for i  in  `find . –name “*.zip”–type f `

do

unzip –d $i /data/www/img/

done
Copy after login

注解:for i in (command); do … done 为 for 循环的一个常用格式,其中I为变量,可以自己指定。

3、sed常用命收集:test.txt做测试

如何去掉行首的.字符:

sed -i ‘s/^.//g’ test.txt
Copy after login

在行首添加一个a字符:

sed’s/^/a/g’    test.txt
Copy after login

在行尾添加一个a字符:

sed’s/$/a/‘     tets.txt
Copy after login

在特定行后添加一个c字符:

sed ‘/wuguangke/ac’ test.txt
Copy after login

在行前加入一个c字符:

sed’/wuguangke/ic’ test.txt
Copy after login

更多sed命令请查阅相关文档。

4、如何判断某个目录是否存在,不存在则新建,存在则打印信息。

if

[! –d /data/backup/];then

Mkdir–p /data/backup/

else

echo  "The Directory alreadyexists,please exit"

fi
Copy after login

注解:if…;then …else ..fi:为if条件语句,!叹号表示反义“不存在“,-d代表目录。

5、监控linux磁盘根分区,如果根分区空间大于等于90%,发送邮件给Linux SA

(1)、打印根分区大小

df -h |sed -n '//$/p'|awk '{print $5}'|awk –F ”%” '{print $1}'
Copy after login

注解:awk ‘{print $5}’意思是打印第5个域,-F的意思为分隔,例如以%分隔,简单意思就是去掉百分号,awk –F. ‘{print $1}’分隔点.号。

(2)、if条件判断该大小是否大于90,如果大于90则发送邮件报警

while sleep 5m

do

for i in `df -h |sed -n '//$/p' |awk '{print $5}' |sed 's/%//g'`

do

echo $i

if [ $i -ge 90 ];then

echo “More than 90% Linux of disk space ,Please LinuxSA Check Linux Disk !” |mail -s “Warn Linux / Parts is $i%” 

XXX@XXX.XX

fi

done

done
Copy after login

6、统计 Nginx 访问日志,访问量排在前20 的 ip地址:

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

注解:sort排序、uniq(检查及删除文本文件中重复出现的行列 )

7、sed另外一个用法找到当前行,然后在修改该行后面的参数:

sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config
Copy after login

Sed冒号方式 sed -i ‘s:/tmp:/tmp/abc/:g’test.txt意思是将/tmp改成/tmp/abc/。

8、打印出一个文件里面最大和最小值:

cat a.txt |sort -nr|awk ‘{}END{print} NR==1′

cat a.txt |sort -nr |awk ‘END{print} NR==1′
Copy after login

这个才是真正的打印最大最小值:sed ‘s/ / /g’ a.txt |sort -nr|sed -n ’1p;$p’

9、使用snmpd抓取版本为v2的cacti数据方式:

snmpwalk -v2c -c public 192.168.0.241
Copy after login

10、修改文本中以jk结尾的替换成yz:

sed -e ‘s/jk$/yz/g’ b.txt
Copy after login

11、网络抓包:Tcpdump

tcpdump -nn host 192.168.56.7 and port 80 抓取56.7通过80请求的数据包。

tcpdump -nn host 192.168.56.7 or ! host 192.168.0.22 and port 80 排除0.22 80端口!

tcp/ip 7层协议物理层–数据链路层-网络层-传输层-会话层-表示层-应用层。
Copy after login

12、显示最常用的20条命令:

cat .bash_history | grep -v ^# | awk ‘{print $1}’ | sort | uniq -c | sort -nr | head-20
Copy after login

13、写一个脚本查找最后创建时间是3天前,后缀是*.log 的文件并删除。

find . -mtime +3  -name "*.log" |xargs rm -rf {} ;
Copy after login

14、写一个脚本将某目录下大于100k的文件移动至/tmp下。

find . -size +100k -exec mv {} /tmp ;
Copy after login

15、写一个防火墙配置脚本,只允许远程主机访问本机的80端口。

iptables -F

iptables -X

iptables -A INPUT -p tcp --dport 80 -j accept

iptables -A INPUT -p tcp -j REJECT
Copy after login

或者

iptables -A INPUT -m state --state NEW-m tcp -p tcp --dport 80 -j ACCEPT
Copy after login

16、写一个脚本进行 Nginx 日志统计,得到访问 IP 最多的前10个(nginx日志路径:

/home/logs/nginx/default/access.log)。

cd /home/logs.nginx/default

sort -m -k 4 -o access.logok access.1 access.2 access.3 .....

cat access.logok |awk '{print $1}'|sort -n|uniq -c|sort -nr |head -10
Copy after login

17、替换文件中的目录

sed 's:/user/local:/tmp:g' test.txt
Copy after login

或者

sed -i 's//usr/local//tmp/g' test.txt
Copy after login

The above is the detailed content of Master these 17 Linux operating skills, and sometimes you can increase your salary!. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What computer configuration is required for vscode What computer configuration is required for vscode Apr 15, 2025 pm 09:48 PM

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

vscode terminal usage tutorial vscode terminal usage tutorial Apr 15, 2025 pm 10:09 PM

vscode built-in terminal is a development tool that allows running commands and scripts within the editor to simplify the development process. How to use vscode terminal: Open the terminal with the shortcut key (Ctrl/Cmd). Enter a command or run the script. Use hotkeys (such as Ctrl L to clear the terminal). Change the working directory (such as the cd command). Advanced features include debug mode, automatic code snippet completion, and interactive command history.

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

What is the main purpose of Linux? What is the main purpose of Linux? Apr 16, 2025 am 12:19 AM

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

How to run sublime after writing the code How to run sublime after writing the code Apr 16, 2025 am 08:51 AM

There are six ways to run code in Sublime: through hotkeys, menus, build systems, command lines, set default build systems, and custom build commands, and run individual files/projects by right-clicking on projects/files. The build system availability depends on the installation of Sublime Text.

See all articles