文件查找方法
1.which
查看可执行文件的位置
[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
查看可执行文件的位置 及相关文件
[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 关键字 文件
vim a.txt #输入以下内容
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 反转 排除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结尾
dddddddfsdfsdf
dfsdf
[root@JhouShuai ~]#
[root@JhouShuai ~]# cat a.txt | grep ^$ # ^$ 空行
[root@JhouShuai ~]#
find
find命令是在目录结构中搜索文件,并执行指定的操作。
find命令提供了相当多的查找条件,功能很强大。
1、find命令的形式;
find pathname -options [-print]
2、find命令的参数;
pathname: find命令所查找的目录路径。例如用.来表示当前目录,用/来表示系统根目录。
3、find命令选项
-name 按照文件名查找文件。
-perm 按照文件权限来查找文件。
-user 按照文件属主来查找文件。
-mtime -n +n 按照文件的更改时间来查找文件,
- n表示文件更改时间距现在n天以内,
+ n表示文件更改时间距现在n天以前。
-size按照文件的大小来查找文件
-type 查找某一类型的文件,
b - 块设备文件。
d - 目录。
c - 字符设备文件。
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选项
按照文件权限模式用-perm选项,
按文件权限模式来查找文件的话。最好使用八进制的权限表示法。
[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选项。
如果系统突然没有可用空间了,很有可能某一个文件的大小在此期间增长迅速,这时就可以用mtime选项来查找这样的文件。
用减号-来限定更改时间在距今n日以内的文件,
用加号+来限定更改时间在距今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
网友评论