Linux常用命令(3)--文件搜索命令


Linux常用命令(3)--文件搜索命令
 
1、命令名称:which  www.2cto.com  
 
  命令所在路径:/usr/bin/which
 
  执行权限:所有用户
 
  功能描述:显示系统命令所在目录
 
  语法:which [命令名称]
 
[root@localhost ~]# which ls 
alias ls='ls --color=auto'
        /bin/ls
[root@localhost ~]# 
 
2、命令名称:find
 
  执行权限:所有用户
 
  功能描述:查找文件或目录
 
  语法:find [搜索路径] [搜索关键字]
 
   1) -name 根据名字查找
 
    * 匹配任意字符
 
    ? 匹配单个字符  www.2cto.com  
 
在目录/etc中根据名字查找文件init
[root@localhost ~]# find /etc -name init
/etc/webmin/init
/etc/kdump-adv-conf/kdump_initscripts/init
/etc/init
/etc/sysconfig/init
[root@localhost ~]# 
 
使用*通配符查找以init开头的文件
[root@localhost ~]# find /etc -name init*
/etc/selinux/targeted/contexts/initrc_context
/etc/inittab
/etc/webmin/inittab
/etc/webmin/init
/etc/kdump-adv-conf/kdump_initscripts/init
/etc/init
/etc/init/init-system-dbus.conf
/etc/rc.d/init.d
/etc/sysconfig/init
/etc/sysconfig/network-scripts/init.ipv6-global
/etc/init.d
[root@localhost ~]# 
 
使用?通配符查找以init开头,后面有三个字符的文件
[root@localhost ~]# find /etc -name init???
/etc/inittab
/etc/webmin/inittab
[root@localhost ~]#
 
  2)-size 根据文件大小查找 以数据块(block)为单位 512字节 = 0.5KB
 
    大于 +
 
    小于 -
 
    等于 直接跟数值
 
在/etc目录下查找大于1MB的文件 1MB = 1024KB = 2048数据块(block)
[root@localhost ~]# find  /etc -size +2048
/etc/selinux/targeted/policy/policy.24
/etc/selinux/targeted/modules/active/policy.kern
/etc/gconf/gconf.xml.defaults/%gconf-tree.xml
[root@localhost ~]# 
  3)-user 根据文件所有者查找
 
在/home目录下查找所有者为luxh的文件
[root@localhost ~]# find /home -user luxh
  4)根据时间查找
 
    (以天为单位):ctime、atime、mtime
 
    (以分钟为单位):cmin、amin、mmin
 
    c-->change 改变,表示文件的属性被修改过,例如所有者、所属组、权限被改变
 
    a-->access 访问,标示文件被访问过
 
    m-->modify 修改,表示文件的内容被修改过
 
    - 表示多长时间之内
 
    + 表示超过多少时间
 
在/etc目录下查找在1天之内被修改过属性的文件和目录
[root@localhost ~]# find /etc -ctime -1
/etc
/etc/resolv.conf
/etc/mtab
/etc/avahi/etc/localtime
[root@localhost ~]#
 
在/etc目录下查找在两个小时之内被修改过内容的文件(两个小时就是120分钟)
[root@localhost ~]# find /etc -mmin -120
/etc
/etc/resolv.conf
/etc/mtab
[root@localhost ~]#
 
  5)-type 根据文件类型查找
 
    f 二级制文件,l软链接文件,d目录
 
查找/etc目录下的目录文件
[root@localhost ~]# find /etc -type d
  6)连接符:
 
    6.1)-a and 逻辑与, -o or 逻辑或
 
在/etc目录下查找大于80MB小于100MB的文件
[root@localhost ~]# find /etc -size +163840 -a -size -204800
    6.2)find ...  -exec 命令 {} \; 或者 find ...  -ok 命令 {} \;   -ok会有询问过程     
 
      {} find查询的结果
 
      \   转义符,使得符号和命令使用本身的含义
 
      ; 结果,表示语句的结束
 
