Linux系统配置及服务管理_文件查找

本文主要介绍了在Linux系统中进行文件查找的方法,包括使用`which`、`find`和`locate`命令。详细讲解了`find`命令的用法,如按文件名字、大小、目录深度、文件类型和权限进行查找,并且提到了找到文件后可以执行的动作,如显示、删除以及结合`tar`进行压缩和解压操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、文件查找
1.which :命令查找

[root@localhost ~]# 
[root@localhost ~]# which ls
alias ls='ls --color=auto'
	/usr/bin/ls
[root@localhost ~]# which ll
alias ll='ls -l --color=auto'
	/usr/bin/ls
[root@localhost ~]# which cat
/usr/bin/cat
[root@localhost ~]# which find
/usr/bin/find
[root@localhost ~]# 

2.find: 文件查找,针对文件名
3.locate:文件查找,依赖数据库
*查找任意文件(find命令)
语法:find [path…] [options] [expression] [action]
命令 路径 选项 表达式 动作
1.按文件名字查找

[root@localhost ~]# find /etc -name "hosts"
/etc/hosts
[root@localhost ~]# find /etc -iname "HoSts"
/etc/hosts
[root@localhost ~]# find /etc -name "hos*"
/etc/selinux/targeted/active/modules/100/hostname
/etc/host.conf
/etc/hosts
/etc/hosts.allow
/etc/hosts.deny
/etc/hostname
[root@localhost ~]# 

-i忽略大小写
2.按文件大小:

[root@localhost ~]# find /etc -size  +5M
/etc/udev/hwdb.bin
[root@localhost ~]# find /etc -size  =5M
find: Invalid argument `=5M' to -size
[root@localhost ~]# 

文件大于5M,用+5M ,小于5M用-5M
3.指定查找的目录深度:

[root@localhost ~]# find / -maxdepth 4 -a -name "ifcfg-en*"
/etc/sysconfig/network-scripts/ifcfg-ens33

[root@localhost ~]# find / -maxdepth 4 -a -name "ifcfg-en*"
/etc/sysconfig/network-scripts/ifcfg-ens33
[root@localhost ~]# find / -maxdepth 3 -a -name "ifcfg-en*"
[root@localhost ~]# find / -maxdepth 3 -a -name "ifcfg-en*"
`
ifcfg-en在第四层目录,在第三层是查不到的
4按文件属主、属组找:

