Linux操作
Linux系统文件查找
# find 可以根据文件名、权限、拥有者、修改日期/时间、文件大小等等查找
# 根据文件名查找
find /etc -name "ifcfg-eth"
# 忽略文件名大小写
find /etc -iname "ifcfg-eth"
# 正则匹配所有et开头
find /etc -iname "ifcfg-et*"
#文件大小查找
find /etc -size +5M
# 文件大小查找并查看具体多大
find /etc -size +5M |xargs du -sh
find /etc -size +5M |xargs stat
find /etc -size +5M |xargs ls -l
# 查询等于5M的
find /etc -size 5M
#查询小于5M的
find /etc -size -5M
# 查找7天以前的文件(不会打印当天的文件)
find ./ -iname "file-*" -mtime +7
# 查找最近7天的文件(会打印当天的文件)
find ./ -iname "file-*" -mtime -7
# 查找第7天的文件(不会打印当天文件)
find ./ -iname "file-*" -mtime 7
# 找到7天以前的并删除
find ./ -iname "file-*" -mtime +7 -delete
# 按照用户查找
find /home -user root
# 按照文件类型查找
find /dec -type f -name #文件
find /dec -type d -name #目录
find /dec -type l -name #连接
find /dec -type b -name #b块设备
find /dec -type c -name #c字符设备
find /dec -type s -name #s套接字
find /dec -type p -name #管道文件
# 权限的方式查找
精确匹配644权限
find . -perm 644 -ls
find /usr/sbin -perm -4000 -ls # 包含set uid
find /usr/sbin -perm -2000 -ls # 包含set gid
find /usr/sbin -perm -1000 -ls # 包含sticky
# find处理动作
-prinf //打印
-ls // 以长格式打印
-delete // 删除查找到的文件(仅能删除空目录)
-exec //后面跟自定义的sell命令(标准写法exec \;)
-ok //后面跟自定义的sell命令(会提示是否操作)
//打印查询到的文件
find /etc -name "ifcfg*"
find /etc -name "ifcfg*" -print
find /etc -name "ifcfg*" -ls
//拷贝文件
find /etc -name "ifcfg*" -exec cp -rvf {} /tmp \;
//-ok会不断提示
find /etc -name "ifcfg*" -ok cp -rvf {} /tmp \;
//删除文件
find /etc -name "ifcfg*" -exec rm -f {} \;
find /etc -name "ifcfg*" -delete
#find结合xarg
find ./ name "file.txt" |xargs rm -f
find ./ name "file.txt" |xargs -I {} cp -rvf {} /var/tmp
Linux重定向与管道
# 查看标准输出设备
ls -l /dev/std*
ls /root /error > ab 2>&1 #正确的和错误的都输入到同一个文件里
#dd命令用于磁盘读写速率测试
dd if=/dev/zero of=/file1.txt bs=1M count=20 #读取dev/zero文件输出到file1.txt bs读取数据大小 count读取次数20次
dd </dev/zero >/file2/txt bs=1M count=20 # 输入内容到file 测试读速率
fio #读写性能测试
# 表结构导入
mysql -uroot -p123 < bbs.sql
# 数据库备份
mysqldump -uroot -p123 -B test > test.sql #-B指定哪个库
# 导入数据库
mysql -uroot -p123 < test.sql
# 非交互式执行sql语句
mysql -uroot -p123 -e "show databases;"
#建立多行文件
cat >file2.txt <<EOF # EOF特殊符号用于结束标记
# 管道符号|
ls |grep text # 将前边命令的标准输出 作为后边命令的标准输入
command1 | command2 | command3 | ... | conmmandN
# 将etc/passwd中的按照用户uid大小排序
sort -t ":" -k3 -n /etc/passwd | head -n 10 | tail -n 1 #用:作为分隔符按照第三列排序,取前10行中的最后一行
ps aux --sort=-%cpu |head -6 #前五个使用进程最少的cpu
# 统计网站访问情况top20
netstat -antup |egrep :80 | egrep ESTAB | sort |uniq -c | sort -k1 -rn |head -n 20
# 打印所有ip
ip addr |grep 'inet' |awk '{print $2}' |awk -F"/" "{print $1}"
# 查看以根结尾的使用率
df |grep "/$" | awk '{print $5}' |awk -F"%" '{print $1}'
#tee 先输出再重定向
df |grep 'inet' |tee ip.txt | awk '{print $5}' |awk -F"%" '{print $2}' # 查询出来inet数据放到ip.txt文件里,再对ip.txt操作
# xargs命令
head -5 /etc/passwd #查看文件前5行
head -5 /etc/passwd |tail -1 #查看文件前5行中最后一行
find /etc/ -name "p*" |xargs egrep -r "root" #找出来某个文件过滤
⚠️注意事项:
1、在管道后边的命令不可以写文件名
2、在管道里边只有标准输入才会传递下一个命令,标准错误会直接报错
3、有一些命令不支持管道,但是可以基于xargs实现
例如:
which cat |xargs ls -l
ls |xargs rm -rvf # r递归模式 v详细模式 f强制
ls |xargs cp -rvf /tmp/ # r递归模式 v详细模式 f强制
ls |xargs -i {} cp -rvf {} /tmp
ls |xargs mv -t /tmp/
ls | xargs -i {} mv {} /tmp

浙公网安备 33010602011771号