Linux 基本命令(三) which-whereis-locate-stat-find

1、which命令

which命令去Linux系统里面查找命令的时候,会固定的到某些文件夹里去寻找 (PATH变量指定的路径)
如何将自己的文件变成可执行文件?

  • 改变自己
    将可执行文件放到环境变量的路径下
  • 改变世界
    将可执行文件的路径加到环境变量中 PATH=/sc:$PATH (临时修改,只对当前终端有效 。修改/etc/profile 文件 在第一行添加就会永久有效,对所有用户都有效)
[root@localhost sc]# PATH=/sc:$PATH   shell编程里=左右不能有空格
[root@localhost sc]# echo $PATH
/sc:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@localhost lianxi]# env |grep PATH
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

引申:rpm命令

# 查看bash存放在磁盘的哪个位置
[root@192 ~]# which bash
/usr/bin/bash  


# -qf 查询文件的选项 query file
# 查看/usr/bin/bash文件是通过哪些rpm包安装过来的。
[root@wangxinzhou boot]# rpm -qf /usr/bin/bash 
bash-4.4.19-12.el8.x86_64


# -ql  查询列出内容  query list
# 查询bash这个软件安装产生了哪些文件,都存放在哪里
# 查询某个已经安装的包相关信息,到底是干什么用的 
[root@wangxinzhou boot]# rpm -ql bash  

# -qa
# 查询当前linux系统已经安装的所有软件
[root@localhost ~]#  rpm -qa |grep mysql
mysql-community-libs-5.6.51-2.el7.x86_64
mysql-community-server-5.6.51-2.el7.x86_64
mysql-community-release-el7-5.noarch
mysql-community-client-5.6.51-2.el7.x86_64
mha4mysql-manager-0.58-0.el7.centos.noarch
php-mysql-5.4.16-48.el7.x86_64
mha4mysql-node-0.58-0.el7.centos.noarch
mysql-community-common-5.6.51-2.el7.x86_64

2、whereis 命令

查询命令存放的路径,同时告诉我们man手册的路径

  • whereis的查询范围也是path 变量的指定范围吗?
    是的,也是到PATHl里面找( and in the places specified by $PATH and $MAN‐PATH)
[cs@localhost ~]$ whereis mkdir
mkdir: /usr/bin/mkdir /usr/share/man/man1/mkdir.1.gz

3、locate 命令

  • 如何知道一个命令是通过那个软件包安装过来的?
    软件包的名字和命令的名字有时候不一样,BaseOs 这个仓库里存放的都是centos 系统基础软件
    locate命令是到数据库文件(/var/lib/mlocate/mlocate.db)里查找文件的,第一次使用需要updatedb更新才能产生数据库。
    updatedb 会帮助我们创建mlocate.db数据库文件
[root@localhost ~]# yum provides locate
上次元数据过期检查:1:18:16 前,执行于 20211020日 星期三 095052秒。
**mlocate**-0.26-20.el8.x86_64 : An utility for finding files by name(根据文件名来查找文件的工具)
仓库        :baseos
匹配来源:
文件名    :/usr/bin/locate

[root@sc ~]# yum   install   mlocate -y  
#安装mlocate软件包得到locate命令

查找类的命令区别:

which和whereis : 只能查找linux里的命令
是精确查找
到PATH变量指定的路径查找
locate :可以查找命令和普通的文件或者文件夹
是模糊查找
到自己的数据库里查找,存在滞后性
范围: 整个/目录下–》整个系统里查找

4、stat命令

查看文件的状态

[root@localhost lianxi]# stat zhangshaowei.txt
  文件:zhangshaowei.txt
  大小:0               块:0          IO 块:4096   普通空文件
设备:fd00h/64768d      Inode:33577364    硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:default_t:s0
最近访问:2021-10-20 11:15:40.838616583 +0800
最近更改:2021-10-20 11:15:40.838616583 +0800
最近改动:2021-10-20 11:15:40.838616583 +0800
创建时间:-

atime 最近访问(access time ): 查看了文件里的内容 vim,cat
mtime最近更改(modify time ):更改内容
ctime 最近改动(change time ):改动文件的属性(所属者、组、大小、名字、时间)

touch 三个时间都会改动

5、find命令

5.1 单条件查找

1)name 根据名字查找,区分大小写

find . -name "hello*"     # 在当前目录查找以hello开头的文件
find / -name hello        # 在根目录下查找hello文件
find / -name "* hello *"  # 在根目录下查找包含名字里包含hello的文件

2)iname 根据名字查找,不区分大小写

find . -iname “hello” – insensitive不敏感的

3)size 根据大小查找

提供了指定文件大小的单位选项进而搜索符合大小文件的功能,这个搜索也常常会让用户感到非常舒服(b:块, c:字节, w:字, k:千字节, M:兆字节, G:吉字节)。

find / -size +10M   # 大于10M
find / -size 10M    # 等于10M
find / -size -10M   # 小于10M 

4)type 按文件类型查找

文件类型含义
f普通文件
d文件夹(目录)
l链接文件
b块设备文件 block
c字符设备文件 character
p管道文件 pipe
ssocket文件
find /data -type d         # 查找/data/目录下的文件夹
find / data ! type d       # 查找/data/目录下的非文件夹
find /data -type I         # 查找/data/目录下的链接文件
find /data -type d | xargs chmod 755 -R  # 查找目录类型并将权限设置为755
find /data -type f |xargs chmod 644 -R   # 查找文件类型并将权限设置为644

5)mtime 按时间查找

find . -mtime +7 7天前
find . -mtime -7 7天内

