find查找文件和grep用法

• 根據預設的條件遞歸查找對應的文件
– find [目錄] [條件1] [-a|-o] [條件2] …
– 常用條件表示:
-type 類型(f、d、l)
-name “文檔名稱”
-size +|-文件大小(k、M、G)
-user 用戶名
-mtime 根據文件修改時間
################################################

-type 類型(f文件、d目錄、l快捷方式)
-name '文檔名稱'

[root@server0 ~]# find /boot/ -type d
[root@server0 ~]# find /boot/ -type f
[root@server0 ~]# find /boot/ -type l
[root@server0 ~]# ls /boot/grub/menu.lst
[root@server0 ~]# ls -l /boot/grub/menu.lst

[root@server0 ~]# find /etc/ -name ‘passwd’
[root@server0 ~]# find /etc/ -name ‘tab’
[root@server0 ~]# find /etc/ -name ‘tab
[root@server0 ~]# find /etc/ -name '
.conf’

[root@server0 ~]# mkdir /root/nsd01
[root@server0 ~]# mkdir /root/nsd02
[root@server0 ~]# touch /root/nsd03.txt
[root@server0 ~]# find /root/ -name ‘nsd*’

]# find /root/ -name ‘nsd*’ -a -type f

]# find /root/ -name ‘nsd*’ -type f
]# find /root/ -name ‘nsd*’ -type d

################################################
-size +或-文件大小(k、M、G)
-user 用戶名 #按照數據的所有者進行查找

[root@server0 ~]# find /boot/ -size +10M #大於10M的
[root@server0 ~]# find /boot/ -size -10M
[root@server0 ~]# find /boot/ -size +300k

/proc:不佔用磁盤空間,佔用內存空間

[root@server0 ~]# find / -user student
[root@server0 ~]# find /home -user student

#################################################
-mtime 根據文件修改時間
-mtime +10 #10天之前的數據
-mtime -10 #最近10天之內的數據

[root@server0 ~]# find /root/ -mtime +1000
[root@server0 ~]# find /root/ -mtime +10
[root@server0 ~]# find /root/ -mtime -2

三個月以前:
[root@server0 ~]# find /root/ -mtime +90

#################################################

find擴展使用
• 使用find命令的 -exec 操作
– find … … -exec 處理命令 {} ;
– 優勢:以 {} 代替每一個結果,逐個處理,遇 ; 結束

]# find /boot/ -size +10M
]# find /boot/ -size +10M -exec cp {} /opt ;
]# ls /opt/

]# find /root/ -name ‘nsd*’
]# find /root/ -name ‘nsd*’ -exec rm -rf {} ;
]# find /root/ -name ‘nsd*’

案例4:查找並處理文件
• 使用find命令完成以下任務
– 找出所有用戶 student 擁有的文件
– 把它們拷貝到 /root/findfiles/ 文件夾中

[root@server0 ~]# mkdir /root/findfiles
[root@server0 ~]# find / -user student -type f

]# find / -user student -type f -exec cp {} /root/findfiles/ ;

]# ls -A /root/findfiles/

################################################
grep用法
顯示文件的有效信息(去除註釋行,去除空行)
]# grep -v ^# /etc/login.defs
]# grep -v ^# /etc/login.defs | grep -v ^$

]# grep -v ^# /etc/login.defs | grep -v ^$ > /opt/1.txt
]# less /opt/1.txt

在Linux大多數配置文件中,以#開頭的行爲註釋行

^$ : 匹配空行
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章