linux亦步亦趋(13)文件管理find命令

 故名思议find命令的作用是查找文件。其格式如下:

find 路径  选项 参数;

其主要的几个选项如下:

根据文件名进行查找:-name

格式:find 路径 -name  nameexpr

##查找/etc目录下的所有叫conf的文件或目录
[root@localhost /]# find /etc -name conf 
/etc/httpd/conf
/etc/logwatch/conf
[root@localhost /]#

 

name参数的值可以使用通配符:*代表任意字符,?代表一个字符。实例如下:

 

##通配符的使用 可以左右或者任意一边使用*号进行通配
[root@localhost /]# find /etc/java -name *.conf  
/etc/java/java.conf
[root@localhost /]#
##??占位符通配
[root@localhost /]# find /etc -name ???.conf 
/etc/dbus-1/system.d/hal.conf
/etc/gre.d/gre.conf

根据文件大小进行查找:-size

格式:find  目录 -size  大小;其中大小的单位是block即512bit,大小前面的+、- 分别代表 大于  小于  不写代表等于。

##根据文件的大小查找文件
[root@localhost /]# find /etc -size +4096
/etc/selinux/targeted/modules/active/base.pp
/etc/selinux/targeted/modules/active/base.linked
/etc/gconf/gconf.xml.defaults/%gconf-tree.xml
[root@localhost /]#

 

根据所有者进行查找:-user

格式:find 路径 -user username;

扫描二维码关注公众号,回复: 1131585 查看本文章

 

##根据文件所有者查找文件
[root@localhost /]# find /home -user test
/home/test/test.sh
[root@localhost /]# ls -l /home/test
总计 0
-rwxrwxrwx 1 test test 0 07-22 21:55 test.sh  ##文件的所有者就是test
[root@localhost /]#

根据时间查找:

这个选项比较重要,其中所涉及的时间也比较多,如下:

  • ctime、cmin  文件属性被修改的时间,time是天为单位,min是分钟为单位
  • atime、amin 文件被访问的时间,time是天为单位,min是分钟为单位
  • mtime、mmin 文件内容被修改的时间,time是天为单位,min是分钟为单位

实例如下:

格式:find 目录  -ctime +n  ;+号表示之外,-号表示之内。看例子吧:

 

##根据时间查找文件
[root@localhost /]# find /etc -ctime -1  ##表示一天内修改了文件属性的文件
/etc
/etc/aliases.db
/etc/shadow-
[root@localhost /]# find /etc -amin -1    ##一分钟内被访问过的文件
/etc/mtab
/etc/nsswitch.conf
/etc/sysconfig/networking/profiles/default/resolv.conf
[root@localhost test]# find ./ -mmin -20  ##20分钟内更改过文件内容的文件
./test.sh
[root@localhost test]#

根据类型查找:-type

上面的操作中我们查到的结果有文件,目录,链接。因此我们可以根据type进行过滤。

  • 格式:find 目录  -type typevalue。 
  • 格式l:表示链接文件。
  • 格式f:表示文件。
  • 格式d:表示目录。

实例如下:

##根据类型查找文件
[root@localhost test]# find /etc/fonts -type l  ##查找为链接的文件
/etc/fonts/conf.d/80-delicious.conf
/etc/fonts/conf.d/30-urw-aliases.conf

条件连接符:-a、 -o

  • -a表示and,即与的关系
  • -o表示或。

实例如下:

##查找文件名ini开头的目录
[root@localhost test]# find /etc -name ini* -a -type d
/etc/rc.d/init.d
[root@localhost test]#
##查找文件名为init或者init.d的文件
[root@localhost /]# find /etc -name init -o -name init.d
/etc/rc.d/init.d
/etc/init.d
/etc/sysconfig/init
[root@localhost /]#

二次操作连接执行:-exec、-ok

我们找到的文件要是进行二次操作应该怎么办呢,我们可以使用 -exec 或者-ok进行连接处理

  • find ..... -exec 命令 {} \;  找到结果后对文件进行命令处理,且不进行询问。
  • find ..... -ok 命令 {} \;找到结果后对文件进行命令处理,且进行询问。

如下实例:

##-exec 连接二次操作
[root@localhost home]# find ./ -name test.sh -exec ls -l {} \; ##找到后ls一下文件的属性
-rwxrwxrwx 1 test test 4 07-22 23:56 ./test/test.sh
[root@localhost home]#
##-ok 连接二次操作
[root@localhost home]# find ./ -name test.sh -ok ls -l {} \; ##使用ok
< ls ... ./test/test.sh > ? y ##此处对我们进行了询问
-rwxrwxrwx 1 test test 4 07-22 23:56 ./test/test.sh
[root@localhost home]#

操作特殊文件:-inum

