猜猜我在哪里-linux查找命令

find:         # 这是一个很强大的查找命令
findfs:       # 依据卷标(LABEL)和UUID查找文件系统所对应的设备文件
locate:       # 是find -name的另一种写法,但是要比后者快得多
slocate:      # 与locate类似
which:        # 在PATH变量指定的路径中,搜索某个系统命令的位置,并且返回第一个搜索结果
whereis:      # 定位命令的二进制程序、源代码文件和man手册页等相关文件的路径
pwd:          # 显示当前工作目录的绝对路径
pwdx:         # 通过pid找程序所在目录的绝对路径

find

语法格式:
find <参数> <路径> <查找和搜索范围>

常用参数:
-name:     # 按照名称查找
-iname:    # 同上,但是不区分大小写
-size:     # 按照大小查找
-user:     # 按照所属用户查找
-type:     # 按照类型查找

实例:
KnowledgeIsPower:~ # find /etc/ -name "*.service"    # 查找/etc目录下所有的.service后缀的文件
/etc/zypp/services.d/opensuse-non-oss.service
..........   # 有很多,就忽略了
KnowledgeIsPower:~ # find /etc/ -size -1M            # 查找/etc目录下所有小于1M的文件
/etc/skel/.bash_history
..........   # 有很多,就忽略了
KnowledgeIsPower:~ # find . -type f -atime +7        # 查找当前目录下,超过七天内呗访问过的所有文件
./.gnupg/secring.gpg
..........   # 有很多,就忽略了
KnowledgeIsPower:~ # find . -type f -amin -10        # 查找当前目录下,十分钟内被访问过的所有文件
./.Xauthority

进阶用法:
'find配合-exec可以使一些操作一步到位'
'-exec后面可以加上一些操作,比如cp,mv等等;{
    
    } 这里需要填写操作后的路径,如果是rm可以为空 \;'
1: 创建了一个测试环境,目录结构如下
test/
├── new
└── old
    └── test.txt
2: KnowledgeIsPower:~ # find test/old -name test.txt -exec mv {} test/new/ \;     # 查找test/old目录下的test.txt,然后将他复制到test/new目录下
test
├── new
│   └── test.txt     # 操作成功
└── old
3: KnowledgeIsPower:~ # find test/ -name test.txt -exec rm -f {} \;    # 查找test目录下的test.txt文件,并将他删除
test/
├── new             # 成功删除
└── old
'find 配合 exec可以达到很多的效果,可以自己尝试,但是如果要用到rm和mv的时候,一定要注意自己的查找目录和操作目录,mv命令还能拯救,但是rm命令的操作是不可逆的,不要错删文件,一定要慎重'

findfs

语法格式:
findfs <参数>

实例:
KnowledgeIsPower:~ # findfs UUID=7f8d169e-5356-4ac2-b019-76369f24ceff
/dev/sda2
KnowledgeIsPower:~ # findfs LABEL="SLE-12-SP3-Server-DVD-x86_640473"
/dev/sr0

扩展:
'UUID和LABEL怎么来的?让我来告诉你'
KnowledgeIsPower:~ # blkid -s UUID
KnowledgeIsPower:~ # blkid -s LABEL
'其实,这俩命令查找出来的内容是一样的,小问号,你是不是有好多朋友'

locate

语法格式:
locate <参数> <文件>

实例:
KnowledgeIsPower:~ # locate /etc/lib    # 查询/etc目录下以lb开头的文件和目录
/etc/libaudit.conf
/etc/libnl
/etc/libuser.conf
/etc/libnl/classid
/etc/libnl/pktloc
KnowledgeIsPower:~ # locate locate      # 查找和locate相关的所有文件,内容比较长,就省略一部分了
/etc/cron.daily/mlocate
.........
/var/lib/mlocate/mlocate.db.HFErma

扩展:
'suse发行版没有自带locate命令,貌似需要yast的方式才能安装,就不尝试了,其实find好用,就是命令长度比locate要长'
'centos发行版也是不自带locate命令,只需要yum -y install mlocate即可,是base源自带的,第一次使用的时候,需要先updatedb,更新一下locate的库才可以查询,建议使用locate之前,都先updatedb一下'

slocate

语法格式:
sloctae <参数> <目录>

常用参数:
-d:     # 指定数据库所在的目录
-u:     # 更新slocate数据库

实例: slocate fdisk # 显示文件名中含有fdisk 关键字的文件的路径信息  
/root/cfdisk        
/root/fdisk  
/root/sfdisk  
/usr/include/grub/ieee1275/ofdisk.h  
/usr/share/doc/util-Linux/README.cfdisk  
/usr/share/doc/util-Linux/README.fdisk.gz  
/usr/share/doc/util-Linux/examples/sfdisk.examples.gz  

扩展:
'slocate貌似是fedora发行版才会有的命令,suse和centos这两个发行版,自身的源是不带slocate这个命令的,本菜13平时基本上还是用find偏多'

which

语法格式:
which <参数> <文件>

实例:
KnowledgeIsPower:~ # which ps
/usr/bin/ps
KnowledgeIsPower:~ # which cd     # 因为cd是bash内建命令,which只能去查找PATH变量的目录
which: no cd in (/sbin:/usr/sbin:/usr/local/sbin:/root/bin:/usr/local/bin:/usr/bin:/bin:/usr/games)

扩展:
KnowledgeIsPower:~ # echo $PATH
/sbin:/usr/sbin:/usr/local/sbin:/root/bin:/usr/local/bin:/usr/bin:/bin:/usr/games
'可以看到,PATH变量包含的目录,和终端回显的目录是一致的,这个是which的一个局限性'

whereis

语法格式:
whereis <参数> <命令名>

常用参数:
-b:          # 查找二进制程序或者命令
-B:          # 从指定目录下查找二进制程序或者命令
-m:          # 查找man手册文件
-M:          # 从指定目录下,查找man手册文件
-s:          # 只查找源代码文件
-S:          # 从指定目录下,查找源代码文件

实例: 
KnowledgeIsPower:~ # whereis pwd       # 查找pwd所在的目录
pwd: /usr/bin/pwd /bin/pwd /usr/include/pwd.h /usr/share/man/man1/pwd.1.gz /usr/share/man/man1p/pwd.1p.gz /usr/share/man/mann/pwd.n.gz
KnowledgeIsPower:~ # whereis -b pwd    # 查找pwd二进制程序或者命令所在的目录
pwd: /usr/bin/pwd /bin/pwd /usr/include/pwd.h
KnowledgeIsPower:~ # whereis -m pwd    # 查找pwd的man手册文件
pwd: /usr/share/man/man1/pwd.1.gz /usr/share/man/man1p/pwd.1p.gz /usr/share/man/mann/pwd.n.gz

pwd

语法格式:
pwd

实例:
KnowledgeIsPower:/usr/local # pwd
/usr/local

扩展:
$(cd `dirname $0`; pwd)     '这条命令只有在shell脚本中才会生效'
解释:
dirname $0       # 取得当前执行的脚本文件的父目录
'所以,这条命令的意思就是切换到当前执行的脚本文件所在的父目录,并回显到终端'

pwdx

语法格式:
pwdx <pid号>

实例:
KnowledgeIsPower:~ # pwdx 1482
1482: /var/spool/postfix

猜你喜欢

转载自blog.csdn.net/u010383467/article/details/111194385