提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
环境及相关软件:VMWare 14、CentOS 7、SecureCRT。
提示:以下是本篇文章正文内容,下面案例可供参考
命令所在路径:/bin/find
执行权限:所有用户
语法: find[搜索范围] [匹配条件]
功能描述:文件搜索
//根据文件名精确搜索 [root@localhost tmp]# find /etc -name init /etc/sysconfig/init /etc/selinux/targeted/active/modules/100/init //根据文件名模糊查搜索(*表示匹配任意个字符) [root@localhost tmp]# find /etc -name *init* /etc/systemd/system/sysinit.target.wants /etc/inittab /etc/sysconfig/init /etc/sysconfig/network-scripts/init.ipv6-global /etc/init.d /etc/rc.d/init.d /etc/selinux/targeted/active/modules/100/init /etc/selinux/targeted/contexts/initrc_context /etc/security/namespace.init //根据文件名模糊查搜索(?表示匹配单个字符) [root@localhost tmp]# find /etc -name init??? /etc/inittab [root@localhost tmp]# touch /etc/INITTAB [root@localhost tmp]# find /etc -name init??? /etc/inittab //-name替换为-iname。为不区分大小写 [root@localhost tmp]# find /etc -iname init??? /etc/inittab /etc/INITTAB
//查找大于100MB的文件 [root@localhost tmp]# find / -size +204800 /proc/kcore find: ‘/proc/1589/task/1589/fd/6’: 没有那个文件或目录 find: ‘/proc/1589/task/1589/fdinfo/6’: 没有那个文件或目录 find: ‘/proc/1589/fd/5’: 没有那个文件或目录 find: ‘/proc/1589/fdinfo/5’: 没有那个文件或目录 /sys/devices/pci0000:00/0000:00:0f.0/resource1_wc /sys/devices/pci0000:00/0000:00:0f.0/resource1 /usr/lib/locale/locale-archive
注:Linux中最小单位为数据块,换算公式为:
1数据块=512B(字节)=0.5KB
[root@localhost tmp]# find /home -user yangf /home/yangf /home/yangf/.bash_logout /home/yangf/.bash_profile /home/yangf/.bashrc
/home/:普通用户的家目录。建立每个用户时,每个用户要有一个默认登录位置,这个位置就是这个用户的家目录,所有普通用户的家目录就是在/home下建立一个和用户名相同的目录。如用户user1的家目录就是home/user1。
范例:$ find /etc -cmin -5
在/etc下查找5分钟内被修改过属性的文件和目录
-amin访问时间access
-cmin文件属性change
-mmin文件内容modify
[root@localhost tmp]# find /etc -mmin -30 /etc /etc/INITTAB
-a 两个条件同时满足
-o 两个条件满足任意一个即可
-type 根据文件类型查找(f:文件,d:目录,l:软链接文件)
[root@localhost tmp]# find /etc -name init* -a -type f /etc/inittab /etc/sysconfig/init /etc/sysconfig/network-scripts/init.ipv6-global /etc/selinux/targeted/contexts/initrc_context
-exec/-ok 命令 {} \ ; 对搜索结果执行操作
-inum 根据i节点查找
//在/etc下查找inittab文件并显示其详细信息 [root@localhost tmp]# find /etc -name inittab -exec ls -l {} \; -rw-r--r--. 1 root root 511 10月 13 2020 /etc/inittab //在/etc下对以init开头的文件模糊搜索,并显示其详细信息 [root@localhost tmp]# find /etc -name init* -a -type f -exec ls -l {} \; -rw-r--r--. 1 root root 511 10月 13 2020 /etc/inittab -rw-r--r--. 1 root root 798 10月 13 2020 /etc/sysconfig/init -rwxr-xr-x. 1 root root 5419 5月 22 2020 /etc/sysconfig/network-scripts/init.ipv6-global -rw-r--r--. 1 root root 30 10月 1 2020 /etc/selinux/targeted/contexts/initrc_context [root@localhost tmp]# find /home -user yangf -ok rm {} \; < rm ... /home/yangf > ? n < rm ... /home/yangf/.bash_logout > ? n < rm ... /home/yangf/.bash_profile > ? n < rm ... /home/yangf/.bashrc > ? n [root@localhost tmp]# [root@localhost tmp]# [root@localhost tmp]# touch "qiangge handsome" [root@localhost tmp]# ls -i 16777665 day1 51041787 day3 16777637 hongjiu 17395469 issue 16778156 issue.hard 16777647 issue.soft 17395463 locale.conf 33625950 ningsy 16777289 pijiu 17395467 program files 16777639 qiangge handsome 17395464 study.list 17395465 sudo.conf 728723 systemd-private-0c4ab19897584e3f95f9f2ffddea2f98-chronyd.service-2AuwFB 6616 systemd-private-0dd6a9e7e1024e15ba73db310090c201-chronyd.service-vyFtyd 537725 systemd-private-86a69b4ef3cf440e9e3cd345c20f1569-chronyd.service-jVuvd3 728713 systemd-private-d8000b3920d44f71b59a0531fec19d10-chronyd.service-n1KqK5 728715 systemd-private-edafe6e8a1284b37be6c9a30e921f73f-chronyd.service-3z0bxa 728724 vmware-root_663-4022243318 6617 vmware-root_664-2722697761 728714 vmware-root_666-2731021219 728716 vmware-root_667-3980363901 537726 vmware-root_673-3988556249 //查找当前目录下i节点为16777639的文件,并删除该文件 [root@localhost tmp]# find . -inum 16777639 -exec rm {} \; [root@localhost tmp]# ls day1 sudo.conf day3 systemd-private-0c4ab19897584e3f95f9f2ffddea2f98-chronyd.service-2AuwFB hongjiu systemd-private-0dd6a9e7e1024e15ba73db310090c201-chronyd.service-vyFtyd issue systemd-private-86a69b4ef3cf440e9e3cd345c20f1569-chronyd.service-jVuvd3 issue.hard systemd-private-d8000b3920d44f71b59a0531fec19d10-chronyd.service-n1KqK5 issue.soft systemd-private-edafe6e8a1284b37be6c9a30e921f73f-chronyd.service-3z0bxa locale.conf vmware-root_663-4022243318 ningsy vmware-root_664-2722697761 pijiu vmware-root_666-2731021219 program files vmware-root_667-3980363901 study.list vmware-root_673-3988556249
命令所在路径:/usr/bin/locate
执行权限:所有用户
语法:locate文件名
功能描述:在文件资料库中查找文件
范例:$ locate inittab
[root@localhost tmp]# locate inittab -bash: locate: 未找到命令 //1、安装mlocate包 [root@localhost tmp]# yum -y install mlocate 已加载插件:fastestmirror Determining fastest mirrors * base: mirrors.njupt.edu.cn * extras: mirrors.ustc.edu.cn * updates: mirrors.nju.edu.cn base | 3.6 kB 00:00 extras | 2.9 kB 00:00 updates | 2.9 kB 00:00 updates/7/x86_64/primary_db | 13 MB 00:06 正在解决依赖关系 --> 正在检查事务 ---> 软件包 mlocate.x86_64.0.0.26-8.el7 将被 安装 --> 解决依赖关系完成 依赖关系解决 ============================================================= Package 架构 版本 源 大小 ============================================================= 正在安装: mlocate x86_64 0.26-8.el7 base 113 k 事务概要 ============================================================= 安装 1 软件包 总下载量:113 k 安装大小:379 k Downloading packages: mlocate-0.26-8.el7.x86_64.rpm | 113 kB 00:00 Running transaction check Running transaction test Transaction test succeeded Running transaction 正在安装 : mlocate-0.26-8.el7.x86_64 1/1 验证中 : mlocate-0.26-8.el7.x86_64 1/1 已安装: mlocate.x86_64 0:0.26-8.el7 完毕! //2、更新数据库 [root@localhost tmp]# updatedb //3、locate搜索 [root@localhost tmp]# locate inittab /etc/inittab
[root@localhost tmp]# touch /root/fuwuqi //若新建一个文件,文件资料库可能未及时更新,需更新资料库再查找 [root@localhost tmp]# locate fuwuqi [root@localhost tmp]# updatedb [root@localhost tmp]# locate fuwuqi /root/fuwuqi //若在tmp目录下新建一个文件,由于tmp目录不属于文件资料库的搜索范围。故搜索不到 [root@localhost tmp]# touch /tmp/kehuduan [root@localhost tmp]# updatedb [root@localhost tmp]# locate kehuduan
注:文件资料库在 /var/lib/mlocate/mlocate.db 文件中。另外,系统在更新数据库的配置文件中,设置了一些搜索限制,所以locate命令搜索不到tmp目录下的文件,搜索限制可在 /etc/updatedb.conf 配置文件中查看:
[root@localhost tmp]# vi /etc/updatedb.conf PRUNE_BIND_MOUNTS = "yes" PRUNEFS = "9p afs anon_inodefs auto autofs bdev binfmt_misc cgroup cifs coda configfs cpuset debugfs devpts ecryptfs exofs fuse fuse.sshfs fusectl gfs gfs2 gpfs hugetlbfs inotifyfs iso9660 jffs2 lustre mqueue ncpfs nfs nfs4 nfsd pipefs proc ramfs roo tfs rpc_pipefs securityfs selinuxfs sfs sockfs sysfs tmpfs ubifs udf usbfs fuse.glusterfs ceph fuse.ceph" PRUNENAMES = ".git .hg .svn" PRUNEPATHS = "/afs /media /mnt /net /sfs /tmp /udev /var/cache/ccache /var/lib/yum/yumdb /var/spool/cups /var/spool/squid /va r/tmp /var/lib/ceph"
[root@localhost tmp]# touch /root/fuwuqI [root@localhost tmp]# updatedb [root@localhost tmp]# locate fuwuqi /root/fuwuqi [root@localhost tmp]# locate -i fuwuqi /root/fuwuqI /root/fuwuqi
命令所在路径:/usr/bin/which
执行权限:所有用户
语法:which 命令
功能描述:搜索命令所在目录及别名信息范例: $ which ls
[root@localhost tmp]# which rm alias rm='rm -i' /usr/bin/rm [root@localhost tmp]# ls /root anaconda-ks.cfg day2 fuwuqI test1 day1 fuwuqi learn2 tmp.soft [root@localhost tmp]# rm /root/fuwuqI rm:是否删除普通空文件 "/root/fuwuqI"?y //由此可知,之前敲的rm指令是别名rm -i指令,-i表示询问确认 [root@localhost tmp]# /bin/rm /root/fuwuqi [root@localhost tmp]# ls /root anaconda-ks.cfg day1 day2 learn2 test1 tmp.soft [root@localhost tmp]# which cp alias cp='cp -i' /usr/bin/cp [root@localhost tmp]# which ifconfig /usr/sbin/ifconfig
命令所在路径:/usr/bin/whereis
执行权限:所有用户
语法: whereis [命令名称]
功能描述:搜索命令所在目录及帮助文档路径
范例: $ whereis ls
[root@localhost tmp]# whereis useradd useradd: /usr/sbin/useradd /usr/share/man/man8/useradd.8.gz [root@localhost tmp]# whereis rm rm: /usr/bin/rm /usr/share/man/man1/rm.1.gz
命令所在路径:/bin/grep
执行权限:所有用户
语法:grep -iv[指定字串] [文件]
功能描述:在文件中搜寻字串匹配的行并输出
-i 不区分大小写
-v 排除指定字串
范例:# grep mysql /root/install.log
//查看inittab文件 [root@localhost tmp]# more /etc/inittab # inittab is no longer used when using systemd. # # ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM. # # Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target # # systemd uses 'targets' instead of runlevels. By default, there are two main targets: # # multi-user.target: analogous to runlevel 3 # graphical.target: analogous to runlevel 5 # # To view current default target, run: # systemctl get-default # # To set a default target, run: # systemctl set-default TARGET.target [root@localhost tmp]# grep system /etc/inittab # inittab is no longer used when using systemd. # Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target # systemd uses 'targets' instead of runlevels. By default, there are two main targets: # systemctl get-default # systemctl set-default TARGET.target [root@localhost tmp]# grep -i system /etc/inittab # inittab is no longer used when using systemd. # ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM. # Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target # systemd uses 'targets' instead of runlevels. By default, there are two main targets: # systemctl get-default # systemctl set-default TARGET.target [root@localhost tmp]# grep -v ^# /etc/inittab [root@localhost tmp]#
注:^#表示排除以#开头的行,由于所有行都以#开头,故没有结果。
待完善。。。