这里所说的特殊文件指的是文件名特殊,我们不方便输入的。比如有乱码、有特殊字符、不可见字符等。而我们要执行改名啊,删除啊,修改啊 等操作。

我们可以使用下面的两个命令进行结合:

  • ls -i 文件名
  • find -inum inode值 -exec 命令 {} \;

举例如下:

##目录下有一个比较怪的文件名的文件,我们要将其删除,直接用文件名肯定是比较费劲的。
[root@localhost test]# ls -l
总计 4
-rw-r--r-- 1 root root 0 07-23 00:16 file df dfd  ~ i~
-rwxrwxrwx 1 test test 4 07-22 23:56 test.sh
[root@localhost test]#
##使用ls -li 显示问价的inum值
[root@localhost test]# ls -li
总计 4
2107057 -rw-r--r-- 1 root root 0 07-23 00:16 file df dfd  ~ i~
2107044 -rwxrwxrwx 1 test test 4 07-22 23:56 test.sh
[root@localhost test]#
##下面使用find命令删除
[root@localhost test]# find ./ -inum 2107057 -exec rm -f {} \;
[root@localhost test]# ls -li
总计 4
2107044 -rwxrwxrwx 1 test test 4 07-22 23:56 test.sh

 故名思议find命令的作用是查找文件。其格式如下:

find 路径  选项 参数;

其主要的几个选项如下:

根据文件名进行查找:-name

格式:find 路径 -name  nameexpr

##查找/etc目录下的所有叫conf的文件或目录
[root@localhost /]# find /etc -name conf 
/etc/httpd/conf
/etc/logwatch/conf
[root@localhost /]#

 

name参数的值可以使用通配符:*代表任意字符,?代表一个字符。实例如下:

 

##通配符的使用 可以左右或者任意一边使用*号进行通配
[root@localhost /]# find /etc/java -name *.conf  
/etc/java/java.conf
[root@localhost /]#
##??占位符通配
[root@localhost /]# find /etc -name ???.conf 
/etc/dbus-1/system.d/hal.conf
/etc/gre.d/gre.conf

根据文件大小进行查找:-size

格式:find  目录 -size  大小;其中大小的单位是block即512bit,大小前面的+、- 分别代表 大于  小于  不写代表等于。

##根据文件的大小查找文件
[root@localhost /]# find /etc -size +4096
/etc/selinux/targeted/modules/active/base.pp
/etc/selinux/targeted/modules/active/base.linked
/etc/gconf/gconf.xml.defaults/%gconf-tree.xml
[root@localhost /]#

 

根据所有者进行查找:-user

格式:find 路径 -user username;

 

##根据文件所有者查找文件
[root@localhost /]# find /home -user test
/home/test/test.sh
[root@localhost /]# ls -l /home/test
总计 0
-rwxrwxrwx 1 test test 0 07-22 21:55 test.sh  ##文件的所有者就是test
[root@localhost /]#

根据时间查找:

这个选项比较重要,其中所涉及的时间也比较多,如下:

  • ctime、cmin  文件属性被修改的时间,time是天为单位,min是分钟为单位
  • atime、amin 文件被访问的时间,time是天为单位,min是分钟为单位
  • mtime、mmin 文件内容被修改的时间,time是天为单位,min是分钟为单位

实例如下:

格式:find 目录  -ctime +n  ;+号表示之外,-号表示之内。看例子吧:

 

##根据时间查找文件
[root@localhost /]# find /etc -ctime -1  ##表示一天内修改了文件属性的文件
/etc
/etc/aliases.db
/etc/shadow-
[root@localhost /]# find /etc -amin -1    ##一分钟内被访问过的文件
/etc/mtab
/etc/nsswitch.conf
/etc/sysconfig/networking/profiles/default/resolv.conf
[root@localhost test]# find ./ -mmin -20  ##20分钟内更改过文件内容的文件
./test.sh
[root@localhost test]#

根据类型查找:-type

上面的操作中我们查到的结果有文件,目录,链接。因此我们可以根据type进行过滤。

  • 格式:find 目录  -type typevalue。 
  • 格式l:表示链接文件。
  • 格式f:表示文件。
  • 格式d:表示目录。

实例如下:

##根据类型查找文件
[root@localhost test]# find /etc/fonts -type l  ##查找为链接的文件
/etc/fonts/conf.d/80-delicious.conf
/etc/fonts/conf.d/30-urw-aliases.conf

条件连接符:-a、 -o

  • -a表示and,即与的关系
  • -o表示或。

实例如下:

##查找文件名ini开头的目录
[root@localhost test]# find /etc -name ini* -a -type d
/etc/rc.d/init.d
[root@localhost test]#
##查找文件名为init或者init.d的文件
[root@localhost /]# find /etc -name init -o -name init.d
/etc/rc.d/init.d
/etc/init.d
/etc/sysconfig/init
[root@localhost /]#

