2.23/2.24/2.25 find命令 2.26 文件名后缀

2.23/2.24/2.25 find命令

搜索工具 which :用于查找可执行文件的绝对路径;

[root@linux-151 ~]# which ls
alias ls='ls --color=auto'
/usr/bin/ls

whereis:通过预先生成的一个文件列表库查找与给出的文件名相关的文件; 格式: whereis [选项] 文件名 -b:只查找二进制文件; -m:只查找帮助文件; -s:只查找源代码文件;

[root@linux-151 ~]# whereis -b ls
ls: /usr/bin/ls
[root@linux-151 ~]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz

locate:和whereis类似,也是通过查找预先生成的文件列表库来查找文件在哪里,后面直接跟文件名;需要安装mlocate软件包:

[root@linux-151 ~]# yum install -y mlocate
[root@linux-151 ~]# updatedb
[root@linux-151 ~]# locate yum.log
/var/log/yum.log

安装好后,第一次运行locate会报错,需要运行updatedb命令立即生成或者更新这个库文件;默认情况下这个库每周更新一次;locate不适合精准查找。

find命令

格式:find [路径] [参数]

-type 文件类型:f(文件)d(目录)l(软链接)s(sock)d(块设备)c(串口设备键盘鼠标等)

-name 文件名

-size 文件大小(10k;10M等)

-mmin +- 分钟数

-inum 跟inode,通过inode来查找一个文件的硬链接

-o 或者

-exec <执行指令>:假设find指令的回传值为True,就执行该指令。

-atime +n/-n 表示访问或执行时间大于或小于n天的文件。

-ctime +n/-n 表示更改inode属性(如更改所有者,权限或者链接)的时间大于或小于n天的文件。

-mtime +n/-n 表示创建或者修改文件时间大于或小于n天的文件,该参数用得最多

-1 小于1天;1天以内

+1 大于1天;1天以前

不能等于1

例子:

  • 查找1天内创建或修改的后缀名为.conf的文件
[root@linux-151 ~]# find /etc/ -type f -mtime -1 -name "*.conf"
/etc/resolv.conf
  • 查找root目录下1天内创建或修改的文件
[root@linux-151 ~]# find /root/ -type f -mtime -1
/root/.bash_history
/root/321.txt
  • 查找root目录下10分钟内创建的文件
[root@linux-151 ~]# find /root/ -type f -mmin -10
/root/11.txt
  • 查找root目录下10分钟创建或修改的文件,并且显示出具体修改时间;
[root@linux-151 ~]# find /root/ -type f -mmin -10 -exec ls -l {} \;
-rw-r--r-- 1 root root 0 3月 30 22:03 /root/11.txt

tips:{}表示前面列出来的文件,\脱意符号,;换行符

  • 查找目录/root/下1天内修改的文件,并且备份一份。
[root@linux-151 ~]# find /root/ -type f -mtime -1 -exec cp {} {}.bak \;
[root@linux-151 ~]# ls -al /root/
总用量 84
dr-xr-x---. 4 root root 288 2月 27 22:13 .
dr-xr-xr-x. 17 root root 265 2月 19 15:07 ..
-rw-r--r-- 1 root root 0 2月 27 22:03 11.txt
-rw-r--r-- 1 root root 0 2月 27 22:13 11.txt.bak
lrwxrwxrwx 1 root root 9 2月 27 01:12 12 -> /tmp/111/
drwxr-xr-x 2 root root 6 2月 27 22:03 123
-rw-------. 2 root root 0 2月 17 03:40 1.log
-rw-r--r-- 1 root root 16816 2月 27 01:31 321.txt
-rw-r--r-- 1 root root 16816 2月 27 22:13 321.txt.bak
-rw-------. 1 root root 1422 2月 17 03:51 anaconda-ks.cfg
-rw-------. 1 root root 6339 2月 27 01:40 .bash_history
-rw------- 1 root root 6339 2月 27 22:13 .bash_history.bak
-rw-r--r--. 1 root root 18 3月 29 2018 .bash_logout
-rw-r--r--. 1 root root 176 3月 29 2018 .bash_profile
-rw-r--r--. 1 root root 176 3月 29 2018 .bashrc
-rw-r--r--. 1 root root 100 3月 29 2018 .cshrc
-rw------- 1 root root 41 2月 25 00:48 .lesshst
drwx------. 2 root root 80 2月 19 17:04 .ssh
-rw-r--r--. 1 root root 129 3月 29 2018 .tcshrc
  • 查看root目录下小于6k的文件,并且列出它具体的大小
[root@linux-151 ~]# find /root/ -type f -size -6k -exec ls -lh {} \;
-rw-r--r--. 1 root root 18 3月 30 2018 /root/.bash_logout
-rw-r--r--. 1 root root 176 3月 30 2018 /root/.bash_profile
-rw-r--r--. 1 root root 176 3月 30 2018 /root/.bashrc
-rw-r--r--. 1 root root 100 3月 30 2018 /root/.cshrc
-rw-r--r--. 1 root root 129 3月 30 2018 /root/.tcshrc
-rw-------. 1 root root 1.4K 2月 17 03:51 /root/anaconda-ks.cfg
-rw-r--r--. 1 root root 806 2月 18 19:41 /root/.ssh/authorized_keys
-rw-r--r-- 1 root root 176 2月 19 16:24 /root/.ssh/known_hosts
-rw------- 1 root root 1.7K 2月 19 17:04 /root/.ssh/id_rsa
-rw-r--r-- 1 root root 396 2月 19 17:04 /root/.ssh/id_rsa.pub
-rw------- 1 root root 41 2月 25 00:48 /root/.lesshst
-rw-------. 2 root root 0 2月 17 03:40 /root/1.log
-rw-r--r-- 1 root root 0 2月 27 22:03 /root/11.txt
-rw-r--r-- 1 root root 0 2月 27 22:13 /root/11.txt.bak
  • 通过inode来查找一个文件的硬链接;
[root@linux-151 ~]# ls -i 1.log
8388681 1.log
[root@linux-151 ~]# find -inum 8388681
./1.log
[root@linux-151 ~]# find / -inum 8388681
/root/1.log
/tmp/yum.log

2.26 文件名后缀

stat命令可以列出文件的atime,mtime,ctime;

[root@linux-128 ~]# stat 11.txt
文件:"11.txt"
大小:0 块:0 IO 块:4096 普通空文件
设备:803h/2051d Inode:16799023 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
最近访问:2018-03-30 22:13:47.084544630 +0800 //atime
最近更改:2018-03-30 22:03:41.825691647 +0800 //mtime
最近改动:2018-03-30 22:03:41.825691647 +0800 //ctime
创建时间:-

注:linux文件后缀名 在linux系统中,文件的后缀名没有具体的意义,加或者不加都无所谓。