Linux-Day3-重定向与管道操作,find 高级使用

重定向与管道操作
 重定向
重定向输出:将前面命令的输出结果,写入到文本文件
>:覆盖重定向
>>:追加重定向

[root@localhost ~]# ls --help 
[root@localhost ~]# ls --help > /opt/ls.txt 
[root@localhost ~]# cat /opt/ls.txt

区分追加重定向和覆盖重定向

[root@localhost ~]# hostname 
[root@localhost ~]# hostname > /opt/ls.txt #覆盖重定向
[root@localhost ~]# cat /opt/ls.txt
[root@localhost ~]# hostname >> /opt/ls.txt #追加重定向
[root@localhost ~]# cat /opt/ls.txt 
[root@localhost ~]# hostname >> /opt/ls.txt 
[root@localhost ~]# cat /opt/ls.txt

过滤 passwd 文件包含 root 的行,并把结果写入到/opt/ls.txt 文件

[root@localhost ~]# grep root /etc/passwd > /opt/ls.txt 
[root@localhost ~]# cat /opt/ls.txt
[root@localhost ~]# echo 123456 >> /opt/ls.txt 
[root@localhost ~]# cat /opt/ls.txt 
[root@localhost ~]# echo hello > /opt/ls.txt
[root@localhost ~]# cat /opt/ls.txt 

补充:
查看日期时间

[root@localhost ~]# date #查看时间
[root@localhost ~]# date -s '2010-1-1 10:10:10' #修改时间

 管道
| 管道操作:将前面命令的输出结果,传递给后面命令当作后面命令的参数
请显示/etc/passwd 的 8-12 行内容

[root@localhost ~]# head -12 /etc/passwd | tail -5 
[root@localhost ~]# head -12 /etc/passwd | tail -5 | cat -n 
[root@localhost ~]# cat -n /etc/passwd | head -12 | tail -5
[root@localhost ~]# ifconfig | less 
[root@localhost ~]# ifconfig | head -2

计算器 bc

  • 加 - 减 * 乘 / 除

[root@localhost ~]# bc #交互式做数学运算

[root@localhost ~]# echo 3+4 | bc
[root@localhost ~]# echo 3*2 | bc
[root@localhost ~]# echo 3-4 | bc
[root@localhost ~]# echo 3/2 | bc

find 精确查找
查找文件
• 根据预设的条件递归查找对应的文件(proc 目录不占硬盘空间大小,在内存上存储数据)
– find [目录] [条件 1] [-a|-o] [条件 2] …
– 常用条件表示:
-type 类型(f 文件【黑】、d 目录【蓝】、l 快捷方式【青】)
-name “文档名称”
-size +|-文件大小(k、M、G)
-user 用户名
-mtime 修改时间 查找文件
-type 按照类型查找

[root@localhost ~]# find /dev/ -type l #查找快捷方式
[root@localhost ~]# find /boot/ -type f #查找是文本文件
[root@localhost ~]# find /boot/ -type d #查找是目录
[root@localhost ~]# find /root/ -type d 

-name ‘文档名称’

[root@localhost ~]# find /etc/ -name '*tab'
[root@localhost ~]# find /etc/ -name 'vm*'
[root@localhost ~]# find /etc/ -name 'passwd' #查找文件名 passwd
[root@localhost ~]# find /etc/ -name 'passwd*' 
[root@localhost ~]# find /etc/ -name '*passwd*'

-size +或-文件大小(k、M、G)

[root@localhost ~]# find /boot/ -size +10M #查找大于 10M 的
[root@localhost ~]# find /boot/ -size -10M #不支持-1M,-1G,-1k

-user 用户名 #按照所有者进行查找,所有者:文件的创建者

[root@localhost ~]# find /home/ -user tom
[root@localhost ~]# find / -user harry 

• 根据文件修改时间(所有的时间都是过去时间)
-mtime
+10:10 天之前的文档
-10:最近 10 天之内的文档

[root@localhost ~]# find /opt/ -mtime +10 
[root@localhost ~]# find /var/ -mtime -10

• 根据名称查找,忽略大小写
– -iname

[root@localhost ~]# find /etc -name 'PASSWD'
[root@localhost ~]# find /etc -iname 'PASSWD'

find 高级使用
与 WC 连用

[root@localhost ~]# wc /etc/passwd
 43 84 2237 /etc/passwd 
 [root@localhost ~]# wc -l /etc/passwd #查看这个文件有多少行 
[root@localhost ~]# find /etc/ -name '*tab'
[root@localhost ~]# find /etc/ -name '*tab' | wc -l
[root@localhost ~]# find /etc/ -name '*.conf' 
[root@localhost ~]# find /etc/ -name '*.conf' | wc -l
[root@localhost ~]# mkdir /root/nsd01
[root@localhost ~]# touch /root/nsd02.txt
[root@localhost ~]# touch /root/nsd03.txt
[root@localhost ~]# find /root/ -name 'nsd*'

-a 并且(都满足,默认是-a) -o 或者(满足其一即可)

[root@localhost ~]# find /root/ -name "nsd*" -a -type f
[root@localhost ~]# find /root/ -name "nsd*" -a -type d
[root@localhost ~]# find /root/ -name "nsd*" -o -type d
[root@localhost ~]# find /root/ -name "nsd*" -type d

2、查找并处理文件
• 使用 find 命令的 -exec 操作
– find 【目录】 条件 -exec 处理命令 {} ;(需要将查找的结果交给后面的命令处理时,红
色部分是固定格式)
– 优势:以 {} 代替每一个结果,逐个处理,遇 ; 结束
find [目录] [条件 1] [-a|-o] [条件 2]

[root@localhost ~]# rm -rf /opt/*
[root@localhost ~]# ls /opt/
[root@localhost ~]# find /boot/ -name 'vm*' #查找/boot/目录中以 VM 所有开头的文件
[root@localhost ~]# find /boot/ -name 'vm*' -exec cp {} /opt \; 
#将/boot/目录下以 vm 开头的所有文件拷贝到/opt/目录
[root@localhost ~]# ls /opt/

把/boot 目录下面大于 10M 的文件拷贝到/opt 下

[root@localhost ~]# find /boot/ -size +10M -type f -exec cp {} /opt \;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值