二次操作连接执行:-exec、-ok

我们找到的文件要是进行二次操作应该怎么办呢,我们可以使用 -exec 或者-ok进行连接处理

  • find ..... -exec 命令 {} \;  找到结果后对文件进行命令处理,且不进行询问。
  • find ..... -ok 命令 {} \;找到结果后对文件进行命令处理,且进行询问。

如下实例:

##-exec 连接二次操作
[root@localhost home]# find ./ -name test.sh -exec ls -l {} \; ##找到后ls一下文件的属性
-rwxrwxrwx 1 test test 4 07-22 23:56 ./test/test.sh
[root@localhost home]#
##-ok 连接二次操作
[root@localhost home]# find ./ -name test.sh -ok ls -l {} \; ##使用ok
< ls ... ./test/test.sh > ? y ##此处对我们进行了询问
-rwxrwxrwx 1 test test 4 07-22 23:56 ./test/test.sh
[root@localhost home]#

操作特殊文件:-inum

这里所说的特殊文件指的是文件名特殊,我们不方便输入的。比如有乱码、有特殊字符、不可见字符等。而我们要执行改名啊,删除啊,修改啊 等操作。

我们可以使用下面的两个命令进行结合:

  • ls -i 文件名
  • find -inum inode值 -exec 命令 {} \;

举例如下:

##目录下有一个比较怪的文件名的文件,我们要将其删除,直接用文件名肯定是比较费劲的。
[root@localhost test]# ls -l
总计 4
-rw-r--r-- 1 root root 0 07-23 00:16 file df dfd  ~ i~
-rwxrwxrwx 1 test test 4 07-22 23:56 test.sh
[root@localhost test]#
##使用ls -li 显示问价的inum值
[root@localhost test]# ls -li
总计 4
2107057 -rw-r--r-- 1 root root 0 07-23 00:16 file df dfd  ~ i~
2107044 -rwxrwxrwx 1 test test 4 07-22 23:56 test.sh
[root@localhost test]#
##下面使用find命令删除
[root@localhost test]# find ./ -inum 2107057 -exec rm -f {} \;
[root@localhost test]# ls -li
总计 4
2107044 -rwxrwxrwx 1 test test 4 07-22 23:56 test.sh

 故名思议find命令的作用是查找文件。其格式如下:

find 路径  选项 参数;

其主要的几个选项如下:

根据文件名进行查找:-name

格式:find 路径 -name  nameexpr

##查找/etc目录下的所有叫conf的文件或目录
[root@localhost /]# find /etc -name conf 
/etc/httpd/conf
/etc/logwatch/conf
[root@localhost /]#

 

name参数的值可以使用通配符:*代表任意字符,?代表一个字符。实例如下:

 

##通配符的使用 可以左右或者任意一边使用*号进行通配
[root@localhost /]# find /etc/java -name *.conf  
/etc/java/java.conf
[root@localhost /]#
##??占位符通配
[root@localhost /]# find /etc -name ???.conf 
/etc/dbus-1/system.d/hal.conf
/etc/gre.d/gre.conf

根据文件大小进行查找:-size

格式:find  目录 -size  大小;其中大小的单位是block即512bit,大小前面的+、- 分别代表 大于  小于  不写代表等于。

##根据文件的大小查找文件
[root@localhost /]# find /etc -size +4096
/etc/selinux/targeted/modules/active/base.pp
/etc/selinux/targeted/modules/active/base.linked
/etc/gconf/gconf.xml.defaults/%gconf-tree.xml
[root@localhost /]#

 

根据所有者进行查找:-user

格式:find 路径 -user username;

 

##根据文件所有者查找文件
[root@localhost /]# find /home -user test
/home/test/test.sh
[root@localhost /]# ls -l /home/test
总计 0
-rwxrwxrwx 1 test test 0 07-22 21:55 test.sh  ##文件的所有者就是test
[root@localhost /]#

根据时间查找:

这个选项比较重要,其中所涉及的时间也比较多,如下:

  • ctime、cmin  文件属性被修改的时间,time是天为单位,min是分钟为单位
  • atime、amin 文件被访问的时间,time是天为单位,min是分钟为单位
  • mtime、mmin 文件内容被修改的时间,time是天为单位,min是分钟为单位

实例如下:

格式:find 目录  -ctime +n  ;+号表示之外,-号表示之内。看例子吧:

 

