前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Linux基本操作实验(3)

Linux基本操作实验(3)

作者头像
py3study
发布2020-01-14 16:03:18
1.3K0
发布2020-01-14 16:03:18
举报
文章被收录于专栏:python3

Linux操作系统拥有非常多的文件,每种文件都代表一些特定的含义。如何快速的定位到你想要的文件,是一个非常复杂的过程。GNU提供了一个非常有效的工具findutil,(http://www.gnu.org/software/findutils/manual/html_mono/find.html#Run-Commands)findutil工具包中提供了如下一些工具:find, locate, updatedb,xargs.  其中find和locate经常用到。 find和xargs结合使用,可以根据相关的参数选项查找出你想要的文件并且对这些文件进行处理。locate和updatedb是结合使用。这里主要讲述find和xargs。使用find的命令比较简单,但是如何用好find还是首先需要了解find预设定的规则。这些规则来源于实际经验的总结,并被总结成find的选项,学好这些规则基本上就领悟了find的精粹。这些规则如下:

1)根据名称中含有特定内容和模式,如name后缀是.c就表示C源码程序。

2)连接到指定文件的连接文件。

3)在一定时间内更新过或者访问过

4)大小在一定时范围

5)指定的类型

6)指定的文件OWNER和GROUP

7)拥有一定的访问权限或者特殊的mode bits

8)包含含有一定模式的内容

9)文件目录下一定深度及以上所有。

GNU作为大牛们的作品,它除了你想到的功能之外,还给你带来意外,就是你通过find查出了一堆文件,你还要进行处理啊?你不能对着屏幕一个一个处理,所以GNU中find还有一些对应的ACTION,通过这些ACTION你就一步到位直接处理这些文件了。find 用来查找文件,其命令基本格式:

find [文件路径] [文件规则表达式]  如find /usr/src –name “*.c”  -size +100k –print。 其中文件规则表达式是由四部分组成option tests actions operators 。 缺省actions是-print。 其中option通常放在前面。

常用的tests如下:

【1】NAME,根据名称 iname name分别表示是否忽略大小写。name后的文件名称,使用SHELL通配符模式。 还有一种方式,使用-path(ipath)和-wholename(iwholename)来指定文件全路径名,总觉得后面这种方式很奇怪,比喻说你都知道全路径名,你还要find什么呢?但这个通常结合-prun来排除路径,比较有用。当然实际中,它其实跟name比较相似。

find /usr/src "*.c" |more

[root@windriver-machine shtest]# find ~ -name "*.txt" -print /root/.subversion/README.txt [root@windriver-machine shtest]# find . -name "*.txt" -print ./123abc/123abc.txt [root@windriver-machine shtest]# find /etc -name "host*" -print /etc/avahi/hosts /etc/host.conf /etc/hosts.allow /etc/hosts.deny /etc/hosts [root@windriver-machine shtest]# find . -name "[a-z][a-z][0-9][0-9].txt" –print

[root@windriver-machine shtest]# find . -path '*.txt' ./123abc/123abc.txt

【2】File mode bits 根据文件权限有四种,-readable –writable –executable –perm,其中前三种不是POXIS标准,最后一种perm 后面跟数字前还有两个符号”-“和“/”号。其中/是GNU扩展。区别这两个符号的作用很简单,就是缺省是精确匹配。加了-表示模糊匹配,就是不管其它位是什么。 ”/”这个表示任意一个匹配就OK。

[root@windriver-machine shtest]# find . -perm -007 -print ./1234 [root@windriver-machine shtest]# ll 1234 lrwxrwxrwx 1 root root 8 2011-08-06 22:07 1234 -> 1234.txt [root@windriver-machine shtest]# find .  -perm -664 -print . ./output ./ex1.sh ./newerr ./errors ./1234 [root@windriver-machine shtest]# find . -perm 664 -print ./output ./newerr ./errors [root@windriver-machine shtest]# ll total 20 lrwxrwxrwx 1 root      root         8 2011-08-06 22:07 1234 -> 1234.txt drwsr-sr-x 2 windriver windriver 4096 2011-08-06 21:24 123abc -rw-rw-r-- 2 windriver windriver   74 2011-08-06 22:11 errors -rwxrwxr-x 1 windriver windriver  120 2011-08-06 01:44 ex1.sh -rw-rw-r-- 2 windriver windriver   74 2011-08-06 22:11 newerr -rw-rw-r-- 1 windriver windriver 1640 2011-08-06 19:13 output [root@windriver-machine shtest]# find -perm /222 -print . ./output ./ex1.sh ./newerr ./errors ./123abc ./123abc/123abc.txt ./1234 [root@windriver-machine shtest]# find . -perm /g+w,o=w -print . ./output ./ex1.sh ./newerr ./errors ./1234 [root@windriver-machine shtest]#

【3】Directories  目录,目录可以搜索深度 –maxdepth  -mindepth -depth还可以通过-prune将一些特定目录排除出去。prune前面一定要用path和wholename指定路径。如果 depth起作用,则prune不起用.

[root@windriver-machine shtest]# find . -wholename './123abc' -prune , -print . ./output ./ex1.sh ./newerr ./errors ./123abc ./1234 [root@windriver-machine shtest]# find . -wholename './123abc'  , -print . ./output ./ex1.sh ./newerr ./errors ./123abc ./123abc/123abc.txt ./1234 [root@windriver-machine shtest]#

[root@windriver-machine shtest]# ll total 20 lrwxrwxrwx 1 root      root         8 2011-08-06 22:07 1234 -> 1234.txt -rw-r--r-- 1 root      root         0 2011-08-07 01:43 1234.txt drwsr-sr-x 2 windriver windriver 4096 2011-08-06 21:24 123abc -rw-rw-r-- 2 windriver windriver   74 2011-08-06 22:11 errors -rwxrwxr-x 1 windriver windriver  120 2011-08-06 01:44 ex1.sh -rw-rw-r-- 2 windriver windriver   74 2011-08-06 22:11 newerr -rw-rw-r-- 1 windriver windriver 1640 2011-08-06 19:13 output [root@windriver-machine shtest]# find . -path "./123abc" -prune -o -name "*.txt" -print ./1234.txt [root@windriver-machine shtest]#

【4】Owner 文件用户及用户组  -user –group –uid –gid –nouser -nogroup这些选项都可以使用,针对用户名称及ID进行排除或者指定。

[root@windriver-machine shtest]# find /home -nouser -print [root@windriver-machine shtest]# find /home -group root -print /home /home/windriver/.java /home/windriver/.java/.userPrefs

【5】Time 时间分为两种,一种是时间范围,一种是两个时间进行比较。 前一种时间范围,通常有两种计算单位,一个是24小时,另一个是分钟,小时的使用的是一个文件的访问时间atime,状态修改时间ctime及内容变更时间mtime,分钟对应的是amin cmin mmin

[root@windriver-machine shtest]# stat 1234.txt   File: `1234.txt'   Size: 0               Blocks: 0          IO Block: 4096   regular empty file Device: 805h/2053d      Inode: 8719789     Links: 1 Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root) Access: 2011-08-07 01:43:59.000000000 +0800 Modify: 2011-08-07 01:43:59.000000000 +0800 Change: 2011-08-07 01:43:59.000000000 +0800 [root@windriver-machine shtest]# date Sun Aug  7 01:55:28 CST 2011 [root@windriver-machine shtest]# find . -amin +20 -cmin -6 [root@windriver-machine shtest]# find . -amin +30 -cmin -6 [root@windriver-machine shtest]# find . -amin +30 ./output ./ex1.sh ./newerr ./errors ./123abc/123abc.txt [root@windriver-machine shtest]# find . -amin +20 ./output ./ex1.sh ./newerr ./errors ./123abc/123abc.txt [root@windriver-machine shtest]# find . -amin +10 ./output ./ex1.sh ./newerr ./errors ./1234.txt ./123abc/123abc.txt [root@windriver-machine shtest]# find . -mmin +10 . ./output ./ex1.sh ./newerr ./errors ./1234.txt ./123abc ./123abc/123abc.txt ./1234 [root@windriver-machine shtest]#

[root@windriver-machine shtest]# find / -mtime -5 -print / /var/spool/clientmqueue /var/spool/anacron/cron.daily /var/spool/mqueue /var/spool/mail /var/spool/mail/root /var/tmp

另一种是希望查找更改时间比某个文件新但比另一个文件旧的所有文件可以使用-newer

