目录
1 find命令
linux下搜索工具,分别有which,whereis,locate,find
- 用which 命令查找可执行文件的绝对路径
[root@worker1 ~]# which ls
alias ls='ls --color=auto'
/usr/bin/ls
- 用whereis 命令查找文件
-b:只查找二进制文件。
-m:只查找帮助文件(在man目录下的文件)。
-s:只查找源代码文件
[root@worker1 ~]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz
- 用locate 命令查找文件
[root@worker1 ~]# yum install -y mlocate
[root@worker1 ~]# locate ls
locate: can not stat () `/var/lib/mlocate/mlocate.db': No such file or directory
- 初次运行locate命令会报错,这是因为系统还没有生成那个文件列表库。
- 可以使用updatedb命令立即生成(或更新)这个库
[root@worker1 ~]# updatedb
[root@worker1 ~]# locate ls
- 如果你的服务器上正执行着重要的业务,那么最好不要去运行这个命令,因为一旦运行,服务器的压力会增大。默认情况下,这个数据库每周更新一次
ctrl+a 命令行光标位置移到行首
ctrl+e 命令行光标位置移到行尾
ctrl+u 命令行光标位置往前删除
1.1 find的参数: -name, -type
- 精准搜索
[root@worker1 ~]# find /etc/ -name "sshd*"
/etc/systemd/system/multi-user.target.wants/sshd.service
/etc/sysconfig/sshd
/etc/ssh/sshd_config
/etc/pam.d/sshd
- 模糊搜索
[root@worker1 ~]# find /etc/ -name "sshd*"
/etc/systemd/system/multi-user.target.wants/sshd.service
/etc/sysconfig/sshd
/etc/ssh/sshd_config
/etc/pam.d/sshd
- 普通文件类型搜索
[root@worker1 ~]# find /etc/ -type f -name "ssh*"
/etc/sysconfig/sshd
/etc/selinux/targeted/modules/active/modules/ssh.pp
/etc/ssh/ssh_config
/etc/ssh/ssh_host_rsa_key
/etc/ssh/ssh_host_rsa_key.pub
/etc/ssh/ssh_host_ecdsa_key
/etc/ssh/ssh_host_ecdsa_key.pub
/etc/ssh/ssh_host_ed25519_key
/etc/ssh/ssh_host_ed25519_key.pub
/etc/ssh/sshd_config
/etc/pam.d/sshd
- 目录类型搜索
[root@worker1 ~]# find /etc/ -type f -name "ssh*"
/etc/sysconfig/sshd
-l软链接 -b块设备 -c字符 -s套接字文件
[root@worker1 ~]# find /etc/ -type l
/etc/pki/tls/certs/ca-bundle.crt
/etc/pki/tls/certs/ca-bundle.trust.crt
/etc/pki/tls/cert.pem
find /dev/ -type b
[root@worker1 ~]# find /dev/ -type b
/dev/sr0
/dev/sda3
/dev/sda2
/dev/sda1
/dev/sda
/dev/fd0
find /dev/ -type c
[root@worker1 ~]# find /dev/ -type c
/dev/vcsa6
/dev/vcs6
/dev/vcsa5
find /dev/ -type s
[root@worker1 ~]# find /dev/ -type s
/dev/log
1.2find参数:-atime, -ctime, -mtime, -o
-atime +n/-n:表示访问或执行时间大于或小于n天的文件。
-ctime +n/-n:表示写入、更改inode属性(如更改所有者、权限或者链接)的时间大于或小于n天的文件。
-mtime +n/-n:表示写入时间大于或小于n天的文件,该参数用得最多
stat 命令查看文件具体信息
[root@worker1 ~]# stat file.txt
File: ‘file.txt’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 803h/2051d Inode: 68337322 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2018-09-18 19:18:17.147699697 +0800
Modify: 2018-09-18 19:18:17.147699697 +0800
Change: 2018-09-18 19:18:17.147699697 +0800
Birth: -
Access:atime Modify:mtime Change:ctime
- ctime 修改了
[root@worker1 ~]# chmod 700 file.txt
[root@worker1 ~]# stat file.txt
File: ‘file.txt’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 803h/2051d Inode: 68337322 Links: 1
Access: (0700/-rwx------) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2018-09-18 19:18:17.147699697 +0800
Modify: 2018-09-18 19:18:17.147699697 +0800
Change: 2018-09-18 19:20:38.409691958 +0800
Birth: -
- 修改了mtime
[root@worker1 ~]# echo "hello world" >> file.txt
[root@worker1 ~]# stat file.txt
File: ‘file.txt’
Size: 12 Blocks: 8 IO Block: 4096 regular file
Device: 803h/2051d Inode: 68337322 Links: 1
Access: (0700/-rwx------) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2018-09-18 19:18:17.147699697 +0800
Modify: 2018-09-18 19:22:06.505687132 +0800
Change: 2018-09-18 19:22:06.505687132 +0800
Birth: -
- 修改了atime
[root@worker1 ~]# cat file.txt
hello world
[root@worker1 ~]# stat file.txt
File: ‘file.txt’
Size: 12 Blocks: 8 IO Block: 4096 regular file
Device: 803h/2051d Inode: 68337322 Links: 1
Access: (0700/-rwx------) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2018-09-18 19:22:56.447684396 +0800
Modify: 2018-09-18 19:22:06.505687132 +0800
Change: 2018-09-18 19:22:06.505687132 +0800
Birth: -
以时间单位查找修改文件:
- 文件的modified time(即mtime)是在写入文件时随文件内容的更改而更改的
搜索一天以内的修改文件
[root@worker1 ~]# find / -type f -mtime -1
搜索大于一天的修改文件
[root@worker1 ~]# find / -type f -mtime +1
- 文件的access time(即atime)是在读取文件或者执行文件时更改的
搜索大于一天atime变化的文件
[root@worker1 ~]# find / -type f -atime +1
- 文件的change time(即ctime)是在写入文件、更改所有者、权限或链接设置时随inode内容的更改而更改的。
搜索大于一天ctime变化的文件
[root@worker1 ~]# find / -type f -ctime +1
搜索大于一天的修改文件,后缀是.conf
[root@worker1 ~]# find /etc/ -type f -mtime +1 -name "*.conf"
/etc/resolv.conf
/etc/pki/ca-trust/ca-legacy.conf
/etc/yum/pluginconf.d/fastestmirror.conf
搜索大于一天修改的文件,条件是或者大于一天 或者名字是.conf,-o是条件或的意思
[root@worker1 ~]# find /etc/ -type f -o -mtime +1 -o -name "*.conf"
1.3 find的参数:-inum , -mmin, -size,
- find查找硬链接文件,使用-inum选项,通过inode检索
[root@worker1 ~]# ls -il
total 12
67223680 -rw-------. 1 root root 1116 Sep 5 22:58 anaconda-ks.cfg
34105802 drwxr-xr-x 2 root root 22 Sep 17 19:46 dir1
68337323 -rw-r--r-- 1 root root 0 Sep 18 20:13 file1.txt
68337332 -rw-r--r-- 1 root root 0 Sep 18 20:13 file2.txt
68337322 -rwx------ 1 root root 12 Sep 18 19:22 file.txt
34105807 -rw-rw-r-- 2 user user 12 Sep 18 20:16 hard.txt
[root@worker1 ~]# find / -inum 34105807
/root/hard.txt
/tmp/dir1/file2.txt
- 查找一小时以内更改的文件
[root@worker1 ~]# find /root/ -type f -mmin -60
/root/hard.txt
/root/file.txt
/root/file1.txt
/root/file2.txt
- {}表示find找出来的文件 ,\是脱义符号
- 查找一小时以内更改的文件,并以详细信息输出
[root@worker1 ~]# find /root/ -type f -mmin -60 -exec ls -l {} \;
-rw-rw-r-- 2 user user 12 Sep 18 20:16 /root/hard.txt
-rwx------ 1 root root 12 Sep 18 19:22 /root/file.txt
-rw-r--r-- 1 root root 0 Sep 18 20:13 /root/file1.txt
-rw-r--r-- 1 root root 0 Sep 18 20:13 /root/file2.txt
- 查找一小时以内更改的文件,并将在名字改为后缀加上.bak
[root@worker1 ~]# find /root/ -type f -mmin -60 -exec mv {} {}.bak \;
[root@worker1 ~]# ls -l
total 12
-rw-------. 1 root root 1116 Sep 5 22:58 anaconda-ks.cfg
drwxr-xr-x 2 root root 22 Sep 17 19:46 dir1
-rw-r--r-- 1 root root 0 Sep 18 20:13 file1.txt.bak
-rw-r--r-- 1 root root 0 Sep 18 20:13 file2.txt.bak
-rwx------ 1 root root 12 Sep 18 19:22 file.txt
-rw-rw-r-- 2 user user 12 Sep 18 20:16 hard.txt.bak
- 查找/root/目录下文件小于10K的文件
[root@worker1 ~]# find /root/ -type f -size -10k -exec ls -lh {} \;
-rw-r--r--. 1 root root 18 Dec 29 2013 /root/.bash_logout
-rw-r--r--. 1 root root 176 Dec 29 2013 /root/.bash_profile
-rw-r--r--. 1 root root 176 Dec 29 2013 /root/.bashrc
- 查找/root/目录下文件小于10K的文件
[root@worker1 ~]# find /root/ -type f -size +10k -exec ls -lh {} \;
-rw-------. 1 root root 14K Sep 18 07:24 /root/.bash_history
- 查找/root/目录下文件小于10M的文件
[root@worker1 ~]# find /root/ -type f -size +10M -exec ls -lh {} \;
2 文件名后缀
- 对于“后缀名”这个概念,相信你并不陌生。在Linux系统中,文件的后缀名没有具体意义,加或者不加都无所谓。但是为了便于区分,我们习惯在定义文件名时加一个后缀名。这样当用户看到这个文件名时,就会很快知道它到底是一个什么文件,例如1.sh、2.tar.gz、my.cnf、test.zip等。
- 列举的几个文件名中,1.sh代表它是一个shell脚本,2.tar.gz代表它是一个压缩包,my.cnf代表它是一个配置文件,test.zip代表它是一个压缩文件。
- 另外需要知道,早期的UNIX系统文件名最多允许14个字符,而新的UNIX或者Linux系统中,文件名最长可达255个字符