在/etc目录下查找inittab文件并显示其详细信息
[root@localhost ~]# find /etc -name inittab -exec ls -l {} \;
-rw-r--r--. 1 root root 884 May  5  2012 /etc/inittab
total 4
-rw-------. 1 root bin 42 May 16  2012 config
[root@localhost ~]# 
 
查找并删除/abc目录下的testfile
[root@localhost ~]# find /abc -name testfile -exec rm -rf {} \;
 
查找并删除/abc目录下的a.txt,使用-ok会提示询问
[root@localhost ~]# find /abc -name a.txt -ok rm {} \;
< rm ... /abc/a.txt > ? y
 
查找/etc目录下以init*开头的目录文件并且详细显示它们的信息
 
[root@localhost ~]# find /etc -name init* -a -type f -exec ls -l {} \;
-rw-r--r--. 1 root root 30 Dec  7  2011 /etc/selinux/targeted/contexts/initrc_context
-rw-r--r--. 1 root root 884 May  5  2012 /etc/inittab
-rwxr-xr-x. 1 root root 4781 Oct 29  2009 /etc/kdump-adv-conf/kdump_initscripts/init
-rw-r--r--. 1 root root 130 Jul 18  2011 /etc/init/init-system-dbus.conf
-rw-r--r--. 1 root root 1154 Dec  8  2011 /etc/sysconfig/init
-rwxr-xr-x. 1 root root 4623 Oct  7  2011 /etc/sysconfig/network-scripts/init.ipv6-global
[root@localhost ~]#
 
  7)-inum 根据文件i节点查找
 
    ls -i  显示文件的i节点
 
[root@localhost ~]# ls -i /abc
  5355 issue.hard  129390 issue.soft  130438 prem
[root@localhost ~]# find /abc -inum 5355
/abc/issue.hard
[root@localhost ~]#
 
有时候可能别人创建了一些命名奇怪的文件,使用rm删不掉
 
[root@localhost ~]# ls -l /abc
total 4
-rw-r--r--. 1 root root    0 Nov 20 06:09 a b
-rw-r--r--. 3 root root    0 Nov 15 04:51 issue.hard
lrwxrwxrwx. 1 root root   10 Nov 15 04:43 issue.soft -> /etc/issue
drwxr-xr-x. 2 root root 4096 Nov 15 05:36 prem
[root@localhost ~]# rm a b
rm: cannot remove `a': No such file or directory
rm: cannot remove `b': No such file or directory
[root@localhost ~]# 
 
这个我们可以根据文件的i节点删除
[root@localhost ~]# ls -i /abc
 a b    5355 issue.hard  129390 issue.soft  130438 prem
[root@localhost ~]# find /abc -inum 129330 -exec rm {} \;
[root@localhost ~]# ls -i /abc
 issue.hard  129390 issue.soft  130438 prem
[root@localhost ~]#
 
3、命令名称:locate
 
  执行权限:所有用户
 
  功能描述:查找文件或目录
 
  语法:locate  [搜索关键字]
 
[root@localhost ~]# locate inittab
  locate 是在一个系统定期更新的文件数据库中查找,速度比较快。系统自动调用updatedb定期更新目录文件数据库
 
4、命令名称:updatedb
 
  执行权限:root
 
  功能描述:建立整个系统目录文件的数据库
 
  语法:updatedb
 
5、命令名称:grep
 
  执行权限:所以用户
 
  功能描述:在文件中搜寻字串匹配的行并输出
 
  语法:grep [指定字串] [源文件]
 
在/etc/services文件中查找tftp匹配的行
[root@localhost ~]# grep tftp /etc/services
tftp            69/tcp
tftp            69/udp
tftp-mcast      1758/tcp
tftp-mcast      1758/udp
mtftp           1759/udp        spss-lm
subntbcst_tftp  247/tcp                 # SUBNTBCST_TFTP
subntbcst_tftp  247/udp                 # SUBNTBCST_TFTP
etftp           1818/tcp                # Enhanced Trivial File Transfer Protocol
etftp           1818/udp                # Enhanced Trivial File Transfer Protocol
tftps           3713/tcp                # TFTP over TLS
tftps           3713/udp                # TFTP over TLS
[root@localhost ~]#
 

相关内容

    暂无相关文章