目录
一.查找系统命令的位置-which
判断某个系统命令是否存在,并且找到位置
which是通过 PATH环境变量到该路径内查找可执行文件,所以本质是寻找可执行文件,并返回第一个搜索结果
# gr @ gr-System-Product-Name in ~ [16:56:36]
$ echo $path
/home/gr/.local/bin /usr/local/sbin /usr/local/bin /usr/sbin /usr/bin /sbin /bin /usr/games /usr/local/games /snap/bin /opt/qt/5.15.2/gcc_64/bin /usr/local/cuda-11.1/bin /usr/local/TensorRT7/bin /opt/v2ray /home/gr/.fzf/bin
# gr @ gr-System-Product-Name in ~ [17:06:26]
$ which mysql
/usr/bin/mysql
# gr @ gr-System-Product-Name in ~ [17:06:35]
$ which cuda
cuda not found
# gr @ gr-System-Product-Name in ~ [17:06:40] C:1
$ which snap
/usr/bin/snap
二.快速查找程序-whereis
用于程序名的搜索
定位指令的二进制程序(binary)、源代码文件(source)和man手册页
都属于普通文件,ls -al 中,首字符是"-"
和find相比,whereis查找的速度非常快,这是因为linux系统会将系统内的所有文件都记录在一个数据库文件中,当使用whereis和locate时,会从数据库中查找数据,而不是像find命令那样,通过遍历硬盘来查找,效率自然会很高。
但是,该数据库文件并不是实时更新,默认情况下时一星期更新一次,因此,我们在用whereis和locate 查找文件时,有时会找到已经被删除的数据,或者刚刚建立文件,却无法查找到,原因就是因为数据库文件没有被更新。
# gr @ gr-System-Product-Name in ~ [17:49:23]
$ whereis mysql
mysql: /usr/bin/mysql /usr/lib/mysql /etc/mysql /usr/share/man/man1/mysql.1.gz
# gr @ gr-System-Product-Name in ~ [17:50:58]
$ whereis cuda
cuda: /usr/local/cuda
# gr @ gr-System-Product-Name in ~ [17:51:03]
$ whereis find
find: /usr/bin/find /usr/share/man/man1/find.1.gz /usr/share/info/find.info.gz /usr/share/info/find.info-2.gz /usr/share/info/find.info-1.gz
三.快速查找文件-locate
根据文件名进行查找
locate命令可以在搜寻数据库时快速找到档案,数据库由updatedb程序来更新,updatedb是由cron daemon周期性建立的.在内定值中,updatedb每天会跑一次,可以手动更新updatedb来更准确的运行locate命令。
# gr @ gr-System-Product-Name in ~ [9:00:13]
$ locate boost
/home/gr/.local/share/Trash/files/clang-14.0.0.src/test/Frontend/Inputs/SystemHeaderPrefix/libs/boost
/home/gr/.local/share/Trash/files/clang-14.0.0.src/test/Frontend/Inputs/SystemHeaderPrefix/libs/boost/all.h
/home/gr/.local/share/Trash/files/clang-14.0.0.src/test/Frontend/Inputs/SystemHeaderPrefix/libs/boost/warn.h
/opt/nvidia/nsight-compute/2023.1.0/host/linux-desktop-glibc_2_11_3-x64/libboost_atomic.so.1.78.0
/opt/nvidia/nsight-compute/2023.1.0/host/linux-desktop-glibc_2_11_3-x64/libboost_chrono.so.1.78.0
...
四.从硬盘查找文件-find
查找符合条件的文件、目录
find不常用,因为find直接去硬盘内查找文件,速度慢,而且影响硬盘的性能
但是find的功能很强大
五.查找文件中符合条件的字符串-grep
创建文件
# gr @ gr-System-Product-Name in ~/Documents [9:11:01]
$ cat > file01.txt
12345
qwert
abcde%
追加内容
# gr @ gr-System-Product-Name in ~/Documents [9:11:51]
$ cat >> file01.txt
asdffg
ahb342%
查找含有字符"a"的内容,并显示行数
# gr @ gr-System-Product-Name in ~/Documents [9:14:29]
$ grep -n "a" file01.txt
3:abcdeasdffg
4:ahb342
本质是过滤
将查找结果过滤,只显示含有字符"statechart"的内容
# gr @ gr-System-Product-Name in ~/Documents [9:21:27]
$ locate statechart
/opt/qt/5.15.2/Src/qtbase/doc/src/images/rogue-statechart.png
/opt/qt/5.15.2/Src/qtscxml/examples/scxml/ftpclient/doc/images/ftpclient-statechart.png
/opt/qt/5.15.2/Src/qtscxml/examples/scxml/pinball/doc/images/pinball-statechart-global.png
/opt/qt/Examples/Qt-6.3.1/scxml/pinball/doc/images/pinball-statechart-onstate.png
/opt/qt/Examples/Qt-6.3.1/scxml/pinball/doc/images/pinball-statechart-workflow.png
/usr/local/include/boost/statechart
/usr/local/include/boost/statechart/asynchronous_state_machine.hpp
/usr/local/include/boost/statechart/custom_reaction.hpp
# gr @ gr-System-Product-Name in ~/Documents [9:23:02]
$ locate statechart | grep 'statechart/'
/usr/local/include/boost/statechart/asynchronous_state_machine.hpp
/usr/local/include/boost/statechart/custom_reaction.hpp
/usr/local/include/boost/statechart/deep_history.hpp
/usr/local/include/boost/statechart/deferral.hpp