Linux常用查找命令的使用——grep、which、find、whereis

Linux的基本查找命令

指令 解释
grep 一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来
find 在目录结构中搜索文件,并对搜索结果执行指定的操作,默认搜索当前目录及其子目录,并且不过滤任何结果
whereis 定位可执行文件、源代码文件、帮助文件在文件系统中的位置。这些文件的属性应属于原始代码,二进制文件,或是帮助文件,whereis会从数据库中查找数据,而不是像find命令那样,通过遍历硬盘来查找
which 在 PATH 变量指定的路径中,搜索某个系统命令的位置, 并且返回第一个搜索结果。使用 which 命令,就可以看到某个系统命令是否存在,以及执行的到底是哪 一个位置的命令。

grep

查找指定服务进程信息

[root@iZ2ze9twtxjrbirmldp9owZ ~]# ps -ef|grep java
root     19354     1  0 Jun21 ?        00:07:10 /software/jdk1.8.0_171/bin/java -jar /software/demo-0.0.1-SNAPSHOT.jar
root     30200 30182  0 16:18 pts/1    00:00:00 grep --color=auto java
[root@iZ2ze9twtxjrbirmldp9owZ ~]# 

查找指定服务进程,排除grep本身

[root@iZ2ze9twtxjrbirmldp9owZ ~]# ps -ef|grep java | grep -v grep
root     19354     1  0 Jun21 ?        00:07:10 /software/jdk1.8.0_171/bin/java -jar /software/demo-0.0.1-SNAPSHOT.jar
[root@iZ2ze9twtxjrbirmldp9owZ ~]# 

查找指定进程个数

[root@iZ2ze9twtxjrbirmldp9owZ ~]# ps -ef|grep java -c
2
[root@iZ2ze9twtxjrbirmldp9owZ ~]# ps -ef|grep java | grep -v grep -c
1
[root@iZ2ze9twtxjrbirmldp9owZ ~]# 

从文件中查找关键词,并显示行号