```powershell
[root@localhost ~]#  find      /home     -user       jack
/home/jack
/home/jack/.bash_logout
/home/jack/.bash_profile
/home/jack/.bashrc
/home/jack/1.txt
/home/jack/2.txt
/home/jack/3.txt
/home/jack/4.txt
/home/jack/5.txt
/home/jack/.bash_history
[root@localhost ~]# 

属主

[root@localhost ~]#  find      /home     -user       jack
/home/jack
/home/jack/.bash_logout
/home/jack/.bash_profile
/home/jack/.bashrc
/home/jack/1.txt
/home/jack/2.txt
/home/jack/3.txt
/home/jack/4.txt
/home/jack/5.txt
/home/jack/.bash_history

属组

[root@localhost ~]# find      /home     -group      hr
[root@localhost ~]# chmod :hr /home/jack/1.txt 
chmod: invalid mode: ‘:hr’
Try 'chmod --help' for more information.
[root@localhost ~]# chown :hr /home/jack/1.txt 
[root@localhost ~]# find      /home     -group      hr
/home/jack/1.txt

5.按文件类型

[root@localhost ~]# find /tmp -type f
/tmp/yum.log
/tmp/ks-script-1FBVzT
/tmp/CentOS-Base.repo
/tmp/CentOS-CR.repo
/tmp/CentOS-Debuginfo.repo
/tmp/CentOS-fasttrack.repo
/tmp/CentOS-Media.repo
/tmp/CentOS-Sources.repo
/tmp/CentOS-Vault.repo
/tmp/CentOS-x86_64-kernel.repo

[root@localhost ~]# find /dev -type b
/dev/sdb1
/dev/dm-1
/dev/dm-0
/dev/sr0
/dev/sde
/dev/sdf
/dev/sdd
/dev/sdc
/dev/sdb
/dev/sda2
/dev/sda1
/dev/sda
[root@localhost ~]# 

其他
f普通文件
b块设备文件
d目录
p管道
l连接
6.按文件权限:

root@localhost ~]# find . -perm 644 -ls
34130991    4 -rw-r--r--   1 root     root           18 Dec 29  2013 ./.bash_logout
34130992    4 -rw-r--r--   1 root     root          176 Dec 29  2013 ./.bash_profile
34130993    4 -rw-r--r--   1 root     root          176 Dec 29  2013 ./.bashrc
34130994    4 -rw-r--r--   1 root     root          100 Dec 29  2013 ./.cshrc
34130995    4 -rw-r--r--   1 root     root          129 Dec 29  2013 ./.tcshrc
33574996    0 -rw-r--r--   1 root     root            0 Aug  4 09:41 ./123.txt
51281340    4 -rw-r--r--   1 root     root           26 Aug  4 10:22 ./dir4/dir5/123.txt
33575006    0 -rw-r--r--   1 root     root            0 Aug  4 10:00 ./2.txt
33575007    0 -rw-r--r--   1 root     root            0 Aug  4 10:00 ./3.txt
33575008    0 -rw-r--r--   1 root     root            0 Aug  4 10:00 ./4.txt
33575009    0 -rw-r--r--   1 root     root            0 Aug  4 10:00 ./5.txt
33575039 29052 -rw-r--r--   1 root     root     29747200 Aug  4 10:50 ./etc.tar
51283477    4 -rw-r--r--   1 root     root          465 Jul 30 20:24 ./etc/fstab
51283480    4 -rw-r--r--   1 root     root           73 Aug  4 09:08 ./etc/resolv.conf
   807    4 -rw-r--r--   1 root     root          483 Apr  4 01:24 ./etc/grub.d/README
33683750    4 -rw-r--r--   1 root     root         1690 Apr  8 06:01 ./etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
33683751    4 -rw-r--r--   1 root     root         1004 Apr  8 06:01 ./etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-Debug-7
33683752    4 -rw-r--r--   1 root     root         1690 Apr  8 06:01 ./etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-Testing-7
51283482    4 -rw-r--r--   1 root     root          166 Nov 27  2019 ./etc/pki/ca-trust/README
51283483    4 -rw-r--r--   1 root     root          980 Nov 27  2019 ./etc/pki/ca-trust/ca-legacy.conf
   810    4 -rw-r--r--   1 root     root          560 Nov 27  2019 ./etc/pki/ca-trust/extracted/README
16777723    4 -rw-r--r--   1 root     root          726 Nov 27  2019 ./etc/pki/ca-trust/extracted/java/README
33683754    4 -rw-r--r--   1 root     root          787 Nov 27  2019 ./etc/pki/ca-trust/extracted/openssl/README
51283485    4 -rw-r--r--   1 root     root          898 Nov 27  2019 ./etc/pki/ca-trust/extracted/pem/README
   812    4 -rw-r--r--   1 root     root          932 Nov 27  2019 ./etc/pki/ca-trust/source/README
16778199    4 -rw-r--r--   1 root     root         2516 Aug  9  2019 ./etc/pki/tls/certs/Makefile
   816   12 -rw-r--r--   1 root     root        10923 Aug  6  2019 ./etc/pki/tls/openssl.cnf
16784259    4 -rw-r--r--   1 root     root          257 Nov 28  2019 ./etc/pki/nss-legacy/nss-rhel7.config
33683765   64 -rw-r--r--   1 root     root        65536 Dec 11  2019 ./etc/pki/nssdb/cert8.db
33683766   12 -rw-r--r--   1 root     root         9216 Dec 11  2019 ./etc/pki/nssdb/cert9.db
33683767   16 -rw-r--r--   1 root     root        16384 Dec 11  2019 ./etc/pki/nssdb/key3.db
33683768   12 -rw-r--r--   1 root     root        11264 Dec 11  2019 ./etc/pki/nssdb/key4.db
33683769    4 -rw-r--r--   1 root     root          451 Nov 28  2019 ./etc/pki/nssdb/pkcs11.txt

-ls 是find的动作之一,精确权限
7.找到后处理的动作 ACTIONS:
(1)找到后默认是显示文件
短格式

[root@localhost ~]#  find      /home     -user       jack -print
/home/jack
/home/jack/.bash_logout
/home/jack/.bash_profile
/home/jack/.bashrc
/home/jack/1.txt
/home/jack/2.txt
/home/jack/3.txt
/home/jack/4.txt
/home/jack/5.txt
/home/jack/.bash_history
[root@localhost ~]# 

长格式

[root@localhost ~]#  find      /home     -user       jack -ls
  7945    0 drwx------   2 jack     jack          148 Aug  4 10:01 /home/jack
  7951    4 -rw-r--r--   1 jack     jack           18 Apr  1 10:17 /home/jack/.bash_logout
  7955    4 -rw-r--r--   1 jack     jack          193 Apr  1 10:17 /home/jack/.bash_profile
  7964    4 -rw-r--r--   1 jack     jack          231 Apr  1 10:17 /home/jack/.bashrc
  7966    0 -rw-rw-r--   1 jack     hr              0 Aug  4 10:01 /home/jack/1.txt
  7967    0 -rw-rw-r--   1 jack     jack            0 Aug  4 10:01 /home/jack/2.txt
  7968    0 -rw-rw-r--   1 jack     jack            0 Aug  4 10:01 /home/jack/3.txt
  7969    0 -rw-rw-r--   1 jack     jack            0 Aug  4 10:01 /home/jack/4.txt
  7970    0 -rw-rw-r--   1 jack     jack            0 Aug  4 10:01 /home/jack/5.txt
  7971    4 -rw-------   1 jack     jack           25 Aug  4 10:01 /home/jack/.bash_history
[root@localhost ~]# 

找到后删除

[root@localhost ~]# find      /home     -group      hr -delete
[root@localhost ~]# find      /home     -group      hr 

`找到后复制
`[root@localhost ~]# find     /etc     -name       "ifcfg*"      -ok     cp     -rvf   {}    /tmp    \;
< cp ... /etc/sysconfig/network-scripts/ifcfg-lo > ? y
‘/etc/sysconfig/network-scripts/ifcfg-lo’ -> ‘/tmp/ifcfg-lo’
< cp ... /etc/sysconfig/network-scripts/ifcfg-ens33 > ? y
‘/etc/sysconfig/network-scripts/ifcfg-ens33’ -> ‘/tmp/ifcfg-ens33’
[root@localhost ~]# y
-bash: y: command not found
[root@localhost ~]# ls /tmp
CentOS-Base.repo           ifcfg-ens33
CentOS-CR.repo             ifcfg-lo
CentOS-Debuginfo.repo      ks-script-1FBVzT
CentOS-fasttrack.repo      systemd-private-8579d1c3e9c042caae1f87fec8a3c362-systemd-hostnamed.service-icDK81
CentOS-Media.repo          vmware-root_666-2731021219
CentOS-Sources.repo        vmware-root_682-2697467275
CentOS-Vault.repo          vmware-root_698-2730496923
CentOS-x86_64-kernel.repo  yum.log
[root@localhost ~]# 
`
(二)文件打包及压缩
简介:tar命令是Unix/Linux系统中备份文件的可靠方法,
几乎可以工作于任何环境中,它的使用权限是所有用户。
建议针对目录
语法:tar  选项  压缩包名称  源文件
打包、压缩

