文件查找方法
1.which
查看可執(zhí)行文件的位置
[root@JhouShuai ~]# which rpm
/usr/bin/rpm
[root@JhouShuai ~]# which ls
alias ls='ls --color=auto'
/usr/bin/ls
[root@JhouShuai ~]# which sh
/usr/bin/sh
[root@JhouShuai ~]# which useradd
/usr/sbin/useradd
[root@JhouShuai ~]#
2.whereis
查看可執(zhí)行文件的位置 及相關(guān)文件
[root@JhouShuai ~]# whereis useradd
useradd: /usr/sbin/useradd /usr/share/man/man8/useradd.8.gz
[root@JhouShuai ~]# whereis rpm
rpm: /usr/bin/rpm /usr/lib/rpm /etc/rpm /usr/share/man/man8/rpm.8.gz
[root@JhouShuai ~]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
[root@JhouShuai ~]#
3.grep
過濾
語法:grep 關(guān)鍵字 文件
vim a.txt #輸入以下內(nèi)容
kkkkkkkkkkkkk
ddddddddddddd
cccccccccccc
dddddddfsdfsdf
aaaaaaaaaaa
dddfffmmmm
dfsdf
e
[root@JhouShuai ~]# grep f a.txt
dddddddfsdfsdf
dddfffmmmm
dfsdf
[root@JhouShuai ~]#
[root@JhouShuai ~]# cat a.txt | grep -v f #-v 反轉(zhuǎn) 排除f的所有行。
kkkkkkkkkkkkk
ddddddddddddd
cccccccccccc
aaaaaaaaaaa
e
[root@JhouShuai ~]#
[root@JhouShuai ~]# cat a.txt | grep ^a # 以a開頭
aaaaaaaaaaa
[root@JhouShuai ~]#
[root@JhouShuai ~]# cat a.txt | grep f$ #以f結(jié)尾
dddddddfsdfsdf
dfsdf
[root@JhouShuai ~]#
[root@JhouShuai ~]# cat a.txt | grep ^$ # ^$ 空行
[root@JhouShuai ~]#
find
find命令是在目錄結(jié)構(gòu)中搜索文件,并執(zhí)行指定的操作。
find命令提供了相當(dāng)多的查找條件,功能很強大。
1、find命令的形式;
find pathname -options [-print]
2、find命令的參數(shù);
pathname: find命令所查找的目錄路徑。例如用.來表示當(dāng)前目錄,用/來表示系統(tǒng)根目錄。
3、find命令選項
-name 按照文件名查找文件。
-perm 按照文件權(quán)限來查找文件。
-user 按照文件屬主來查找文件。
-mtime -n +n 按照文件的更改時間來查找文件,
- n表示文件更改時間距現(xiàn)在n天以內(nèi),
+ n表示文件更改時間距現(xiàn)在n天以前。
-size按照文件的大小來查找文件
-type 查找某一類型的文件,
b - 塊設(shè)備文件。
d - 目錄。
c - 字符設(shè)備文件。
p - 管道文件。
l- 符號鏈接文件。
f - 普通文件。
使用name選項\
文件名選項是find命令最常用的選項,
要么單獨使用該選項,要么和其他選項一起使用。
[root@JhouShuai ~]# find ~ -name "*.txt"
/root/.cache/tracker/db-version.txt
/root/.cache/tracker/db-locale.txt
/root/.cache/tracker/parser-sha1.txt
/root/.cache/tracker/locale-for-miner-user-guides.txt
/root/.cache/tracker/locale-for-miner-apps.txt
/root/.cache/tracker/last-crawl.txt
/root/.cache/tracker/first-index.txt
/root/a.txt
[root@JhouShuai ~]# find ./ -name "*.txt"
./.cache/tracker/db-version.txt
./.cache/tracker/db-locale.txt
./.cache/tracker/parser-sha1.txt
./.cache/tracker/locale-for-miner-user-guides.txt
./.cache/tracker/locale-for-miner-apps.txt
./.cache/tracker/last-crawl.txt
./.cache/tracker/first-index.txt
./a.txt
[root@JhouShuai ~]# find /etc/ -name "host*" -print
/etc/host.conf
/etc/hosts
/etc/hosts.allow
/etc/hosts.deny
/etc/selinux/targeted/active/modules/100/hostname
/etc/hostname
/etc/avahi/hosts
[root@JhouShuai ~]#
用perm選項
按照文件權(quán)限模式用-perm選項,
按文件權(quán)限模式來查找文件的話。最好使用八進制的權(quán)限表示法。
[root@JhouShuai ~]# find ./ -perm 755
./.cache/evolution/addressbook
./.cache/evolution/addressbook/trash
./.cache/evolution/calendar
./.cache/evolution/calendar/trash
./.cache/evolution/mail
使用user選項
按文件屬主查找文件
[root@JhouShuai ~]# find /home/zhoushuai/ -user zhoushuai
/home/zhoushuai/
/home/zhoushuai/.mozilla
/home/zhoushuai/.mozilla/extensions
/home/zhoushuai/.mozilla/plugins
/home/zhoushuai/.bash_logout
/home/zhoushuai/.bash_profile
/home/zhoushuai/.bashrc
/home/zhoushuai/.cache
/home/zhoushuai/.cache/abrt
/home/zhoushuai/.cache/abrt/lastnotification
/home/zhoushuai/.config
/home/zhoushuai/.config/abrt
/home/zhoushuai/.bash_history
[root@JhouShuai ~]#
按照更改時間查找文件
如果希望按照更改時間來查找文件,可以
使用mtime,atime或ctime選項。
如果系統(tǒng)突然沒有可用空間了,很有可能某一個文件的大小在此期間增長迅速,這時就可以用mtime選項來查找這樣的文件。
用減號-來限定更改時間在距今n日以內(nèi)的文件,
用加號+來限定更改時間在距今n日以前的文件。
n代表正好那個時間
[root@JhouShuai ~]# find /root -mtime -5
/root
/root/.cache/imsettings
/root/.cache/imsettings/log.bak
/root/.cache/imsettings/log
/root/.cache/tracker
/root/.cache/tracker/meta.db
[root@JhouShuai ~]# find / -mtime +3
使用type選項
-type 查找某一類型的文件,
在/etc目錄下查找所有的目錄,可以用:
find /etc -type d
find /etc -type f