您的位置:首页 > 运维架构 > Linux

losbyday Linux查找命令

2016-08-25 10:31 316 查看
PS:第一次发表博客,试一下水,晚一点修改文本格式

linux下的命令都存放在/bin /sbin /usr/bin /usr/sbin路径下等
echo $PATH

which 是用来查询命令存放的路径,一般在PATH环境变量指定路径下去找(找到之后的对象必须具有可执行权限,后面的命令参数用外部命令,另找到对象后不会往下找)。
rpm -qf `which mkdir` 与rpm组合查找安装包

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

[root@teacher ~]# which mkdir
/bin/mkdir
[root@teacher ~]# whereis mkdir
mkdir: /bin/mkdir /usr/share/man/man3p/mkdir.3p.gz /usr/share/man/man1p/mkdir.1p.gz /usr/share/man/man1/mkdir.1.gz /usr/share/man/man2/mkdir.2.gz
[root@teacher ~]# touch aa
[root@teacher ~]# whereis aa
aa:
[root@teacher ~]# rm -rf aa
[root@teacher ~]# touch /bin/aa
[root@teacher ~]# whereis aa
aa: /bin/aa
以上的配置总结:
(1)whereis和which相同之处便是要寻找的对象都必须约束在PATH环境变量的命令路径中。
(2)whereis找到的结果除了命令的路径之外,还会显示该命令的帮助文档路径。whereis找到的对象可以不带执行权限(whereis的参数可以是命令或文件)
which查找的文件,需要具有可执行权限,如果没有执行权限,就算在PATH变量的路径下,也查找不到。
========
locate 命令
[root@teacher bin]# locate mkdir 遍历locate专用数据库文件(/var/lib/mlocate/mlocate.db数据库文件,该文件是整个linux目录树结构形成的镜像),查看所有包含mkdir字样的文件或命令,都查找出来
/bin/mkdir
/image/bin/mkdir
/image123/bin/mkdir
/test/bin/mkdir
/usr/bin/gnomevfs-mkdir
/usr/bin/gvfs-mkdir
/usr/lib/perl5/auto/POSIX/mkdir.al
/usr/share/aclocal-1.11/mkdirp.m4
/usr/share/man/man1/mkdir.1.gz
/usr/share/man/man1p/mkdir.1p.gz
/usr/share/man/man2/mkdir.2.gz
/usr/share/man/man2/mkdirat.2.gz
/usr/share/man/man3p/mkdir.3p.gz
[root@teacher bin]# which mkdir
/bin/mkdir

[root@teacher ~]# touch 1111111111
[root@teacher ~]# locate 1111111111 //对于新建的文件,locate数据库没有同步linux目录树结构,所以找不到1111111111
[root@teacher ~]# updatedb //更新locate专用数据库(同步现在linux目录树结构)
[root@teacher ~]# locate 1111111111
/root/1111111111
[root@teacher ~]# locate cut.sh
/root/cut.sh
[root@teacher ~]# mv cut.sh tuc.sh
[root@teacher ~]# locate cut.sh
/root/cut.sh
删除,移动,重命令某个文件后,也需要更新locate数据库。
Locate命令用户查找文件,它比find命令搜索的速度快,但它需要一个数据库,这个数据库由每天例行的工作(crontab)程序来建立。当我们建立好这个数据库后,就可以方便来搜索所需文件了。需要先运行updatedb
==========================================
===
find命令
用途:用于查找文件或目录
find - search for files in a directory hierarchy

SYNOPSIS
find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression] //专注点在于find要确定寻找范围和寻找条件
格式:find [查找范围] [查找条件] [-exec|-ok ...] 命令 {} \;
常用查找条件
-name:按文件名称查找
-size:按文件大小查找
-user:按文件属主查找
-type:按对象类型查找
-type 类型
f (file) 普通文件
d (directory) 目录(文件夹)
c 字符设备(character)
b 块设备 (block)
l (link) 链接文件
p 管道
-perm:按文件权限查找
-mtime:按文件内容更改时间查找(天数)
-atime:按对象被访问的时间查找(天数)
-ctime:按对象状态被修改的时间去查找(天数)
-mmin:按文件更改时间查找(分钟)
-amin
-cmin
====
[root@teacher ]# find / -name "zghhr" -type f -exec cp {} /zhou \;
find 是一个查找的命令
/ 查找的范围和位置
-name 根据名字来查找
zghhr 是文件名
-exec 执行后面的命令
cp 是复制的命令
{} 用来存放前面的find命令查找的结果。相当于一个容器,用来存放find找的结果
/zhou 是cp复制内容的目的地
\; 结束标志
===========
[root@losnau ~]# find / -name mysql_log -ok rm {} \;
< rm ... /pythonbak/root/mysql/mysql_log > ? n
< rm ... /root/mysql/mysql_log > ? n
< rm ... /root/mysql_log > ? n
[root@teacher ]#
-ok选项会提醒你是否要执行后面的删除命令;若不写明查找范围,则默认在当前目录下去找
=============
find命令支持通配符?和*,但需要用到双引号对“”。
[root@teacher ~]# touch zghhr1
[root@teacher ~]# touch zghhr2
[root@teacher ~]# find -name zghhr
[root@teacher ~]# find -name zghhr*
find: paths must precede expression: zghhr2
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
[root@teacher ~]# find -name zghhr?
find: paths must precede expression: zghhr2
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
[root@teacher ~]# find -name "zghhr?"
./zghhr2
./zghhr1
[root@teacher ~]# find -name "zghhr*"
./zghhr2
./zghhr1
[root@teacher ~]# find -name "zghhr*" -exec rm -rf {} \;
[root@teacher ~]# find -name "zghhr*" //验证之前删除带有“zghhr”字样的文件是否被删除。
[root@teacher ~]#
=============================
[root@teacher ~]# find /boot -name "vmlinuz*"|xargs ls -l
-rwxr-xr-x. 1 root root 4044560 Jan 30 2013 /boot/vmlinuz-2.6.32-358.el6.x86_64
[root@teacher ~]# find /boot -name "vmlinuz*" -exec ls -l {} \;
-rwxr-xr-x. 1 root root 4044560 Jan 30 2013 /boot/vmlinuz-2.6.32-358.el6.x86_64
以上两条命令的效果一样。
=============================
[root@teacher boot]# find /boot -size +1024k -o -name "vmlinuz*" |xargs ls -l
====
-a and
-o or
! not
[root@teacher ~]# find /boot -size +1M -a -name "vmlinuz*" -a -type f|xargs ls -l
-rwxr-xr-x. 1 root root 4044560 Jan 30 2013 /boot/vmlinuz-2.6.32-358.el6.x86_64
[root@teacher ~]# find /boot -size +1M -a -name "vmlinuz*" -a -type f|xargs ls -lh
-rwxr-xr-x. 1 root root 3.9M Jan 30 2013 /boot/vmlinuz-2.6.32-358.el6.x86_64
[root@teacher ~]# find /boot -size +1M -name "vmlinuz*" -type f|xargs ls -lh //-a的逻辑与功能可以省略
-rwxr-xr-x. 1 root root 3.9M Jan 30 2013 /boot/vmlinuz-2.6.32-358.el6.x86_64

[root@teacher ~]# find /boot -size +4M -o -name "vmlinuz*" |xargs ls -lh
-rw-r--r--. 1 root root 16M Jul 13 2014 /boot/initramfs-2.6.32-358.el6.x86_64.img //该文件满足空间大于4M
-rwxr-xr-x. 1 root root 3.9M Jan 30 2013 /boot/vmlinuz-2.6.32-358.el6.x86_64 //该文件满足名字中含有“vmlinuz”字样
[root@teacher ~]# find /boot -type d -name "grub*"|xargs ls -lh //查看找到的目录下的内容
total 274K
-rw-r--r--. 1 root root 63 Jul 13 2014 device.map
-rw-r--r--. 1 root root 14K Jul 13 2014 e2fs_stage1_5
-rw-r--r--. 1 root root 13K Jul 13 2014 fat_stage1_5
-rw-r--r--. 1 root root 12K Jul 13 2014 ffs_stage1_5
-rw-------. 1 root root 761 Jul 13 2014 grub.conf
-rw-r--r--. 1 root root 12K Jul 13 2014 iso9660_stage1_5
-rw-r--r--. 1 root root 13K Jul 13 2014 jfs_stage1_5
lrwxrwxrwx. 1 root root 11 Jul 13 2014 menu.lst -> ./grub.conf
-rw-r--r--. 1 root root 12K Jul 13 2014 minix_stage1_5
-rw-r--r--. 1 root root 15K Jul 13 2014 reiserfs_stage1_5
-rw-r--r--. 1 root root 1.4K May 7 2010 splash.xpm.gz
-rw-r--r--. 1 root root 512 Jul 13 2014 stage1
-rw-r--r--. 1 root root 124K Jul 18 01:38 stage2
-rw-r--r--. 1 root root 12K Jul 13 2014 ufs2_stage1_5
-rw-r--r--. 1 root root 12K Jul 13 2014 vstafs_stage1_5
-rw-r--r--. 1 root root 14K Jul 13 2014 xfs_stage1_5
[root@teacher ~]# find /boot -type d -name "grub*"|xargs ls -dlh //加上-d选项后,只查看目录本身的信息
drwxr-xr-x. 2 root root 1.0K May 5 22:14 /boot/grub

[root@teacher ~]# find /boot ! -type f //查找/boot下非文件对象(包含目录对象d,链接符对象l,字符设备对象c,块设备对象b)
/boot
/boot/grub
/boot/grub/menu.lst
/boot/lost+found
/boot/test
/boot/test/soft1
/boot/efi
/boot/efi/EFI
/boot/efi/EFI/redhat
[root@teacher ~]# find /boot -type d //精确匹配找的只有目录对象
/boot
/boot/grub
/boot/lost+found
/boot/test
/boot/efi
/boot/efi/EFI
/boot/efi/EFI/redhat

[root@teacher ~]# find /dev -type c //查找字符设备对象
[root@teacher ~]# find /dev -type b //查找块设备对象
[root@teacher ~]# find /etc/rc.d/rc5.d -type l -exec ls -l --color {} \; //查找链接型对象类型

[root@teacher ~]# find /boot -size +1M -size -5M
/boot/System.map-2.6.32-358.el6.x86_64
/boot/vmlinuz-2.6.32-358.el6.x86_64

[root@teacher ~]# find /boot -size +1M ! -size +4M -exec ls -lh {} \; //语法与前一条命令一样,但结果为完全显示,推荐用此法找容量范围的对象
-rw------- 1 root root 3.8M Jul 13 2014 /boot/initrd-2.6.32-358.el6.x86_64kdump.img
-rw-r--r--. 1 root root 2.3M Jan 30 2013 /boot/System.map-2.6.32-358.el6.x86_64
-rwxr-xr-x. 1 root root 3.9M Jan 30 2013 /boot/vmlinuz-2.6.32-358.el6.x86_64

-----------------------------------------
[root@mini ~]# date
Mon Jul 20 19:[root@mini ~]# find /home -user zs -type f
/home/zs/.bash_profile
/home/zs/.bashrc
/home/zs/.bash_history
/home/zs/.bash_logout
/home/zs/zsfile
43:28 CST 2015
[root@mini ~]# ll /lianxi
total 0
-rw-r--r--. 1 root root 0 Jul 17 19:31 file1
-rw-r--r--. 1 root root 0 Jul 20 19:29 file2
[root@mini ~]# touch -d "6 days ago" /lianxi/file2
[root@mini ~]# ll /lianxi
total 0
-rw-r--r--. 1 root root 0 Jul 17 19:31 file1
-rw-r--r--. 1 root root 0 Jul 14 19:37 file2
[root@mini ~]# find /lianxi -mtime +5
/lianxi/file2
[root@mini ~]# find /lianxi -mtime +3
/lianxi/file2
[root@mini ~]# find /lianxi -mtime +2
/lianxi/file1
/lianxi/file2
[root@mini ~]# touch /lianxi/file3
[root@mini ~]# find /lianxi -mmin -3
/lianxi
/lianxi/file3
[root@mini ~]# find /lianxi -mmin +3
/lianxi/file1
/lianxi/.hulijing
/lianxi/file2
[root@mini ~]# rm -rf /lianxi/.hulijing
[root@mini ~]# find /lianxi -mmin +3
/lianxi/file1
/lianxi/file2
--------------------------------------
[root@mini ~]# find /home -user zs -type f
/home/zs/.bash_profile
/home/zs/.bashrc
/home/zs/.bash_history
/home/zs/.bash_logout
/home/zs/zsfile
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: