linux我ll查不到usr,Linux学习-文件查寻

Linux学习---文件查找

grep, egrep, fgrep  :文本查找

文件查找

locate

全系统查找,非实时,模糊匹配。查找时根据全系统文件数据库进行的。

系统在每天的计划任务时间生成数据库。

updatedb  手动生成文件数据库。

速度快。

find

实时查找,精确。速度慢。

遍历指定目录中所有文件完成查找。

支持多种查找标准。

find   PATH   查找标准   找到后的处理动作

路径默认:表示当前目录

标准默认:指定路径下所有文件

动作默认:为打印到屏幕上

查找标准:

-name: 根据文件名精确查找,做精确匹配。严格区分大小写

支持文件名通配:

*     ?    []

-iname :  文件名匹配不区分大小写。

[root@localhost shell_example]# find /etc -name passwd

/etc/passwd

/etc/pam.d/passwd

[root@localhost shell_example]# find /etc -name 'passwd*'

/etc/passwd

/etc/passwd-

/etc/pam.d/passwd

[root@localhost shell_example]# find /etc -name '*passwd'

/etc/passwd

/etc/security/opasswd

/etc/pam.d/passwd

-regex  PATTERN  :基于正则表达式进行文件查找。

-user   USERNAME: 根据文件的属主来查找

-group GROUPNAME: 根据属组查找

一旦某个用户被删除,那么此前属主是这个用户的文件的属主变成这个用户此前的UID。

-uid: 根据UID查找

-gid: 根据GID查找

-nonuser :查找没有属主的文件。

-nogroup:查找没有属组的文件。

-type : 根据文件类型

f:普通文件

d:目录文件

c: 字符设备

b:块文件

l  :链接文件,符号链接

p :管道文件

s :套接字文件

-size :根据文件大小。默认单位字节。

[+/-]#k

#M

#G

找 10M 的文件,所有9.xM 的文件都认为是10M。

组合条件:

-a:  and

-o:  or

-not: not

默认是 and 操作。

[root@localhost test]# find ./ -not -user user1 -ls

479024 0 drwxr-xr-x 2 root root 38 7月 18 21:43 ./

3670696 0 -rw-r--r-- 1 user2 root 0 7月 18 21:43 ./b

3692673 0 -rw-r--r-- 1 root root 0 7月 18 21:43 ./c

3692674 0 -rw-r--r-- 1 root root 0 7月 18 21:43 ./d

[root@localhost test]# find ./ -not -user user1 -a -not -user user2 -ls

479024 0 drwxr-xr-x 2 root root 38 7月 18 21:43 ./

3692673 0 -rw-r--r-- 1 root root 0 7月 18 21:43 ./c

3692674 0 -rw-r--r-- 1 root root 0 7月 18 21:43 ./d

[root@localhost test]# find ./ -not \( -user user1 -a -type d \)

./

./a

./b

./c

./d

./hi

[root@localhost test]# ll

总用量 0

-rw-r--r--. 1 user1 root 0 7月 18 21:43 a

-rw-r--r--. 1 user2 root 0 7月 18 21:43 b

-rw-r--r--. 1 root root 0 7月 18 21:43 c

-rw-r--r--. 1 root root 0 7月 18 21:43 d

drwxr-xr-x. 2 user1 root 6 7月 18 21:44 hello

drwxr-xr-x. 2 root root 6 7月 18 21:45 hi

-mtime:修改时间

-ctime: 改变时间

-atime: 访问时间

#     #天前

+#    至少#天没有

-#     #天之内

[root@localhost test]# find /tmp -atime +7

/tmp/.X11-unix/X0

/tmp/.ICE-unix/3504

/tmp/.esd-1000/socket

/tmp/a.hadoop

/tmp/ssh-DqYXtl0A4ffy/agent.3504

/tmp/swap.txt

/tmp/sed.txt

/tmp/blandline.txt

/tmp/bash.txt

/tmp/nologin.txt

/tmp/.X0-lock

/tmp/set.txt

[root@localhost test]# find /tmp -atime +30

/tmp/.X11-unix/X0

/tmp/.ICE-unix/3504

/tmp/.esd-1000/socket

/tmp/a.hadoop

/tmp/ssh-DqYXtl0A4ffy/agent.3504

/tmp/.X0-lock

分钟:

-mmin

-cmin

-amin

[root@localhost test]# find ./ -amin -5

[root@localhost test]# touch -a a

[root@localhost test]# find ./ -amin -5

./a

-perm MODE  : 755 644  精确匹配权限来查找。

/MODE: 任意一位权限匹配即满足条件。  或关系。

-MODE: 文件权限能完全包含此MODE时才显示。  与关系。

[root@localhost test]# find ./ -perm 644 -ls

2121677 0 -rw-r--r-- 1 user1 root 0 7月 18 21:43 ./a

3670696 0 -rw-r--r-- 1 user2 root 0 7月 18 21:43 ./b

3692673 0 -rw-r--r-- 1 root root 0 7月 18 21:43 ./c

3692674 0 -rw-r--r-- 1 root root 0 7月 18 21:43 ./d

[root@localhost test]# chmod o-r a

[root@localhost test]# ll

总用量 0

-rw-r-----. 1 user1 root 0 7月 18 21:43 a

-rw-r--r--. 1 user2 root 0 7月 18 21:43 b

-rw-r--r--. 1 root root 0 7月 18 21:43 c

-rw-r--r--. 1 root root 0 7月 18 21:43 d

drwxr-xr-x. 2 user1 root 6 7月 18 21:44 hello

drwxr-xr-x. 2 root root 6 7月 18 21:45 hi

[root@localhost test]# find ./ -perm 644 -ls

3670696 0 -rw-r--r-- 1 user2 root 0 7月 18 21:43 ./b

3692673 0 -rw-r--r-- 1 root root 0 7月 18 21:43 ./c

3692674 0 -rw-r--r-- 1 root root 0 7月 18 21:43 ./d

[root@localhost test]# man chmod

[root@localhost test]# find ./ -perm /644

./

./a

./b

./c

./d

./hello

./hi

[root@localhost test]# find ./ -perm /640

./

./a

./b

./c

./d

./hello

./hi

[root@localhost test]# chmod 006 b

[root@localhost test]# find ./ -perm /640

./

./a

./c

./d

./hello

./hi

执行动作:

默认 -print  :打印

-ls :类似 ls -l 的形式显示每个文件的详细信息。

-ok  COMMAND  {}  \;  反斜线分号结束。 每个操作需要用户确认。

-exec  COMMAND {}  \;   无需确认,直接执行。

[root@localhost test]# ll

总用量 0

-rw-r-----. 1 user1 root 0 7月 18 21:43 a

-------rw-. 1 user2 root 0 7月 18 21:43 b

-rw-r--r--. 1 root root 0 7月 18 21:43 c

-rw-r--r--. 1 root root 0 7月 18 21:43 d

drwxr-xr-x. 2 user1 root 6 7月 18 21:44 hello

drwxr-xr-x. 2 root root 6 7月 18 21:45 hi

[root@localhost test]# find ./ -perm -006

./b

[root@localhost test]# find ./ -perm -006 -exec chmod o-rw {} \;

[root@localhost test]# ll

总用量 0

-rw-r-----. 1 user1 root 0 7月 18 21:43 a

----------. 1 user2 root 0 7月 18 21:43 b

-rw-r--r--. 1 root root 0 7月 18 21:43 c

-rw-r--r--. 1 root root 0 7月 18 21:43 d

drwxr-xr-x. 2 user1 root 6 7月 18 21:44 hello

drwxr-xr-x. 2 root root 6 7月 18 21:45 hi

[root@localhost test]# find ./ -type d -ok chmod +x {} \;

< chmod ... ./ > ?

< chmod ... ./hello > ?

< chmod ... ./hi > ?

[root@localhost test]# find ./ -perm -060 -exec mv {} {}.new \;

[root@localhost test]# ll

总用量 0

-rw-rw-r--. 1 root root 0 7月 18 21:43 1.new.new

-rw-r-----. 1 user1 root 0 7月 18 21:43 a

-rw-rw-rw-. 1 root root 0 7月 18 22:24 b.new

-rw-r--r--. 1 root root 0 7月 18 21:43 c

-rw-rw-rw-. 1 root root 0 7月 18 22:24 d.new

drwxr-xr-x. 2 user1 root 6 7月 18 21:44 hello

drwxr-xr-x. 2 root root 6 7月 18 21:45 hi

找到所有.sh 结尾的并且所有用户都有执行权限的文件,将其他用户的执行权限去掉。

[root@localhost sh]# find ./ -name "*.sh" -perm -111 -exec chmod o-x {} \;

[root@localhost sh]# ll

总用量 112

-rwxr-xr--. 1 root root 153 7月 18 22:28 add.sh

-rwxr-xr--. 1 root root 647 7月 18 22:28 adduser.sh

-rwxr-xr--. 1 root root 303 7月 18 22:28 bash2.sh

-rwxr-xr--. 1 root root 209 7月 18 22:28 bash.sh

-rwxr-xr--. 1 root root 57 7月 18 22:28 calc.sh

-rwxr-xr--. 1 root root 267 7月 18 22:28 cpu.sh

-rwxr-xr--. 1 root root 340 7月 18 22:28 expired.sh

-rwxr-xr--. 1 root root 232 7月 18 22:28 file.sh

-rwxr-xr--. 1 root root 194 7月 18 22:28 filetest1.sh

-rwxr-xr--. 1 root root 227 7月 18 22:28 filetest.sh

-rwxr-xr--. 1 root root 148 7月 18 22:28 history.sh

-rwxr-xr--. 1 root root 296 7月 18 22:28 lees4.sh

-rwxr-xr--. 1 root root 155 7月 18 22:28 less1.sh

-rwxr-xr--. 1 root root 70 7月 18 22:28 less2.sh

-rwxr-xr--. 1 root root 394 7月 18 22:28 less3_1.sh

-rwxr-xr--. 1 root root 201 7月 18 22:28 less3_2.sh

-rwxr-xr--. 1 root root 206 7月 18 22:28 less3.sh

-rwxr-xr--. 1 root root 254 7月 18 22:28 sayhi.sh

-rwxr-xr--. 1 root root 66 7月 18 22:28 shift.sh

-rwxr-xr--. 1 root root 1209 7月 18 22:28 statisticsshell.sh

-rwxr-xr--. 1 root root 262 7月 18 22:28 string.sh

-rwxr-xr--. 1 root root 249 7月 18 22:28 sum2.sh

-rwxr-xr--. 1 root root 188 7月 18 22:28 sum3.sh

-rwxr-xr--. 1 root root 85 7月 18 22:28 sum.sh

-rwxr-xr--. 1 root root 49 7月 18 22:28 test1.sh

-rwxr-xr--. 1 root root 116 7月 18 22:28 than.sh

-rwxr-xr--. 1 root root 366 7月 18 22:28 usermanage.sh

-rwxr-xr--. 1 root root 167 7月 18 22:28 utest.sh

练习:

1、找到/var下属主root  属组mail的所有文件

[root@localhost sh]# find /var -user root -group mail -ls

100664298 4 drwxrwxr-x 2 root mail 4096 7月 18 21:40 /var/spool/mail

103419761 196 -rw------- 1 root mail 198458 6月 12 23:23 /var/spool/mail/root

2、找到/usr下不属于root、bin或student的文件

[root@localhost sh]# find /usr -not \( -user root -o -user bin -o -user beny \) -ls

68744954 4 drwx------ 2 polkitd root 4096 4月 25 22:10 /usr/share/polkit-1/rules.d

34704584 16 -rwsr-sr-x 1 abrt abrt 15336 12月 1 2015 /usr/libexec/abrt-action-install-debuginfo-to-abrt-cache

3、找/etc下最近一周内内容修改且不属于root及student用户的文件

[root@localhost sh]# find /etc -mtime -7 -not -user root -ls

4、找当前系统没有属主或属组且最近1天内访问过,并将属主、属组改为root

[root@localhost sh]# find / -nouser -nogroup -exec chown root:root {} \;

5、找/etc下大于1M的文件并写入/tmp/etc.largefiles

[root@localhost sh]# find /etc/ -size +1M -exec cp {} /tmp/etc.largefiles \;

[root@localhost sh]# ls -lh /tmp/etc.largefiles

-r--r--r--. 1 root root 1.4M 7月 18 22:55 /tmp/etc.largefiles

6、找/etc下所有用户都没有写权限的文件,显示详细信息。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值