Linux下搜索文件
which
which 用来查找可执行文件的绝对路径:
[root@localhost ~]# which ls
alias ls='ls --color=auto'
/usr/bin/ls
which 只能用来查找PATH环境变量中出现的路径下的可执行文件
whereis
通过预先生成的一个文件列表库去查找跟给出的文件名相关的文件
语法: whereis [-bmsu] [文件名称]
[root@localhost ~]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz
‘-b’ : 只找binary 文件
‘-m’ : 只找在说明文件manual路径下的文件
‘-s’ : 只找source来源文件
‘-u’ : 没有说明档的文件
locate
‘locate’类似于whereis, 也是通过查找预先生成的文件列表库来查找文件
安装:yum install -y mlocate
’locate’所搜索到的文件列表,不管是目录名还是文件名,只要包含我们要搜索的关键词,都会列出来,所以’locate’不适合精准搜索
find
语法:find [路径] [参数] “文件/目录名”
- 参数 “-name” 通过文件名查找
[root@xxlinux-02 ~]# find /root/ -name "abc"
/root/abc
- 参数 “-type” 通过文件类型查找(如:f 、b、c、d、l、s)
[root@xxlinux-02 ~]# find -type l
./1soft.txt
- find /路径/ -type 参数 -atime/ctime/mtime +n/-n (通过三个时间来查找文件)
三个时间:
“-atime +n/-n” : 访问或读取文件时间大于/小于n天的文件
“-ctime +n/-n” : 写入、更改inode属性(如更改所有者、权限或者链接)时间大于/小于n天的文件
“-mtime +n/-n” : 写入文件时间大于/小于n天的文件, “-mmin”以分钟为单位
‘stat’ 命令可用来列出文件的 atime、ctime 和 mtime
[root@xxlinux-02 ~]# stat 1.txt
文件:"1.txt"
大小:0 块:0 IO 块:4096 普通空文件
设备:803h/2051d Inode:33585545 硬链接:2
权限:(0644/-rw-r--r--) Uid:( 1000/ xx) Gid:( 1000/ xx)
环境:unconfined_u:object_r:admin_home_t:s0
最近访问:2017-06-10 00:26:45.676547283 +0800
最近更改:2017-06-10 00:26:45.676547283 +0800
最近改动:2017-06-15 23:27:05.966580293 +0800
创建时间:-
- find / -inum inode:通过inode号查找硬链接文件
[root@xxlinux-02 ~]# ln 2.txt /tmp/hard2.txt
[root@xxlinux-02 ~]# ls -i 2.txt
33585547 2.txt
[root@xxlinux-02 ~]# find / -inum 33585547
/root/2.txt
/tmp/hard2.txt
-
find /路径/ 参数 -exec ls -l {} ; (查看所找到的文件的信息)
“{} ;” :表示找到的文件
“-exec”:表示执行什么命令,后面跟命令。如:copy、mv、cat等
[root@xxlinux-02 ~]# find / -inum 33585547 -exec ls -l {} \;
-rw-r--r--. 2 root root 0 6月 10 01:05 /root/2.txt
-rw-r--r--. 2 root root 0 6月 10 01:05 /tmp/hard2.txt
- find /路径/ -size +/-大小(如100k、10M等)
[root@xxlinux-02 ~]# find /root/ -size +1k
/root/anaconda-ks.cfg
/root/.bash_history
/root/.ssh/authorized_keys
Linux中后缀名并不能代表它的文件类型,只是为了方便区分,所以给同一类型的文件定义了相同类型的后缀名