which-whereis-locate-grep find 命令使用
查找文件一般有以下几个命令:
| which | 查看可执行文件的位置 |
|--|--|
| whereis | 查看可执行文件的位置及相关文件 |
| locate | 配合数据库缓存,快速查看文件位置 |
|grep|过滤匹配,它是一个文件搜索工具|
| find| 查找相关文件 |
举例:
[root@xuegod63 ~]# which cd /usr/bin/cd [root@xuegod63 ~]# whereis cd cd: /usr/bin/cd /usr/share/man/man1/cd.1.gz /usr/share/man/man1p/cd.1p.gz [root@xuegod63 ~]# whereis ls ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.g locate
locate 命令和 find -name 功能差不多,是它的另外一种写法,但是这个要比 find 搜索快的多,因
为 find 命令查找的是具体目录文件,而 locate 它搜索的是一个数据库
/var/lib/mlocate/mlocate.db,这个数据库中存有本地所有的文件信息;这个数据库是 Linux 自动创
建并每天自动更新维护。相关的配置信息在 /etc/updatedb.conf,查看定时任务信息在
/etc/cron.daily/mlocate
[root@xuegod63 ~]# yum -y install mlocate [root@xuegod63 mnt]# touch /opt/xuegod.txt [root@xuegod63 mnt]# locate xuegod.txt #发现找不到 [root@xuegod63 mnt]# updatedb #如果对当天文件查找,需要手动更新数据库 updatedb [root@xuegod63 mnt]# locate xuegod
grep 查找使用
作用:过滤,它能够使用正则表达式来搜索文本,并把结果打印出来
参数:
| -v | 取反 |
|--|--|
| -i | 忽略大小写 |
| ^# | 以#开头 |
|# | 空行 |
| -n | 对过滤的内容加上行号 |
| l | 或者的意思 |
[root@xuegod63 ~]# ps -aux | grep sshd | grep -v grep root 1089 0.0 0.2 105996 4088 ? Ss 20:19 0:00 /usr/sbin/sshd –D [root@xuegod63 ~]# cat /etc/passwd | grep ^a #以 a 开头 [root@xuegod63 ~]# grep bash$ /etc/passwd #以 bash 结尾 [root@xuegod63 ~]# grep "nologin\|root" /etc/passwd | wc -l 注: \ 表示转义符 [root@xuegod63 ~]# egrep "nologin|root" /etc/passwd | wc -l #查看包括 nologin 或 root 的行 注:egrep 是 grep 加强版本
find 命令使用
格式:find pathname -options [-print] 命令字 路径名称 选项 输出
参数:
pathname: find 命令所查找的目录路径,不输入代表当前目录例如用 . 来表示当前目录,用 /
来表示系统根目录。
find 命令选项:
| -name | 按照文件名查找文件‘名称’|
|--|--|
| -perm | 按照文件权限来查找文件。666 777 等 |
| -user | 按照文件属主来查找文件 |
|-group|按照文件所属的组来查找文件|
| -mtime | -n/ +n 按照文件的更改时间来查找文件 |
| - n | 表示文件更改时间距现在 n 天以内 |
| + n | 表示文件更改时间距现在 n 天以前 |
|-type|查找某一类型的文件|
| b - | 块设备文件 |
| d- | 目录 |
|c -|字符设备文件|
| p - | 管道文件 |
| l- | 符号链接文件 |
| f - | 普通文件 |
|-size n|查找符合指定的文件大小的文件|
| -exec |对匹配的文件执行该参数所给出的其他 linux 命令, 相应命令的形式为' 命令 {} \ 注意{ }和 \;之间的空格,{}代表查到的内容|
示例 1:希望在 root 目录下查找更改时间在 1 天以内,被修改的文件
[root@xuegod63 ~]# find /root/ -mtime -1
对查找内容执行相应命令
-exec 这个选项参数后面可以跟自定义的 SHELL 命令,格式如下:
例 2:
[root@xuegod63 ~]# touch {1,2,3}.back [root@xuegod63 mnt]# find . -name "*.back" -exec ls -l {} \;
例 3:
[root@xuegod63 ~]# find . -name "*.back" -exec mv {} /opt \; [root@xuegod63 ~]# ls /opt/ 1.back 2.back 3.back rh xuegod.txt
例 4:把查找到的文件复制到一个指定的目录
[root@xuegod63 mnt]# find /root -name "*.txt" -exec cp {} /opt\;
例 5:xargs 和 find 命令结合 复制文件
-i 表示 find 传递给 xargs 的结果 由{}来代替 (了 解) [root@xuegod63 ~]# rm -rf /opt/* [root@xuegod63 ~]# find . -name "*.txt" | xargs -i cp {} [root@xuegod63 ~]# ls /opt/
例 6:查找多个类型文件
比较符的使用:
| -a | and 并且|
|--|--|
| -o | or或者 |
| + | 超过 |
|-|低于|
[root@xuegod63 ~]# touch a.pdf back.sh [root@xuegod63 ~]# find . -name "*.sh" -o -name "*.pdf" [root@xuegod63 ~]# find /etc -size +20k -a -size -50k | wc -l 22 [root@xuegod63 ~]# find /etc -size +20k | wc -l 49
例 7: 按权限查找:-perm
[root@xuegod63 ~]# find /bin/ -perm 755 #等于 0755 权限的文件或目录 [root@xuegod63 ~]# find /bin/ -perm -644 # -表示至少,至少有 644 权限的文件或目录
例:查看系统中权限至少为 777 的文件或目录
创建一些测试文件:
[root@xuegod63 ~]# mkdir ccc [root@xuegod63 ~]# chmod 777 ccc [root@xuegod63 ~]# mkdir test [root@xuegod63 ~]# chmod 1777 test [root@xuegod63 ~]# touch b.sh [root@xuegod63 ~]# chmod 4777 b.sh
查找:
[root@xuegod63 ~]# find /root/ -perm 777
[root@xuegod63 ~]# find /root/ -perm 1777
[root@xuegod63 ~]# find /root/ -perm
4777
例:把系统中权限不低于 777 的危险目录查找出来
[root@xuegod63 ~]# find /root/ -perm-777 #至少有 777 权限
例:把系统中权限不低于 777 的危险文件查找出来
[root@xuegod63 ~]# find / -type f -perm-777
例 8:查找的目录深度:
-maxdepth 1 #只查找目录第一层的文件和目录
如:查找/bin 目录下权限等于 644 的文件
[root@xuegod63 ~]# find /etc/ -maxdepth 1 -perm 644 | more [root@xuegod63 ~]# find /bin/ -maxdepth 1 -perm 755 #/bin 后面要有/ [root@xuegod63 ~]# find /bin -maxdepth 1 -perm 755 #没加/无法满足我们的需求
例 9:查找系统中所有属于用户 user1 的文件,并把这个文件,放到/root/findresults 目录下
注意:/root/findresults 这个需要提前创建好。
root@xuegod63 ~]# mkdir /root/findresults [root@xuegod63 ~]# useradd user1 [root@xuegod63 ~]# find / -user user1 -exec cp -a {} /root/findresults/
参数: -a #复制时,保留原来文件的所有属性
报错:
find: ‘/proc/43475/task/43475/fd/6’: 没有那个文件或目录 find: ‘/proc/43475/task/43475/fdinfo/6’: 没有那个文件或目录 find: ‘/proc/43475/fd/6’: 没有那个文件或目录 find: ‘/proc/43475/fdinfo/6’: 没有那个文件或目录 cp: 无法以目录"/home/user1" 来覆盖非目录"/root/findresults/user1"
同一个目录下,可以创建文件 user1 和文件夹 user1 吗?同一个目录下创建的文件名和目录
名一样吗? 不可以
[root@xuegod63 ~]# touch abc [root@xuegod63 ~]# mkdir abc mkdir: 无法创建目录"abc": 文件已存在
解决:
[root@xuegod63 ~]# find / -user user1 #发现 [root@xuegod63 ~]# ll /var/spool/mail/user1 #查看这个文件 [root@xuegod63 ~]# ll /home/user1 发现/var/spool/mail/user1 和/home/user1 的名字是一样的。 而两者都要复制到 /root/findresults/下,先复制了/var/spool/mail/user1,所以/home/user1 就不能复制了。 [root@xuegod63 ~]# mv /var/spool/mail/user1 [root@xuegod63 ~]# rm -rf /root/findresults/ * /var/spool/mail/user1.mail [root@xuegod63 ~]# find / -user mk -exec cp -a {} /root/findresults/ \; [root@xuegod63 ~]# mv /var/spool/mail/user1.mail /var/spool/mail/user1 #再修改过来
文章到此结束 想要了解更多的技术+