```powershell
[root@localhost ~]# tar   -cf         etc.tar          /etc
tar: Removing leading `/' from member names
[root@localhost ~]# ls
123.txt  1.txt  2.txt  3.txt  4.txt  5.txt  anaconda-ks.cfg  dir1  dir2  dir3  dir4  etc.tar  file1
[root@localhost ~]# tar   -xf   etc.tar
[root@localhost ~]# ls
123.txt  1.txt  2.txt  3.txt  4.txt  5.txt  anaconda-ks.cfg  dir1  dir2  dir3  dir4  etc  etc.tar  file1
[root@localhost ~]# 

#tar -czvf etc-gzip.tar.gz /etc/ /z是gzip
#tar -cjf etc-bzip.tar.bz /etc/ /j是bzip
#tar -cJf etc-xzip.tar.xz /etc/ ///J是xzip

观察三个包的体积。

[root@localhost ~]# tar   -cJf         etc-xzip.tar.xz        /etc/
tar: Removing leading `/' from member names
[root@localhost ~]#  ll -h etc*
-rw-r--r--.  1 root root 9.9M Aug  4 18:50 etc-gzip.tar.gz
-rw-r--r--.  1 root root  29M Aug  4 18:47 etc.tar
-rw-r--r--.  1 root root 7.1M Aug  4 18:52 etc-xzip.tar.xz

2.解压、解包
查看,并没有解压
tar -tf etc.tar

#tar -xvf etc-xzip.tar.xz

[root@localhost ~]# ls
123.txt  2.txt  4.txt  anaconda-ks.cfg  dir2  dir4  etc-gzip.tar.gz  etc-xzip.tar.xz
1.txt    3.txt  5.txt  dir1             dir3  etc   etc.tar          file1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值