一、which 命名
1.which 只能用于查询环境变量PATH中的路径下的可执行文件。例如 ls cat mv ,我们看到他们都是在PATH下的。
[root@localhost ~]# which ls
alias ls='ls --color=auto' /usr/bin/ls[root@localhost ~]# which cat/usr/bin/cat[root@localhost ~]# echo $paht[root@localhost ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin[root@localhost ~]# which mv/usr/bin/mv二、whereis 命令
1.whereis 是通过从一个定期更新生成的文件库查找用户想查找的文件名相关的文件。格式:where file
[root@localhost ~]# whereis test
test: /usr/bin/test /usr/share/man/man1/test.1.gz /usr/share/man/man1p/test.1p.gz三、locate 命令
1.locate 也是从一个预先生成的库中查找用户要查找的文件,类似于whereis.需要安装mlocate包。格式 : locate file
安装命令 yum install -y mlocate
[root@localhost ~]# locate find
/usr/bin/eu-findtextrel/usr/bin/find/usr/bin/find-jar四、find 命令
1. find 命令的查询方式很多,查询时条件越多,查询的越快。 find 如果不加路径查找,默认在当前目录下查找。最少条件查询 find 路径 -name file
[root@localhost ~]# find -name dir
./dir. 代表当前目录。
[root@localhost ~]# find /opt/ -name dir
/opt/test/dir/opt/dir1/dir指定路径后,会在指定路径下查找出用户要查找的所有的相关的文件名。
2. 指定查找文件类型: find 路径 -type(类型) {
d f (-) c b} 等 -name file 查找 etc 下以p开头的目录, * 代表所有。
[root@localhost etc]# find /etc/ -type d -name "p*"
/etc/pki/etc/pki/ca-trust/extracted/pem/etc/pki/tls/private3.查找 etc 下以p开头的文件, * 代表所有。
[root@localhost etc]# find /etc/ -type f -name "p*"
/etc/pki/nssdb/pkcs11.txt/etc/abrt/plugins/python.conf
4.时间属性,有三种 atime ctime mtime , stat 列出一个文件的三种时间属性。由下图对比可知,在查看1.txt 后, atime 发生变化,即 atime 是在读取或者执行文件时发生改变。
5.由下图对比可知,在对1.txt 写入内容后, mtime和ctime 发生变化,即 mtime 和 ctime是在写入时发生改变。
6.由下图对比可知,在改变1.txt 权限后,只有 ctime 时间发生变化,即 ctime 是在改变权限时发生改变。
7.指定查找文件的时间属性 find 路径 -type {d f c b }等 {-mtime -ctime -atime} + - n -name file
查找etc 下一天以内变动过的文件,
[root@localhost ~]# find /etc/ -type f -mtime 1
/etc/group-/etc/gshadow-[root@localhost ~]# ls -ld /etc/group
-rw-r--r--. 1 root root 1135 Dec 22 08:07 /etc/group8.查找 tmp下 查找文件 或者一天以内或者 txt文件 -o 或者
[root@localhost ~]# find /tmp/ -type f -o -mtime -1 -o -name "*.txt"
/tmp//tmp/.X11-unix/tmp/.X11-unix/X0/tmp/.ICE-unix/tmp/.ICE-unix/1228/tmp/.ICE-unix/1235/tmp/11.txt/tmp/22.txt/tmp/1.txt/tmp/34.txt/tmp/55.txt9.以时间查找时还有以分钟查找,即mmin cmin amin
查找一个小时内变动或者创建的文件
[root@localhost ~]# find /tmp/ -type f -mmin -60 -name "*.txt"
/tmp/22.txt[root@localhost ~]# ls -ld /tmp/22.txt -rw-r--r--. 1 root root 0 Dec 23 00:40 /tmp/22.txt
10.在查找文件的同时将文件的信息展示出来。{}类似于集合或者列表。
[root@localhost ~]# find /etc/ -type f -mtime -7 -exec ls -l {} \;
-rw-r--r--. 1 root root 1135 Dec 22 08:07 /etc/group----------. 1 root root 901 Dec 22 08:07 /etc/gshadow-rw-r--r--. 1 root root 1117 Dec 21 00:53 /etc/group-----------. 1 root root 887 Dec 21 00:53 /etc/gshadow--rw-r-----. 1 root lp 410 Dec 18 17:16 /etc/cups/subscriptions.conf.O-rw-r-----. 1 root lp 111 Dec 18 18:26 /etc/cups/subscriptions.conf-rw-r--r--. 1 root root 14 Dec 22 22:58 /etc/tuned/active_profile11.同时还可以重命名或者复制等动作。
[root@localhost ~]# find /opt/ -type f -name "*.txt" -exec cp {} {}.bat \;
[root@localhost ~]# find /opt/ -type f -name "*.txt" -exec ls -l {} \;-rw-r--r--. 1 root root 0 Dec 22 09:15 /opt/1.txt-rw-r--r--. 1 root root 0 Dec 23 01:05 /opt/2.txt-rw-r--r--. 1 root root 0 Dec 23 01:05 /opt/3.txt-rw-r--r--. 1 root root 0 Dec 23 01:05 /opt/4.txt-rw-r--r--. 1 root root 0 Dec 23 01:05 /opt/5.txt[root@localhost ~]# cd /opt/[root@localhost opt]# ls1.bat 1.txt 1.txt.bat 2.txt 2.txt.bat 3.txt 3.txt.bat 4.txt 4.txt.bat 5.txt 5.txt.bat dir1 rh test12.以大小查找, -size
查找tmp 下大于10k的文件
[root@localhost opt]# find /tmp/ -type f -size +10k -exec ls -lh {} \;
-rw-r--r--. 1 root root 27K Dec 14 14:00 /tmp/1.txt.bat-rw-------. 1 root root 116K Dec 10 19:31 /tmp/yum_save_tx.2017-12-10.19-31.vcurlN.yumtx-rw-------. 1 root root 116K Dec 13 12:02 /tmp/yum_save_tx.2017-12-13.12-02.Po5XR3.yumtx五.文件后缀名。Linux 下文件后缀名不严格,用户可以自定义后缀名。但是在使用过程中默认还是分类,方便大家使用区分。