[root@iZ2ze9twtxjrbirmldp9owZ ~]# cd /software/jdk1.8.0_171/ 
[root@iZ2ze9twtxjrbirmldp9owZ jdk1.8.0_171]# grep -n 'http' THIRDPARTYLICENSEREADME.txt 
191:http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231
304:     http://hg.openjdk.java.net/jdk8u/jdk8u/jdk/file/tip/src/share/native/sun/security/ec/impl
317:     http://www.oracle.com/goto/opensourcecode/request
858:Web: http://www.ecma-international.org
884:AVAILABLE AT http://www.ecma-international.org/memento/codeofconduct.htm FOR
957:the freebxml Software Foundation, please see <http://www.freebxml.org/>.
960:(http://www.apache.org/).
980:        freebxml.org (http://www.freebxml.org/)."
1904:  file, You can obtain one at http://mozilla.org/MPL/2.0/.
1945:      http://www.linuxnet.com (MUSCLE)
1972:See: http://tartarus.org/~martin/PorterStemmer
2039:    and Kohsuke Kawaguchi (http://relaxngcc.sf.net/)."
2338:http://www.unicode.org/Public/, http://www.unicode.org/reports/, and
2339:http://www.unicode.org/cldr/data/. Unicode Data Files do not include PDF
2340:online code charts under the directory http://www.unicode.org/Public/.
2342:the directories http://www.unicode.org/Public/,
2343:http://www.unicode.org/reports/, and http://www.unicode.org/cldr/data/.
2355:Terms of Use in http://www.unicode.org/copyright.html.
2413:               http://wildsau.idv.uni-linz.ac.at/mfx/upx.html
2414:                          http://www.nexus.hu/upx
2415:                            http://upx.tsx.org
2527:The UPX license file is at http://upx.sourceforge.net/upx-license.html.
2559:   software developed by The XFree86 Project, Inc (http://www.xfree86.org/) and
2860:                        http://www.apache.org/licenses/
3052:       http://www.apache.org/licenses/LICENSE-2.0
[root@iZ2ze9twtxjrbirmldp9owZ jdk1.8.0_171]# 

找出以http开头的行内容

[root@iZ2ze9twtxjrbirmldp9owZ jdk1.8.0_171]# cat THIRDPARTYLICENSEREADME.txt |grep ^http
http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231
http://www.unicode.org/Public/, http://www.unicode.org/reports/, and
http://www.unicode.org/cldr/data/. Unicode Data Files do not include PDF
http://www.unicode.org/reports/, and http://www.unicode.org/cldr/data/.
[root@iZ2ze9twtxjrbirmldp9owZ jdk1.8.0_171]# 

显示包含http或者 www的内容行

[root@iZ2ze9twtxjrbirmldp9owZ jdk1.8.0_171]# cat THIRDPARTYLICENSEREADME.txt |grep -E "http|www"
http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231
including specific terms and conditions for packages like libwww, Amaya, and
     http://hg.openjdk.java.net/jdk8u/jdk8u/jdk/file/tip/src/share/native/sun/security/ec/impl
     http://www.oracle.com/goto/opensourcecode/request
Web: http://www.ecma-international.org
AVAILABLE AT http://www.ecma-international.org/memento/codeofconduct.htm FOR
the freebxml Software Foundation, please see <http://www.freebxml.org/>.
(http://www.apache.org/).
        freebxml.org (http://www.freebxml.org/)."
  file, You can obtain one at http://mozilla.org/MPL/2.0/.
      http://www.linuxnet.com (MUSCLE)
See: http://tartarus.org/~martin/PorterStemmer
    and Kohsuke Kawaguchi (http://relaxngcc.sf.net/)."
http://www.unicode.org/Public/, http://www.unicode.org/reports/, and
http://www.unicode.org/cldr/data/. Unicode Data Files do not include PDF
online code charts under the directory http://www.unicode.org/Public/.
the directories http://www.unicode.org/Public/,
http://www.unicode.org/reports/, and http://www.unicode.org/cldr/data/.
Terms of Use in http://www.unicode.org/copyright.html.
               http://wildsau.idv.uni-linz.ac.at/mfx/upx.html
                          http://www.nexus.hu/upx
                            http://upx.tsx.org
The UPX license file is at http://upx.sourceforge.net/upx-license.html.
   software developed by The XFree86 Project, Inc (http://www.xfree86.org/) and
                        http://www.apache.org/licenses/
       http://www.apache.org/licenses/LICENSE-2.0
[root@iZ2ze9twtxjrbirmldp9owZ jdk1.8.0_171]# 

find

查找software目录下权限为777的文件

[root@iZ2ze9twtxjrbirmldp9owZ jdk1.8.0_171]# find /software/ -perm 777
/software/jdk1.8.0_171/man/ja
/software/jdk1.8.0_171/bin/ControlPanel
/software/jdk1.8.0_171/jre/bin/ControlPanel
/software/jdk1.8.0_171/jre/lib/amd64/server/libjsig.so
/software/git-2.17.1/RelNotes
[root@iZ2ze9twtxjrbirmldp9owZ jdk1.8.0_171]# 

查找当前目录大于50M的文件

[root@iZ2ze9twtxjrbirmldp9owZ jdk1.8.0_171]# find . -size +50M 
./jre/lib/rt.jar
./jre/lib/amd64/libjfxwebkit.so
[root@iZ2ze9twtxjrbirmldp9owZ jdk1.8.0_171]#

查找当前所有目录并排序

[root@iZ2ze9twtxjrbirmldp9owZ jdk1.8.0_171]# find . -type d | sort
.
./bin
./db
./db/bin
./db/lib
./include
./include/linux
./jre
./jre/bin
./jre/lib
./jre/lib/amd64
./jre/lib/amd64/jli

查找当目录,以.html结尾的普通文件

[root@iZ2ze9twtxjrbirmldp9owZ jdk1.8.0_171]# find . -type f -name "*.html"
./db/README-JDK.html
./db/RELEASE-NOTES.html
./jre/Welcome.html
./lib/missioncontrol/plugins/org.eclipse.ui.themes_1.0.1.v20140819-1717/about.html
./lib/missioncontrol/plugins/org.eclipse.equinox.launcher.gtk.linux.x86_64_1.1.200.v20141007-2033/about.html
./lib/missioncontrol/features/org.eclipse.ecf.filetransfer.ssl.feature_1.0.0.v20140827-1444/epl-v10.html
./lib/missioncontrol/features/org.eclipse.ecf.filetransfer.ssl.feature_1.0.0.v20140827-1444/license.html

whereis

将和cd文件相关的文件都查找出来

[root@iZ2ze9twtxjrbirmldp9owZ ~]# whereis cd
cd: /usr/bin/cd /usr/share/man/man1/cd.1.gz
[root@iZ2ze9twtxjrbirmldp9owZ ~]# 

which

查找ls、ll、java 命令所在路径

[root@iZ2ze9twtxjrbirmldp9owZ ~]# which ls
alias ls='ls --color=auto'
        /usr/bin/ls
[root@iZ2ze9twtxjrbirmldp9owZ ~]# which ll
alias ll='ls -l --color=auto'
        /usr/bin/ls
[root@iZ2ze9twtxjrbirmldp9owZ ~]# which java
/software/jdk1.8.0_171/bin/java
[root@iZ2ze9twtxjrbirmldp9owZ ~]# 

which是根据使用者所配置的PATH变量内的目录去搜寻可运行档,所以,不同的 PATH 配置内容所找到的命令会不一样。

猜你喜欢

转载自blog.csdn.net/weixin_37490221/article/details/80846115