以下是正文:
指令与文件内容搜索
一:指令文件名搜索
1、which
(寻找“可执行文件”)
which -[a] command
-a :就是在PATH路径路径目录下能找的命令都列出来。同样这也是限制。
例如:
linux@linux-virtual-machine:~/gitLearn$ which ls
/bin/ls
linux@linux-virtual-machine:~/gitLearn$ which which
/usr/bin/which
2,type
(查询指令是否为BASH shell的内置命令)
比如:cd 、umask就是内置在bash中
使用如下:
type [-tpa] name
不加参数时 会显示是内置的或外部的。
-t :会将name 以下显示意义
file :表外部指令
alias :表指令为命令别名所设置的名称
builtin :表命令为bash内置指令功能
-p :接的name 为外部指令时,才显示完整文件名
-a :会由PATH的路径下列出含name 的指令
优点:能够解除受环境PATH的影响
二:文件文件名的搜索
基本使用whereis 、locate 或者find 。
优先使用前两个,因为find比较操磁盘并且慢但准确也很强大。
1、whereis
(由一些特定目录中寻找文件文件名)
使用说明:
whereis [-bmsu] 文件或目录名
OPTIONS
-b Search only for binaries.
-m (只找说明文档manual下的文件)Search only for manual sections.
-s Search only for sources.
-u 搜索不再以上三个项目的其他特殊文件
whereis快是应为在几个特定的目录下查找,如:/bin/sbin;/usr/share/man下的man page文件等
书上写用whereis -l可以 看看有在那些目录下查找。但我试了试,不行啊^_^,直接看手册吧!
2、locate/udatedb
locate比较快,是因为数据是从数据库/var/lib/mlocate中搜索的。
所以使用要使用“udatedb”命令来调用/etc/updatedb.conf的设置来搜索硬盘中文件名来更新/var/lib/mlocate数据库
使用说明:
locate [OPTION]... PATTERN...
OPTIONS
-A, --all
Print only entries that match all PATTERNs instead of requiring
only one of them to match.
-b, --basename
Match only the base name against the specified patterns. This
is the opposite of --wholename.
-c, --不输出文件名,仅仅输出数量
-d, --database DBPATH
Replace the default database with DBPATH. DBPATH is a :-sepa‐
rated list of database file names. If more than one --database
option is specified, the resulting path is a concatenation of
the separate paths.
An empty database file name is replaced by the default data‐
base. A database file name - refers to the standard input.
Note that a database can be read from the standard input only
once.
-e, --existing
Print only entries that refer to files existing at the time
locate is run.
(注:如果没有及时的更新数据库,就会出现当前删掉的文件以也可以找出来(对了,有时候会一不注意rm 某个文件,是不是可以从数据库中还原出来?试试!)或者说新添文件却找不到的情况)
3、find
find [PATH] [option] [action]
根据option选项参数可以分为以下几大类:
3.1 :时间
-mtime,-atime,-ctime
例如:
-mtime n :n天前“一天内”变动的文件
-mtime +n: n天之前“不含第n天”变动的文件
-mtime -n :n天之后“含第n天”变动的文件
3.2:群组、用户等
uid,gid,nouser等
-uid n :n = UID;
-gid n : n = GID;
-user name : name 使用者的名字
-nouser :寻找文件拥有者不存在/etc/passwd的人
…
3.3:与文件权限有关
-name filename :收缩文件名称为“filename”的文件
-size [±]size :搜索比size大或小的文件
-type TYPE :搜索文件类型为f b c d l s p 等属性
-perm mode :搜索权限刚好是mode的文件
-perm -mode:搜索权限囊括mode的文件(比如:搜0744时4755也会搜出来)
-perm /mode :搜索包含任一mode权限的文件
(比如:搜-rwxr-xr-x时-rw-------也会搜出来)
OK!
搭配 “管道” 食用,效果更佳!