首页 > 代码库 > 文件查找 /which/whereis/locate/find/grep

文件查找 /which/whereis/locate/find/grep

Which #查找固定的目录#

[root@lab home]# which ba

/usr/bin/which: no ba in (/usr/lib/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)

[root@lab home]# which ls

alias ls=‘ls --color=auto‘

        /bin/ls

我们可以看到 which 命令只查这些指定目录。

Whereis  #帮助文件也显示给我们#

[root@lab home]# whereis ls

ls: /bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz

[root@lab home]#

clipboard

Locate 案例:

在/home/huxiaoming目录下, 新建了1.txt和2.txt两个文件。

[root@lab huxiaoming]# locate 1.txt

/etc/pki/nssdb/pkcs11.txt

/usr/share/cups/charmaps/iso-8859-1.txt

/usr/share/cups/charmaps/iso-8859-11.txt

/usr/share/cups/charmaps/windows-1251.txt

/usr/share/cups/charmaps/windows-1361.txt

/usr/share/doc/dstat-0.7.0/dstat.1.txt

/usr/share/vim/vim72/doc/gui_x11.txt.gz

/usr/share/vim/vim72/doc/usr_01.txt.gz

/usr/share/vim/vim72/doc/usr_11.txt.gz

/usr/share/vim/vim72/doc/usr_21.txt.gz

/usr/share/vim/vim72/doc/usr_31.txt.gz

/usr/share/vim/vim72/doc/usr_41.txt.gz

[root@lab huxiaoming]# locate 2.txt

/usr/share/cups/charmaps/iso-8859-2.txt

/usr/share/cups/charmaps/windows-1252.txt

/usr/share/cups/charmaps/windows-932.txt

/usr/share/doc/certmonger-0.61/dogtag-notes-2.txt

/usr/share/doc/python-lxml-2.2.3/doc/lxml2.txt

/usr/share/doc/vim-common-7.2.411/README_os2.txt

/usr/share/vim/vim72/doc/gui_w32.txt.gz

/usr/share/vim/vim72/doc/os_os2.txt.gz

/usr/share/vim/vim72/doc/os_win32.txt.gz

/usr/share/vim/vim72/doc/usr_02.txt.gz

/usr/share/vim/vim72/doc/usr_12.txt.gz

/usr/share/vim/vim72/doc/usr_22.txt.gz

/usr/share/vim/vim72/doc/usr_32.txt.gz

/usr/share/vim/vim72/doc/usr_42.txt.gz

并没有发现 新加的文件, 这是因为我们要执行一下更新目录树的命令

[root@lab huxiaoming]# updatedb   更新目录树

[root@lab huxiaoming]# locate 2.txt

/home/huxiaoming/2.txt  这样就找到了

[root@lab huxiaoming]# cat /etc/cron.daily/mlocate.cron

#!/bin/sh

nodevs=$(< /proc/filesystems awk ‘$1 == "nodev" { print $2 }‘)

renice +19 -p $$ >/dev/null 2>&1

ionice -c2 -n7 -p $$ >/dev/null 2>&1

/usr/bin/updatedb -f "$nodevs"

实际上系统每天都会执行一个mlocate.cron  来更新目录树

find 命令---在当前目录查找

[root@lab huxiaoming]# find 1.txt

1.txt

[root@lab huxiaoming]# cd /

[root@lab /]# find 1.txt

find: `1.txt‘: No such file or directory

如果想查找整个硬盘,此时根据一下命令,从根目录开始一个个目录树开始查找

[root@lab /]# find / -name 1.txt

/home/huxiaoming/1.txt

正则表达式 一些find技巧

例如我想配置网卡,但是我只记得配置文件大概叫ifc...之类,在一个 /etc目录中

find /etc/ -name "*ifc*"

[root@lab /]# find /etc -name "*ifcfg-eth0*" -exec vi {} \;

{里面代表搜寻出的文件 }可以直接在-exec后面加参数来执行

两种显示方法: 一种 -al 另一直把ls 作为命令来调用

clipboard[1]

clipboard[2]

还有一些高级的技巧

如何批量按提示删除.txt文件

[root@lab /]# find /home -name "*.txt*" -ok rm {} \;

< rm ... /home/huxiaoming/2.txt > ? no

< rm ... /home/huxiaoming/1.txt > ? yes

[root@lab /]# find /home -name "*.txt*" -ok ls {} \;                                   #这里就是需要find之后的动作是否需要交互#

< ls ... /home/huxiaoming/2.txt > ? yes

/home/huxiaoming/2.txt

如何批量查找属于某些用户的文件夹:

[root@lab /]# find /home -user huxiaoming -type d -ls  #如果不加type则显示所有#

129793 4 drwx------ 2 huxiaoming huxiaoming 4096 Mar 18 17:14 /home/huxiaoming

[root@lab /]# find /home -user huxiaoming -ls

129793 4 drwx------ 2 huxiaoming huxiaoming 4096 Mar 18 17:14 /home/huxiaoming

129795 4 -rw-r--r-- 1 huxiaoming huxiaoming 18 Jul 9 2013 /home/huxiaoming/.bash_logout

129794 4 -rw-r--r-- 1 huxiaoming huxiaoming 176 Jul 9 2013 /home/huxiaoming/.bash_profile

129798 4 -rw------- 1 huxiaoming huxiaoming 25 Mar 13 20:11 /home/huxiaoming/.bash_history

129797 4 -rw------- 1 huxiaoming huxiaoming 778 Mar 13 10:25 /home/huxiaoming/.viminfo

129796 4 -rw-r--r-- 1 huxiaoming huxiaoming 124 Jul 9 2013 /home/huxiaoming/.bashrc

Grep 命令

查找一些文件内包含的文字内容

[root@lab ~]# grep huxiaoming /etc/passwd

huxiaoming:x:500:500::/home/huxiaoming:/bin/bash

[root@lab ~]# grep -R huxiaoming /home

[root@lab ~]# grep -R huxiaoming /etc 

/etc/group:huxiaoming:x:500:

grep: /etc/alternatives/jre/lib/audio/default.sf2: No such file or directory

grep: /etc/alternatives/jre_openjdk/lib/audio/default.sf2: No such file or directory

grep: /etc/alternatives/jre_1.7.0/lib/audio/default.sf2: No such file or directory

/etc/passwd:huxiaoming:x:500:500::/home/huxiaoming:/bin/bash

/etc/shadow:huxiaoming:$6$v77h3vFZ$8EFht1cShXZ7ORtT3LKX82HRwysnGw9ClJI1NxDPOztAsdOoBVsXcqZ53bTE7GRDv.ZqFVxlAUcKKzwX3doVX0:16141:0:99999:7:::

/etc/gshadow:huxiaoming:!::

/etc/sudoers:User_Alias ADMINS = huxiaoming

/etc/sudoers.tmp:User_Alias ADMINS = huxiaoming

Binary file /etc/.sudoers.tmp.swp matches