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

Which #查找固定的目录#

[[email protected] 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)

[[email protected] home]# which ls

alias ls=‘ls --color=auto‘

/bin/ls

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

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

[[email protected] home]# whereis ls

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

[[email protected] home]#

Locate 案例:

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

[[email protected] 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

[[email protected] 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

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

[[email protected] huxiaoming]# updatedb   更新目录树

[[email protected] huxiaoming]# locate 2.txt

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

[[email protected] 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 命令---在当前目录查找

[[email protected] huxiaoming]# find 1.txt

1.txt

[[email protected] huxiaoming]# cd /

[[email protected] /]# find 1.txt

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

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

[[email protected] /]# find / -name 1.txt

/home/huxiaoming/1.txt

正则表达式 一些find技巧

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

find /etc/ -name "*ifc*"

[[email protected] /]# find /etc -name "*ifcfg-eth0*" -exec vi {} \;

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

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

还有一些高级的技巧

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

[[email protected] /]# find /home -name "*.txt*" -ok rm {} \;

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

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

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

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

/home/huxiaoming/2.txt

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

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

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

[[email protected] /]# 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 命令

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

[[email protected] ~]# grep huxiaoming /etc/passwd

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

[[email protected] ~]# grep -R huxiaoming /home

[[email protected] ~]# 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

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

时间: 2024-08-27 18:50:58

文件查找 /which/whereis/locate/find/grep的相关文章

Linux学习总结(十五)文件查找 which whereis locate find

which命令 用于查找并显示给定命令的绝对路径,环境变量PATH中保存了查找命令时需要遍历的目录.which指令会在环境变量$PATH设置的目录里查找符合条件的文件.也就是说,使用which命令,就可以看到某个系统命令是否存在,以及执行的到底是哪一个位置的命令.格式 which + 命令 例如 which ls whereis命令 用来定位指令的二进制程序.源代码文件和man手册页等相关文件的路径.whereis命令只能用于程序名的搜索,而且只搜索二进制文件(参数-b).man说明文件(参数-

Linux文件查找之find&locate

Linux文件查找之find&locate 一.概述 Linux系统核心的思想之一"一切皆文件",对于这么多的文件,如何快速查找过滤呢?下面我们就看看系统提供的文件查找命令find和locat,熟练使用find命令是运维人员的必经之路 二.find的用法及示例 1.find特点 查找速度略慢 精确查找 实时查找 只能搜索有读取和执行权限的目录 2.find用法 用法:find  [options]  [查找路径]  [ 查找条件]  [处理动作] 查找条件: 根据文件类型查找

linux文件名称查找which,whereis,locate

1. 文件名称查找 使用find查询时.因为磁盘查询.所以速度较慢. 所以linux下查询更常使用which, whereis, locate来查询,因为是利用数据库查询.所以速度非常快. 2. which 我们经常使用的ls命令.假设我们想知道这些经常使用命令放在哪儿. 就使用which命令来查询地址. which使用PATH 环境变量去查找文件名称, which -a 返回全部的查到的命令, 默认返回第一条. $ which grep /bin/grep $ which ll alias l

linux文件名查找which,whereis,locate

1. 文件名查找 使用find查询时,由于磁盘查询,所以速度较慢, 所以linux下查询更常使用which, whereis, locate来查询,由于是利用数据库查询,所以速度很快. 2. which 我们常用的ls命令,如果我们想知道这些常用命令放在哪儿, 就使用which命令来查询地址. which使用PATH 环境变量去查找文件名, which -a 返回所有的查到的命令, 默认返回第一条. $ which grep /bin/grep $ which ll alias ll='ls -

Linux 入门之文件查找(find、locate)命令

Linux入门之Find文件查找命令 在liunx中有很多文件查找工具,但是最常用的却是locate和find命令,他们都有多种条件进行文件搜索,当然各自也有不同的特点. locate 命令: 原理:此命令查询文件的准确度依赖于系统上预建的文件索引数据库文件: /var/lib/mlocate/mlocate.db [[email protected] ~]# ls -l /var/lib/mlocate/mlocate.db  -rw-r-----. 1 root slocate 213573

linux文件查找(find,locate)

文件查找: locate: 非实时,模糊匹配,查找是根据全系统文件数据库进行的: # updatedb, 手动生成文件数据库 速度快 find: 实时 精确 支持众多查找标准 遍历指定目录中的所有文件完成查找,速度慢: find 查找路径 查找标准 查找到以后的处理运作 查找路径:默认为当前目录 查找标准:默认为指定路径下的所有文件 处理运作:默认为显示 匹配标准: -name 'FILENAME':对文件名作精确匹配 文件名通配: *:任意长度的任意字符 ? [] -iname 'FILENA

Linux文件查找find和locate

目 录 第1章 locate文件查找    1 1.1 概述    1 1.2 locate文件查找的特性    1 第2章 文件查找概述    1 第3章    1 3.1 文件名查找    1 3.2 文件大小查找    1 3.3 时间戳查找    1 3.4 文件从属关系查找    1 3.5 文件类型查找    1 3.6 权限查找    1 3.7 组合查找    1 3.8 处理动作    1 locate文件查找 概述 安装 yum install -y mlocate loca

文件查找与压缩 locate find

1 locate 查找文件 使用updatedb 建立mlocate.db 整个磁盘文件搜索建立索引 影响磁盘性能.locate -i 不区分大小写locate -n 只列出前n个locate -r " .(jpg|png)$" 查找以jpg或png结尾的文件 支持j基本的正则表达式 2 find 实时搜索 默认递归搜索 find [OPTION] ...[查找路径] [查找条件][处理动作] find /etc -name passwd ---find +l路径 + 指定文件名 [2

文件查找工具locate与find

写在前面: 博客书写牢记5W1H法则:What,Why,When,Where,Who,How. 本篇主要内容: ● locate用法 ● 使用find基于文件属性进行查找 locate与find简介: locate与find都是文件查找工具(区别与grep等文本查找工具),尤其是find,可根据文件的诸多属性(如文件大小.属主属组.权限信息.修改时间等),对文件进行实时查找 locate: 1.查找默认数据库文件/var/lib/mlocate/mlocate.db,所以查找速度快,非实时,每天