atime, access time  # 文件被读取或者执行的时间
ctime, change time  # 文件状态改变时间
mime, modify time   # 文件内容被修改的时间

6)mmin 按时间查找

find . -mmin +20 20分钟前
find . -mmin -20 20分钟内

find /data -mtime +30 -name ".log" # 查找30天以前的log文件
find /data -mtime -30 -name ".txt" # 查找30天以内的log文件
Find /data -mtime +30 -name ".txt" # 查找第30天的log文件
find /data -mmin +30 -name ".log"  # 查找30min以前被修改的log文件
find /data -amin -30 -name ".log"  # 查找30min以内被访问的log文件
find /data -cmin 30 -name ".log"   # 查找第30min改变的log文件

7)newer 按比某文件更新查找

find . -newer dengdeyong.txt 查找比dengdeyong.txt 更新建的文件

8)user 按用户查找

find / -user hejin

9)perm按权限查找

find /data -perm 755   # 查找/data/目录权限为755的文件或者目录
find /data -perm -007  # 与perm77相同,表示所有权限。
find /data -perm +644  # 查找文件权限符号为644以上的文件

5.2 多条件查找

1)-a与

默认是and 优先级高于-o

[root@192 ~]# find /boot -size +3M  -name "*linuz*"
/boot/vmlinuz-4.18.0-240.el8.x86_64
/boot/vmlinuz-0-rescue-812d56c86721496cb62f7dc52e5b72a5

2)-o或

组合:and 、or 简化的就是 -a -o

find /boot -size +3M -and -name “linuz
find /boot -size +3M -or -name “linuz

3)-not非

取反

4)优先级

find /root -type f -size +1M -o -name “vmlinuz”
改变优先级
find /root -type f \(-size +1M -o -name “vmlinuz”\)
解释:find /root -type f -size +1M或者find /root -type f -name “vmlinuz”

5.3 -exec

find / -name hello.c -exec cp {} /backup ;
查找/ 目录下的hello.c文件 复制到/backup 目录下

-exec 需要执行后面的命令
{}代表前面放的找到的内容
\ ; find命令的结束的符号

-ok 和 -exec的作用相同,只不过以一种更为安全的模式来执行该参数所给出的shell命令,在执行每一个命令之前,都会给出提示,让用户来确定是否执行

5.4 maxdepth

-maxdepth指定遍历搜索的最大深度
-maxdepth的参数为1时,只匹配当前目录下。
-mindepth 指定开始遍历搜索的最小深度

 find . -maxdepth 2 -name  sc1.txt

使用 -maxdepth 和-mindefth 可以限制 find 命令遍历的目录深度,并且 find 命令默认不搜索符号链接,可以用 -L 选项改变这种行为。

5.5 结合xargs使用

  • 查找指定数据信息进行删除
find /test -type f -name "test*.txt"|xargs rm
find /test -type f -name "test*.txt" -exec rm -rf {} \;
find /test -type f -name "test*.txt" -delete
  • 查找指定数据信息进行复制
 find /test -type f -name "test*.txt" |xargs -i cp {} /test01/
 find /test -type f -name "test*.txt" |xargs cp -t /test01/
 find /test -type f -name "test*.txt" -exec cp -a {} /test01 \;
  • 查找指定数据信息进行移动
find /test -type f -name "test*.txt" |xargs -i mv {} /test01/
find /test -type f -name "test*.txt" |xargs mv -t /test01/
find /test -type f -name "test*.txt" -exec mv {} /test01 \;
  • 查找指定数据信息按照日期 --主要用于批量删除历史数据信息
   查找7天以前的数据: find /test -type f -mtime +7 
   查找最近7天的数据: find /test -type f -mtime -7
   查找距今第7天数据: find /test -type f -mtime 7

5.6 实际应用

  1. 如何找出/test/目录中.txt结尾的文件,将找出的文件进行统一删除
a、find /test/ -maxdepth 1 -type f -name "*.txt" -delete
b、find /test/ -type f -name "*.txt" -exec rm -rf {} \;
c、find /test/ -type f -name "*.txt" | xargs rm -f 
d、rm -rf $(find /test -type f -name "*.txt")
e、rm -f `find /test/ -type f -name "*.txt"`
  1. 如何找出/test/目录中.txt结尾的文件,将找出的文件进行批量复制/移动到/tmp目录中
find /test/ -maxdepth 1 -type f -name "*.txt" -exec cp  {} /tmp \;
find /test/ -maxdepth 1 -type f -name "*.txt" | xargs cp /tmp         ---xargs其作用是将查找内容一列变为1行

补充:
1.查找指定数据信息进行删除

01.find /test -type f -name "test*.txt"|xargs rm
02.find /test -type f -name "test*.txt" -exec rm -rf {} \;
03.find /test -type f -name "test*.txt" -delete

2.查找指定数据信息进行复制

01.find /test -type f -name "test*.txt"|xargs -i cp {} /test01/
02.find /test -type f -name "test*.txt"|xargs cp -t /test01/
03.find /test -type f -name "test*.txt" -exec cp -a {} /test01 \;

3.查找指定数据信息进行移动

01.find /test -type f -name "test*.txt"|xargs -i mv {} /test01/
02.find /test -type f -name "test*.txt"|xargs mv -t /test01/
03.find /test -type f -name "test*.txt" -exec mv {} /test01 \;

4.查找指定数据信息按照日期(主要用于批量删除历史数据信息)

01.查找7天以前的数据:find /test -type f -mtime +7
02.查找最近7天的数据:find /test -type f -mtime -7
03.查找距今第7天数据:find /test -type f -mtime 7
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

这个手刹不太灵儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值