[root@windriver-machine shtest]# ll total 20 lrwxrwxrwx 1 root      root         8 2011-08-06 22:07 1234 -> 1234.txt -rw-r--r-- 1 root      root         0 2011-08-07 01:43 1234.txt drwsr-sr-x 2 windriver windriver 4096 2011-08-06 21:24 123abc -rw-rw-r-- 2 windriver windriver   74 2011-08-06 22:11 errors -rwxrwxr-x 1 windriver windriver  120 2011-08-06 01:44 ex1.sh -rw-rw-r-- 2 windriver windriver   74 2011-08-06 22:11 newerr -rw-rw-r-- 1 windriver windriver 1640 2011-08-06 19:13 output [root@windriver-machine shtest]# find . -newer output ! -newer 1234.txt . ./newerr ./errors ./1234.txt ./123abc ./123abc/123abc.txt ./1234 [root@windriver-machine shtest]#

【6】type 文件类型,文件类型通常指目录、连接等,使用-type

root@windriver-machine shtest]# find . -type l -print ./1234 [root@windriver-machine shtest]#

【7】SIZE 大小, 以字节计量文件长度的表达形为Nc,以块只用数字如 find . –size +100000c –print  -empty表示空。

root@windriver-machine shtest]# find . -type l -print ./1234 [root@windriver-machine shtest]#

常用的OPTION如下:

【1】mount表示只查找当前文件系统  find . –name “*.XC” –mount –print

【2】P,H,L表示是否根据符号软连接文件进行处理.

常用ACTION如下:

【1】文件名及文件信息打印,使用这些Action: print fprint ls fls, 用来打印到屏幕、输出到文件,列出所有信息等。

[root@windriver-machine shtest]# find . -name "*.txt" -print ./1234.txt ./123abc/123abc.txt [root@windriver-machine shtest]# find . -name "*.txt" -fprint testoutput [root@windriver-machine shtest]# cat testoutput ./1234.txt ./123abc/123abc.txt [root@windriver-machine shtest]# find . -name "*.txt" -ls 8719789    0 -rw-r--r--   1 root     root            0 Aug  7 01:43 ./1234.txt 8719793    0 -rwxr-xr-x   1 windriver windriver        0 Aug  6 21:24 ./123abc/123abc.txt [root@windriver-machine shtest]# find . -name "*.txt" -fls testoutput2 [root@windriver-machine shtest]# cat testoutput2 8719789    0 -rw-r--r--   1 root     root            0 Aug  7 01:43 ./1234.txt 8719793    0 -rwxr-xr-x   1 windriver windriver        0 Aug  6 21:24 ./123abc/123abc.txt [root@windriver-machine shtest]#

【2】运行命令,使用-exec –execdir  -ok 等运行一个指定命令。这些命令需要一些参数,那么怎么放置这些参数呢? 这里使用两个特殊参数,一个是{}代表前面find出来的文件名 “;”代表参数结束。

oot@windriver-machine shtest]# find /etc  -name "passwd" -exec grep "windriver" {} \; windriver:x:500:500::/home/windriver:/bin/bash

[root@windriver-machine shtest]# find . -name '*.h' -execdir diff -u '{}' /tmp/master ';' [root@windriver-machine shtest]#

exec 通常是一个一个文件进行处理,如果使用{}+进行多个文件一起处理,会带来一些性能问题。这时候可以使用xargs进行处理。使用xargs是先进行管道输出,这一点与-exec 这个action是不一样的。

[root@windriver-machine shtest]# cut -d':' -f1</etc/passwd|head -n 3|xargs -p -n 5  finger finger root bin daemon ?...y Login: root                             Name: root Directory: /root                        Shell: /bin/bash Last login Mon Aug 22 22:21 (CST) on pts/0 from 10.141.225.19 Mail last read Wed Sep  7 20:51 2011 (CST) No Plan.

Login: bin                              Name: bin Directory: /bin                         Shell: /sbin/nologin Never logged in. No mail. No Plan.

Login: daemon                           Name: daemon Directory: /sbin                        Shell: /sbin/nologin Never logged in. No mail. No Plan.

Login: rpc                              Name: Rpcbind Daemon Directory: /var/lib/rpcbind             Shell: /sbin/nologin Never logged in. No mail. No Plan.

Login: nscd                             Name: NSCD Daemon Directory: /                            Shell: /sbin/nologin Never logged in. No mail. No Plan.

Login: avahi                            Name: Avahi daemon Directory: /                            Shell: /sbin/nologin Never logged in. No mail. No Plan.

Login: haldaemon                        Name: HAL daemon Directory: /                            Shell: /sbin/nologin Never logged in. No mail. No Plan. [root@windriver-machine shtest]# find . -name '*.h' -execdir diff -u '{}' /tmp/master ';' [root@windriver-machine shtest]#

【3】删除命令  -delete直接就删除了,也可以使用前面的命令 rm

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/08/09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档