您的位置:首页 > 运维架构 > Linux

Linux find命令示例

2015-08-13 00:38 561 查看
find命令
1

[root@localhost ~]# ls
aaa aaa.sh anaconda-ks.cfg install.log install.log.syslog

[root@localhost ~]# find ./* -name "[aa]*.sh" -exec rm {} \;
[root@localhost ~]# ls
aaa anaconda-ks.cfg install.log install.log.syslog
注意 rm空格{ }空格\;(封号),一个都不能少!。
-exec是将查找到的文件被执行某种命令的的命令!
-name 参数后跟想要查找的文件名用“ ”包含,而文件的话一般不用“ ”号。
-name 的参数可以用正则表达式,如上"[aa]*.sh",以aa开头的.sh后缀的文件。
2

[root@localhost ~]# ls
aaa anaconda-ks.cfg install.log install.log.syslog
[root@localhost ~]# find ~/* -mtime -1 -type f -print
/root/aaa
-mtime 按更改时间查找文件 +n 表示n天以前 -n表示n天之内
-type 表示文件类型 参数 b/d/c/p/l/f 分别是块设备、目录、字符设备、管道、符号链接、普通文件,-print打印。
[root@localhost ~]# find ./* -mtime +1 -type f -print
./anaconda-ks.cfg
./install.log
./install.log.syslog

3
[root@localhost ~]# ls -l |grep aaa
-rw-r--r-- 1 root root 134 8月 11 16:25 aaa
[root@localhost ~]# chmod 0777 aaa
[root@localhost ~]# ls -l |grep aaa
-rwxrwxrwx 1 root root 134 8月 11 16:25 aaa
[root@localhost ~]# find ./* -cmin -1 -type f -print
./aaa
[root@localhost ~]#
-cmin -ctime 按文件创建时间来查找文件 -cminb表示多少分钟 -ctime表示天数 -1表示min内,上面例子更改了aaa的属性。

4

[root@localhost share]# ls -lh
总用量 28M
-rw-r--r-- 1 root root 54 7月 22 23:23 aaa.txt
-rw-r--r-- 1 root root 6 7月 22 23:19 bbb
-rw-r--r-- 1 root root 9 7月 22 23:21 ccc.txt
-rw-r--r-- 1 root root 28M 8月 11 18:27 LINUX_FIREWALL.pdf
[root@localhost share]# find . -type f -size +10 -exec ls -l {} \;
-rw-r--r-- 1 root root 28663077 8月 11 18:27 ./LINUX_FIREWALL.pdf
[root@localhost share]#
[root@localhost share]# find . -type f -size -10 -exec ls -l {} \;
-rw-r--r-- 1 root root 54 7月 22 23:23 ./aaa.txt
-rw-r--r-- 1 root root 6 7月 22 23:19 ./bbb
-rw-r--r-- 1 root root 9 7月 22 23:21 ./ccc.txt
上例中 -size 查找文件大小默认是块 参数是 n[c],其中c代表字节,1块=512字节=512*8(bit)

-exec 对查找到的结果执行命令 ls -l 显示出详细信息.. +10=10 大于10块的文件 -10小于10块的文件如查找文件大小为6字节的文件示例:[root@localhost share]# find . -type f -size 6c -exec ls -l {} \;
-rw-r--r-- 1 root root 6 7月 22 23:19 ./bbb

5
[root@localhost share]# ls -l
总用量 28004
-rwxrwxrwx 1 root root 54 7月 22 23:23 aaa.txt
-rw-r--r-- 1 root root 6 7月 22 23:19 bbb
-rw-r--r-- 1 root root 9 7月 22 23:21 ccc.txt
-rw-r--r-- 1 root root 28663077 8月 11 18:27 LINUX_FIREWALL.pdf
[root@localhost share]# find -perm 0777 -print
.
./aaa.txt
[root@localhost share]#
-perm 按执行权限查找 0777= u=rwx,g=rwx,o=rwx 并打印,可以看到多了一个 . 这个点代表当前目录,是系统默认的没关系用 ls -a 可以看到每个文件夹都会有两个隐藏文件 . 和 .. 。

6
[root@localhost share]# find . -type f -size +10 -exec rm {} \;
找出当前路径中 大于10块的文件并删除。
[root@localhost share]# ls -l
-rwxrwxrwx 1 root root 54 7月 22 23:23 aaa.txt-rw-r--r-- 1 root root 6 7月 22 23:19 bbb-rw-r--r-- 1 root root 9 7月 22 23:21 ccc.txt然后排序 -n是按数值大小排序 -r是降序,默认情况下是升序, head -n -n是显示头n行。[root@localhost share]# find . -type f -exec ls -l {} \; | sort -n -r | head -2
-rwxrwxrwx 1 root root 54 7月 22 23:23 ./aaa.txt
-rw-r--r-- 1 root root 9 7月 22 23:21 ./ccc.txt

---------------------------------------------------------------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: