shell之路 Linux核心命令【第四篇】文件管理

文件与目录管理

http://www.apelearn.com/study_v2/chapter7.html

文档的压缩与打包

http://www.apelearn.com/study_v2/chapter11.html

文件搜索工具Find命令

为什么使用find

查找方式功能强大

1 which 查看可执行文件的位置。
( which指令会在PATH变量指定的路径中,搜索某个系统命令的位置,并且返回第一个搜索结果。)

2 whereis 查看文件的位置。
(whereis命令是定位可执行文件、源代码文件、帮助文件在文件系统中的位置。这些文件的属性应属于原始代码,二进制文件,或是帮助文件。)

3 locate 配合数据库查看文件位置。
(需要updatedb,不精准搜索,不能搜索/tmp下的文件)

4 find 实际搜寻硬盘查询文件名称。

find命令总结

常用选项

-name    查找/etc目录下以conf结尾的文件                     find /etc -name ' *conf,
-iname   查找当前目录下文件名为aa的文件,不区分大小写          find . -iname aa
-user      查找文件属主为hdfs的所有文件                     find . -user hdfs
-group    查找文件属组为yarn的所有文件                      find . -group yarn
-type
	f  文件        find. -type f
	d    目录       find . -type d
	c  字符设备文件    find.-type c
	b    块设备文件    find.-type b
	l  链接文件      find . -type l
	p    管道文件      find . -type P

-size
	-n  大小大于n的文件
	+n   大小小于n的文件
	n   大小等于n的文件

	例子1:查找/etc目录下小于10000字节的文件  find /etc -size -10000c
	例子2:香找/etc目录下大干1M的文件      find /etc -size +1M

-mtime
	-n  n天以内修改的文件
	+n  n天以外修改的文件n  正好n天修改的文件

	例子1:查找/etc目录下5天之内修改且以conf结尾的文件find /etc -mtime -5 -name *. conf' 
	例子2:查找/etc目 录下10天之前修改且属主为root的文件 find /etc -mtime +10 -user root

-mmin  
	-n  n分钟以内修改的文件+n  n分钟以外修改的文件

	例子1:查找/etc目录下30分钟之前修改的文件  find /etc -mmin +30
	例子2:查找/etc 目录下30分钟之内修改的目录  find /etc -mmin -30 -type d

-mindepth n  表示从n级子目录开始搜索
	例子:在/etc 下的3级子目录开始搜索  find /etc -mindepth 3

-maxdepth n  表示最多搜索到n-1级子目录

	例子1:在/etc下搜索符合条件的文件,但最多搜索到2级子目录  find /etc -maxdepth 3 -name **.
	例子2:find ./etc/ -type f -name '*.conf' -size +10k -maxdepth 2

了解选项

了解选项:
-nouser  查找没有属主的用户
	例子: find .-type f -nouser-nogroup  查找没有属组的用户
	例子: find. -type f -nogroup

-perm
	例子: find. -perm 664

-prune
	通常和-path起使用,用于将特定目录排除在搜索条件之外
	例子1:查找当前目录下所有普通文件,但排除test目录(-o是逻辑运算符,后面有详解)
		find . -path . /etc -prune -o -type f
	例子2:查找当前目录下所有普通文件,但排除etc和opt目录
		find . -path ./etc -prune -o -path ./opt -prune -o -type f
	例子3:查找当前目录下所有普通文件,但排除etc和opt目录,但属主为hdfs
		find . -path ./etc -prune -o -path ./opt -prune -o -type f -a -user hdfs
	例子4:查找当前目录下所有普通文件,但排除etc和opt目录,但属主为hdfs, 且文件大小必须大于500字之广 		
     find . -path ./etc -prune -o -path ./opt -prune -o -type f -a -user hdfs -a -size +500c
	
-newer file1
	例子: find /etc -newer a

操作

-exec  	对搜索到的文件执行特定的操作,格式为-exec , 'comnand' {} \;  # 每个{}代表一个文件
    例子1:搜索/etc 下的文件(非目录),文件名以conf结尾,且大于10k, 然后将其删除
        find ./etc/ -type f -name '**.conf' -size +10k -exec rm -f {} \;
	例子2:将/var/1og/目录下以1og结尾的文件,且 更改时间在7天以上的删除
		find /var/1og/ -name  '**.1og' -mtime +7 -exec rm -rf {} \;
    例子3:搜索条件和例子1一样,只是不删除,而是将其复制到/root/conf目录下
        find ./etc/ -size +10k -type f -name '**.conf' -exec cp {} /root/conf/ \;

 

注意:xargs没有exec功能强大,文件名有特殊字符的情况下xargs不能识别,因此xargs不推荐使用

这里的{}可代表文件和目录

find -exec cmd {} \; #{}代表单个文件,-exec仅是find的一个参数
 
cmd |xargs -i cmd {} #-i 代表单个文件

逻辑运算符

-a 与
-o 或
-notl! 非

例子1:查找当前目录下,属主不是hdfs的所有文件
  find . -not -user hdfs | find . ! -user hdfs
例子2:查找当前目录下,属主属于hdfs,且大小大于300字节的文件
  find . type f -a -user hdfs -a -user hdfs -a -size +300c
例子3:查找当前目录下的属主为hdfs或者以xm1结尾的普通文件
  find . -type f -a ( -user hdfs -o -name '*.xml' )

  

  

 

posted @ 2015-08-21 15:00  沐风先生  阅读(218)  评论(0编辑  收藏  举报