文章目录
常用命令-文件搜索命令
文件搜索命令一:find
文件搜索
语法
find [搜索范围] [匹配条件]
范例
常规搜索
在目录/etc中查找文件init:
find /etc -name init
在目录etc中查找包含init的文件:
find /etc -name *init*
-iname 不区分大小写
通配符
*匹配任意多个字符
?匹配一个字符
过滤文件大小
- 在根目录下查找大于100MB的文件:
find / -size +204800
+n:大于 -n:等于 n:等于
n是数据块的大小,默认大小为0.5k,100M: 100*1024*2=204800 - 在/etc查找大于80MB小于100MB的文件:
find /etc -size +163840 -a -size -204800
-a 两个条件同时满足
-o 两个条件满足任意一个即可
通过用户(所有者/组)过滤
在根目录下查找所有者为root的文件:find /home -user root
-user 根据所有者查找
-group 根据所属组查找
最近时间修改的文件
在/etc下查找5分钟内被修改过属性的文件和目录:find /etc -cmin -5
-amin 访问时间access
-cmin 文件属性change
-mmin 文件内容modify
对搜索结果执行操作
在/etc下查找inittab文件并显示其详细信息:find /etc -name inittab -exec ls -l {} \;
-exec/-ok 命令 {} ; 对搜索结果执行操作,其中-ok需要用户逐一确认
其他搜索
-type 根据文件类型查找
f:文件 d:目录 l:软连接文件
-inum 根据i节点查找,查找硬链接
文件搜索命令二:locate
在文件资料库中查找文件,类似于windows的everything,搜索速度很快,占用资源少。
语法
locate 文件名
范例
如:locate -i inittab
-i:不区分大小写
手动更新资料库:updatedb
注:tmp目录下不会进入资料库
文件搜索命令三:which
搜索命令所在目录及别名信息
语法
which [命令名称]
范例
如:which ls
bin目录下所有用户可用
sbin目录root用户可用
文件搜索命令四 whereis
搜索命令所在目录及帮助文档路径
语法
whereis [命令名称]
范例
如 whereis ls
扩展:whatis与apropos
- whatis:查看命令的作用。
- apropos:查看配件文件的作用。
# whatis ls
ls (1) - list directory contents
# apropos passwd
checkPasswdAccess (3) - query the SELinux policy database in the kernel
chgpasswd (8) - update group passwords in batch mode
chpasswd (8) - update passwords in batch mode
etcdctl3-user-passwd (1) - Changes password of user
gpasswd (1) - administer /etc/group and /etc/gshadow
grub2-mkpasswd-pbkdf2 (1) - Generate a PBKDF2 password hash.
lpasswd (1) - Change group or user password
mkpasswd (1) - generate new password, optionally apply it to a user
pam_localuser (8) - require users to be listed in /etc/passwd
passwd (1) - update user's authentication tokens
sslpasswd (1ssl) - compute password hashes
pwhistory_helper (8) - Helper binary that transfers password hashes from passwd or shadow to opasswd
selinux_check_passwd_access (3) - query the SELinux policy database in the kernel
SSL_CTX_set_default_passwd_cb (3ssl) - set passwd callback for encrypted PEM file handling
SSL_CTX_set_default_passwd_cb_userdata (3ssl) - set passwd callback for encrypted PEM file handling
文件搜索命令五:grep
在文件中搜寻字串匹配的行并输出
语法
grep -iv [指定字符] [文件或目录]
-i 不区分大小写
-v 排除执行字串
-r 递归寻找子目录
范例
在/etc/inittab中查找包含multiuser的行:grep -i multiuser /etc/inittab
去掉以#开头的行:grep -v ^# /etc/inittab