find命令用法

stat命令

atime mtime ctime

stat命令用来查看文件或目录的详细信息,用法为stat 文件名

[root@localhost ~]# stat /tmp/1
  文件:"/tmp/1"
  大小:6         	块:0          IO 块:4096   目录
设备:803h/2051d	Inode:620180      硬链接:2
权限:(0755/drwxr-xr-x)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:user_tmp_t:s0
最近访问:2018-06-09 08:18:49.367936994 +0800
最近更改:2018-06-09 08:18:49.367936994 +0800
最近改动:2018-06-09 08:18:49.367936994 +0800
创建时间:-

使用这个命令可以看到里面有三个时间点,Access ,Modify,Change 改成英文看比较直观

[root@localhost ~]# LANG=en
[root@localhost ~]# !stat
stat /tmp/1
  File: '/tmp/1'
  Size: 6         	Blocks: 0          IO Block: 4096   directory
Device: 803h/2051d	Inode: 620180      Links: 2
Access: (0755/drwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:user_tmp_t:s0
Access: 2018-06-09 08:18:49.367936994 +0800
Modify: 2018-06-09 08:18:49.367936994 +0800
Change: 2018-06-09 08:18:49.367936994 +0800
 Birth: -

这三个时间点可以分别表示为,atime , mtime ,ctime 当对该文件访问后,atime 会改变;改变了文件内容后mtime ,ctime 会一起改变,改变了文件的iNode属性后(例如权限,链接,属主属组等信息)会改变它的ctime

[root@localhost ~]# stat /tmp/2.txt 
  文件:"/tmp/2.txt"
  大小:0         	块:0          IO 块:4096   普通空文件
设备:803h/2051d	Inode:100663426   硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:user_tmp_t:s0
最近访问:2018-06-09 08:19:01.604936912 +0800
最近更改:2018-06-09 08:19:01.604936912 +0800
最近改动:2018-06-09 08:19:01.604936912 +0800
创建时间:-
[root@localhost ~]# echo "1212121212121212" >> /tmp/2.txt 
[root@localhost ~]# !stat
stat /tmp/2.txt 
  文件:"/tmp/2.txt"
  大小:17        	块:8          IO 块:4096   普通文件
设备:803h/2051d	Inode:100663426   硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:user_tmp_t:s0
最近访问:2018-06-09 08:19:01.604936912 +0800
最近更改:2018-06-09 08:38:19.209929176 +0800
最近改动:2018-06-09 08:38:19.209929176 +0800
创建时间:-

在这里我们可以看到,当改变了文件内容后,Mtime和ctime备改变了 ,atime没有改变

最近访问:2018-06-09 08:19:01.604936912 +0800
最近更改:2018-06-09 08:38:19.209929176 +0800
最近改动:2018-06-09 08:38:19.209929176 +0800
创建时间:-
[root@localhost ~]# cat /tmp/2.txt 
1212121212121212
[root@localhost ~]# !stat
stat /tmp/2.txt 
  文件:"/tmp/2.txt"
  大小:17        	块:8          IO 块:4096   普通文件
设备:803h/2051d	Inode:100663426   硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:user_tmp_t:s0
最近访问:2018-06-09 08:40:11.105928429 +0800
最近更改:2018-06-09 08:38:19.209929176 +0800
最近改动:2018-06-09 08:38:19.209929176 +0800
创建时间:-

上面代码可以看到,访问文件之后被改变的只有atime 。

最近访问:2018-06-09 08:40:11.105928429 +0800
最近更改:2018-06-09 08:38:19.209929176 +0800
最近改动:2018-06-09 08:38:19.209929176 +0800
创建时间:-
[root@localhost ~]# chmod 666 /tmp/2.txt 
[root@localhost ~]# !stat
stat /tmp/2.txt 
  文件:"/tmp/2.txt"
  大小:17        	块:8          IO 块:4096   普通文件
设备:803h/2051d	Inode:100663426   硬链接:1
权限:(0666/-rw-rw-rw-)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:user_tmp_t:s0
最近访问:2018-06-09 08:40:11.105928429 +0800
最近更改:2018-06-09 08:38:19.209929176 +0800
最近改动:2018-06-09 08:42:06.055927660 +0800

而改变文件的权限后,只有ctime改变,其他不变。

find 命令

上面讲这些是为了这个find命令的一个用法做的铺垫。 find命令是一个搜索命令,它的用途特别多,命令格式为: find <指定目录> <指定条件> <指定动作>

  • 指定目录 :可以指定目录,如果不指定则默认为当前目录下搜索
  • 指定条件:这个选项比较丰富了
  1. 指定文件类型 -type 文件类型
  2. 指定更改时间 -mtime +1/-1(更改时间为一天以前的/更改时间为一天以内的)
  3. -mmin 100 (更改时间100分钟以内的);
  4. -name "*.tar" (文件名中有.tar字符的)
  • 指定动作:将查找出的文件做什么处理,例如:find -type f -mtime -1 -exec lsattr {} ; (找出更改时间在1天以内的文件并显示它的隐藏权限)
[root@localhost ~]# find /tmp/ -type f -mtime -1 -exec ls -l {} \;
-rw-rw-rw-. 1 root root 55 6月   9 09:09 /tmp/2.txt
[root@localhost ~]# find /tmp/ -type f -mtime -1 -exec lsattr {} \;
---------------- /tmp/2.txt

还有一个查找硬链接文件,当我们知道一个文件做了硬链接了,想找到硬链接文件可以使用:find 目录 -inum inode号 来查找

[root@localhost ~]# ln /tmp/2.txt /media/567.txt
[root@localhost ~]# ls -li /tmp/2.txt 
100663426 -rw-rw-rw-. 2 root root 55 6月   9 09:09 /tmp/2.txt
[root@localhost ~]# find / -inum 100663426 
/tmp/2.txt
/media/567.txt

find命令用法多样化,而且很强大。

lacate whereis命令

首先安装mlocate包才能运行locate命令,

[root@test-01 ~]# yum install -y mlocate

并且首次使用会报错,因为系统没有生成mlocate.db这个库,可以使用updatedb命令 立即生成,生成库文件会占用较多资源,如果服务器在跑业务,最好不要执行这条命令,locate命令也是模糊查找,在搜索到的列表中,不管是目录还是文件,只要包含我们所查找的字符,就会被列出来,所以locate不适合精确查找。

[root@localhost ~]# locate /lic
/home/lic
/home/lic/.bash_history
/home/lic/.bash_logout
/home/lic/.bash_profile
/home/lic/.bashrc
/usr/share/doc/ivtv-firmware-20080701/license-end-user.txt
/usr/share/doc/ivtv-firmware-20080701/license-oemihvisv.txt
/var/spool/mail/lic
whereis命令

whereis命令也不常用,格式为whereis_ -参数 filename_ 参数及含义如下:

  • -b: 只查找二进制文件
  • -m:只查找帮助文件(在man目录下的文件)
  • -s:只查找源代码文件
[root@test-01 ~]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz

whereis命令类似于模糊查找,如上所示,运行whereis ls 只要带有ls字符,就会被列出来

[root@localhost ~]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz
[root@localhost ~]# whereis -m ls
ls: /usr/share/man/man1/ls.1.gz

文件后缀名

在Linux系统中,文件后缀名并不严谨,常常没有实际意义,就是因为惯例 ,大家为了方便管理,会将一类的文件给定统一的后缀名。

转载于:https://my.oschina.net/u/3731306/blog/1827198

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值