


Master these 17 Linux operating skills, and sometimes you can increase your salary!
本篇文章重点为大家讲解一下linux中的17个操作技巧,有需要的小伙伴可以参考一下。

1、查找当前目录下所有以.tar结尾的文件然后移动到指定目录:
find . -name “*.tar” -exec mv {}./backup/ ;
“
❝
注解: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 {};
2、批量解压当前目录下以 .zip 结尾的所有文件到指定目录:
for i in `find . –name “*.zip”–type f ` do unzip –d $i /data/www/img/ done
“
❝
注解:for i in (command); do … done 为 for 循环的一个常用格式,其中I为变量,可以自己指定。
”
3、sed常用命收集:test.txt做测试
如何去掉行首的.字符:
sed -i ‘s/^.//g’ test.txt
在行首添加一个a字符:
sed’s/^/a/g’ test.txt
在行尾添加一个a字符:
sed’s/$/a/‘ tets.txt
在特定行后添加一个c字符:
sed ‘/wuguangke/ac’ test.txt
在行前加入一个c字符:
sed’/wuguangke/ic’ test.txt
更多sed命令请查阅相关文档。
4、如何判断某个目录是否存在,不存在则新建,存在则打印信息。
if [! –d /data/backup/];then Mkdir–p /data/backup/ else echo "The Directory alreadyexists,please exit" fi
注解:if…;then …else ..fi:为if条件语句,!叹号表示反义“不存在“,-d代表目录。
5、监控linux磁盘根分区,如果根分区空间大于等于90%,发送邮件给Linux SA
(1)、打印根分区大小
df -h |sed -n '//$/p'|awk '{print $5}'|awk –F ”%” '{print $1}'
注解: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
6、统计 Nginx 访问日志,访问量排在前20 的 ip地址:
cat access.log |awk '{print $1}'|sort|uniq -c |sort -nr |head -20
“
❝
注解:sort排序、uniq(检查及删除文本文件中重复出现的行列 )
”
7、sed另外一个用法找到当前行,然后在修改该行后面的参数:
sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config
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′
这个才是真正的打印最大最小值:sed ‘s/ / /g’ a.txt |sort -nr|sed -n ’1p;$p’
9、使用snmpd抓取版本为v2的cacti数据方式:
snmpwalk -v2c -c public 192.168.0.241
10、修改文本中以jk结尾的替换成yz:
sed -e ‘s/jk$/yz/g’ b.txt
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层协议物理层–数据链路层-网络层-传输层-会话层-表示层-应用层。
12、显示最常用的20条命令:
cat .bash_history | grep -v ^# | awk ‘{print $1}’ | sort | uniq -c | sort -nr | head-20
13、写一个脚本查找最后创建时间是3天前,后缀是*.log 的文件并删除。
find . -mtime +3 -name "*.log" |xargs rm -rf {} ;
14、写一个脚本将某目录下大于100k的文件移动至/tmp下。
find . -size +100k -exec mv {} /tmp ;
15、写一个防火墙配置脚本,只允许远程主机访问本机的80端口。
iptables -F iptables -X iptables -A INPUT -p tcp --dport 80 -j accept iptables -A INPUT -p tcp -j REJECT
或者
iptables -A INPUT -m state --state NEW-m tcp -p tcp --dport 80 -j ACCEPT
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
17、替换文件中的目录
sed 's:/user/local:/tmp:g' test.txt
或者
sed -i 's//usr/local//tmp/g' test.txt
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!

The main difference between Linux and Windows in process management lies in the implementation and concept of tools and APIs. Linux is known for its flexibility and power, relying on kernel and command line tools; while Windows is known for its user-friendliness and integration, mainly managing processes through graphical interfaces and system services.

Linuxisidealforcustomization,development,andservermanagement,whileWindowsexcelsineaseofuse,softwarecompatibility,andgaming.Linuxoffershighconfigurabilityfordevelopersandserversetups,whereasWindowsprovidesauser-friendlyinterfaceandbroadsoftwaresupport

The main difference between Linux and Windows in user account management is the permission model and management tools. Linux uses Unix-based permissions models and command-line tools (such as useradd, usermod, userdel), while Windows uses its own security model and graphical user interface (GUI) management tools.

Linux'scommandlinecanbemoresecurethanWindowsifmanagedcorrectly,butrequiresmoreuserknowledge.1)Linux'sopen-sourcenatureallowsforquicksecurityupdates.2)Misconfigurationcanleadtovulnerabilities.Windows'commandlineismorecontrolledbutlesscustomizable,with

This guide explains how to automatically mount a USB drive on boot in Linux, saving you time and effort. Step 1: Identify Your USB Drive Use the lsblk command to list all block devices. Your USB drive will likely be labeled /dev/sdb1, /dev/sdc1, etc

Cross-platform applications have revolutionized software development, enabling seamless functionality across operating systems like Linux, Windows, and macOS. This eliminates the need to switch apps based on your device, offering consistent experien

Artificial Intelligence (AI) is rapidly transforming numerous sectors, from healthcare and finance to creative fields like art and music. Linux, with its open-source nature, adaptability, and performance capabilities, has emerged as a premier platfo

Looking for a fast, minimal, and efficient Linux distribution without a graphical user interface (GUI)? Lightweight, GUI-less Linux distros are perfect for older hardware or specialized tasks like servers and embedded systems. They consume fewer res


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version
Visual web development tools

Atom editor mac version download
The most popular open source editor

SublimeText3 Mac version
God-level code editing software (SublimeText3)