##根据时间查找文件
[root@localhost /]# find /etc -ctime -1  ##表示一天内修改了文件属性的文件
/etc
/etc/aliases.db
/etc/shadow-
[root@localhost /]# find /etc -amin -1    ##一分钟内被访问过的文件
/etc/mtab
/etc/nsswitch.conf
/etc/sysconfig/networking/profiles/default/resolv.conf
[root@localhost test]# find ./ -mmin -20  ##20分钟内更改过文件内容的文件
./test.sh
[root@localhost test]#

根据类型查找:-type

上面的操作中我们查到的结果有文件,目录,链接。因此我们可以根据type进行过滤。

  • 格式:find 目录  -type typevalue。 
  • 格式l:表示链接文件。
  • 格式f:表示文件。
  • 格式d:表示目录。

实例如下:

##根据类型查找文件
[root@localhost test]# find /etc/fonts -type l  ##查找为链接的文件
/etc/fonts/conf.d/80-delicious.conf
/etc/fonts/conf.d/30-urw-aliases.conf

条件连接符:-a、 -o

  • -a表示and,即与的关系
  • -o表示或。

实例如下:

##查找文件名ini开头的目录
[root@localhost test]# find /etc -name ini* -a -type d
/etc/rc.d/init.d
[root@localhost test]#
##查找文件名为init或者init.d的文件
[root@localhost /]# find /etc -name init -o -name init.d
/etc/rc.d/init.d
/etc/init.d
/etc/sysconfig/init
[root@localhost /]#

二次操作连接执行:-exec、-ok

我们找到的文件要是进行二次操作应该怎么办呢,我们可以使用 -exec 或者-ok进行连接处理

  • find ..... -exec 命令 {} \;  找到结果后对文件进行命令处理,且不进行询问。
  • find ..... -ok 命令 {} \;找到结果后对文件进行命令处理,且进行询问。

如下实例:

##-exec 连接二次操作
[root@localhost home]# find ./ -name test.sh -exec ls -l {} \; ##找到后ls一下文件的属性
-rwxrwxrwx 1 test test 4 07-22 23:56 ./test/test.sh
[root@localhost home]#
##-ok 连接二次操作
[root@localhost home]# find ./ -name test.sh -ok ls -l {} \; ##使用ok
< ls ... ./test/test.sh > ? y ##此处对我们进行了询问
-rwxrwxrwx 1 test test 4 07-22 23:56 ./test/test.sh
[root@localhost home]#

操作特殊文件:-inum

这里所说的特殊文件指的是文件名特殊,我们不方便输入的。比如有乱码、有特殊字符、不可见字符等。而我们要执行改名啊,删除啊,修改啊 等操作。

我们可以使用下面的两个命令进行结合:

  • ls -i 文件名
  • find -inum inode值 -exec 命令 {} \;

举例如下:

##目录下有一个比较怪的文件名的文件,我们要将其删除,直接用文件名肯定是比较费劲的。
[root@localhost test]# ls -l
总计 4
-rw-r--r-- 1 root root 0 07-23 00:16 file df dfd  ~ i~
-rwxrwxrwx 1 test test 4 07-22 23:56 test.sh
[root@localhost test]#
##使用ls -li 显示问价的inum值
[root@localhost test]# ls -li
总计 4
2107057 -rw-r--r-- 1 root root 0 07-23 00:16 file df dfd  ~ i~
2107044 -rwxrwxrwx 1 test test 4 07-22 23:56 test.sh
[root@localhost test]#
##下面使用find命令删除
[root@localhost test]# find ./ -inum 2107057 -exec rm -f {} \;
[root@localhost test]# ls -li
总计 4
2107044 -rwxrwxrwx 1 test test 4 07-22 23:56 test.sh
 故名思议find命令的作用是查找文件。其格式如下: find 路径  选项 参数; 其主要的几个选项如下: 格式:find 路径 -name  nameexpr   name参数的值可以使用通配符:*代表任意字符,?代表一个字符。实例如下:   格式:find  目录 -size  大小;其中大小的单位是block即512bit,大小前面的+、- 分别代表 大于  小于  不写代表等于。   格式:find 路径 -user username;   这个选项比较重要,其中所涉及的时间也比较多,如下:
  • ctime、cmin  文件属性被修改的时间,time是天为单位,min是分钟为单位
  • atime、amin 文件被访问的时间,time是天为单位,min是分钟为单位
  • mtime、mmin 文件内容被修改的时间,time是天为单位,min是分钟为单位
实例如下: 格式:find 目录  -ctime +n  ;+号表示之外,-号表示之内。看例子吧:   上面的操作中我们查到的结果有文件,目录,链接。因此我们可以根据type进行过滤。
  • 格式:find 目录  -type typevalue。 
  • 格式l:表示链接文件。
  • 格式f:表示文件。
  • 格式d:表示目录。

猜你喜欢

转载自xuelianbobo.iteye.com/blog/2096268
今日推荐