文件查找
1.which 用于显示命令的完全路径 [root@localhost ~]# which ls alias ls='ls --color=tty' /bin/ls //命令的完全路径是/bin/ls [root@localhost ~]# echo $PATH /usr/lib/qt-3.3/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin [root@localhost ~]# which cd /usr/bin/which: no cd in (/usr/lib/qt-3.3/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin) which 查找命令的路径是环境变量规定的路径2.whereis 用于查找命令的的路径、该文件的帮助文件、该文件的相关程序的路径
[root@localhost ~]# whereis ls ls: /bin/ls /usr/share/man/man1p/ls.1p.gz /usr/share/man/man1/ls.1.gz3.locate
所有包含关键字段的文件和目录都会显示。locate是搜索一个数据库,数据库里包括当前目录。 [root@localhost ~]# locate passwd /etc/passwd /etc/passwd- /etc/pam.d/passwd /etc/security/opasswd /lib/security/pam_passwdqc.so /lib/security/pam_unix_passwd.so …… 新建立的文件的目录无法自动写入数据库,需手动更新数据库 [root@localhost ~]# touch Loveyouforever [root@localhost ~]# locate Loveyouforever//无法找到文件目录 [root@localhost ~]# updatedb//手动更新数据库 [root@localhost ~]# locate Loveyouforever /root/Loveyouforever//找到文件目录4.find
非常强大的查找命令,现对花费时间会很长,非常消耗资源。很常用 格式 find [目录] [选项] [条件表达式] [-exec,-ok……]指令 {} \; find /etc -name passwd -exec ls -l {} \; 在目录/etc下查找name是passwd 放在{}给ls -l执行 执行结束 选项: -user:查找文件拥有者符合条件的文件 -nouser :没有拥有者的文件 [root@localhost ~]# su - zhx [zhx@localhost ~]$ find ./ -user zhx -exec ls -l {} \; 总计 8 drwxrwxr-x 2 zhx zhx 4096 01-19 00:44 zhx -rw-r--r-- 1 zhx zhx 124 01-19 00:43 ./.bashrc -rw-r--r-- 1 zhx zhx 515 01-19 00:43 ./.emacs 总计 16 drwxr-xr-x 2 zhx zhx 4096 01-19 00:43 extensions drwxr-xr-x 2 zhx zhx 4096 01-19 00:43 plugins 总计 0 总计 0 -rw------- 1 zhx zhx 56 01-19 00:45 ./.bash_history -rw-r--r-- 1 zhx zhx 176 01-19 00:43 ./.bash_profile 总计 0 -rw-r--r-- 1 zhx zhx 33 01-19 00:43 ./.bash_logout 总计 8 drwxr-xr-x 2 zhx zhx 4096 01-19 00:43 Autostart 总计 0 -rw-r--r-- 1 zhx zhx 381 01-19 00:43 ./.kde/Autostart/.directory -rw-r--r-- 1 zhx zhx 658 01-19 00:43 ./.zshrc [root@localhost ~]# userdel zhx//删除用户 [root@localhost ~]# find /home/zhx -nouser -exec ls -l {} \;在/home/zhx 查找孤儿文件,因为zhx已被删除 zhx家目录下的所有文件都属于孤儿文件 总计 8 drwxrwxr-x 2 500 500 4096 01-19 00:44 zhx -rw-r--r-- 1 500 500 124 01-19 00:43 /home/zhx/.bashrc -rw-r--r-- 1 500 500 515 01-19 00:43 /home/zhx/.emacs 总计 16 drwxr-xr-x 2 500 500 4096 01-19 00:43 extensions drwxr-xr-x 2 500 500 4096 01-19 00:43 plugins 总计 0 总计 0 -rw------- 1 500 500 98 01-19 00:46 /home/zhx/.bash_history -rw-r--r-- 1 500 500 176 01-19 00:43 /home/zhx/.bash_profile 总计 0 -rw-r--r-- 1 500 500 33 01-19 00:43 /home/zhx/.bash_logout 总计 8 drwxr-xr-x 2 500 500 4096 01-19 00:43 Autostart 总计 0 -rw-r--r-- 1 500 500 381 01-19 00:43 /home/zhx/.kde/Autostart/.directory -rw-r--r-- 1 500 500 658 01-19 00:43 /home/zhx/.zshrc [root@localhost ~]#-group -nogroup找属组满足条件的文件 用法同-user -nouser -perm 查找权限满足条件的文件 find -perm num 查找权限是num的文件 -perm -num 查找权限全部包括num的文件 -perm +num 查找权限部分包括num的文件
[root@localhost ~]# find / -perm 777 -ok ls -l {} \;|more
< ls ... /bin/sh > ? < ls ... /bin/traceroute6 > ? < ls ... /bin/egrep > ? lrwxrwxrwx 1 root root 10 01-15 04:51 /bin/traceroute6 -> traceroute -size 查找文件大小满足条件的文件 -size +num (单位) 大于num(单位)的文件 -size -num(单位) 小于num(单位)的文件 [root@localhost ~]# dd if=/dev/zero of=/root/hello bs=1M count=200 200+0 records in 200+0 records out 209715200 bytes (210 MB) copied, 1.01237 seconds, 207 MB/s [root@localhost ~]# find ./ -size +100M -exec ls -l {} \; -rw-r--r-- 1 root root 209715200 01-19 01:05 ./hello-type查找某一类型的文件,主要有以下几种
条件 b 块设备文件 d 目录 c 字符设备文件 p 管道文件 l 链接文件 f 普通文件find -mtime -n +n
-n n天之内 +n n天以为 n n天以前的 24时小时以内 打包归档及压缩 1.unzip,zip -l列出压缩文件多包含的内容 -v显示详细的压缩过程 [root@localhost ~]# zip -lv cc.zip cc//压缩cc 为cc.zip adding: cc/ (in=0) (out=0) (stored 0%) total bytes=0, compressed=0 -> 0% savings [root@localhost ~]# ls anaconda-ks.cfg cc.zip install.log Loveyouforever cc Desktop install.log.syslog 小鸟chacha初学linux.ncd2.gzip和zcat
-#:压缩等级。-1最快,压缩比最差;-9最慢,压缩比最小 [root@localhost ~]# gzip -9 Loveyouforever [root@localhost ~]# ls anaconda-ks.cfg Desktop install.log.syslog 小鸟chacha初学linux.ncd cc install.log Loveyouforever.gz [root@localhost temp]# zcat redhat.txt.gz what do you want to do? shut up3.tar
-c 建立压缩文件 -x解压文件 -v显示压缩 解压过程 -f 紧跟压缩文件名称 -t 查看压缩文件 [root@localhost /]# tar -cvf root.tar /root cvf压缩的常用组合 tar: 从成员名中删除开头的“/” /root/ /root/小鸟chacha初学linux.ncd /root/.metacity/ /root/.metacity/sessions/ /root/.metacity/sessions/1326903016-2435-745770735.ms /root/.metacity/sessions/1326578140-2447-2422197700.ms /root/.metacity/sessions/1326653136-2469-468411399.ms /root/.metacity/sessions/1326575460-2584-1184827547.ms /root/.metacity/sessions/1326744477-2478-3945392455.ms /root/.config/ /root/.config/gtk-2.0/ /root/.config/gtk-2.0/gtkfilechooser /root/.nautilus/ /root/.nautilus/metafiles/ …… [root@localhost /]# tar -tvf root.tar //查看tar文件中的内容 drwxr-x--- root/root 0 2012-01-19 02:34:28 root/ -rw------- root/root 22215 2012-01-19 02:34:28 root/小鸟chacha初学linux.ncd drwx------ root/root 0 2012-01-15 05:11:00 root/.metacity/ drwx------ root/root 0 2012-01-19 00:10:16 root/.metacity/sessions/ -rw-r--r-- root/root 355 2012-01-19 00:10:16 root/.metacity/sessions/1326903016-2435-745770735.ms -rw-r--r-- root/root 355 2012-01-15 05:55:40 root/.metacity/sessions/1326578140-2447-2422197700.ms -rw-r--r-- root/root 355 2012-01-16 02:45:36 root/.metacity/sessions/1326653136-2469-468411399.ms -rw-r--r-- root/root 82 2012-01-15 05:11:00 root/.metacity/sessions/1326575460-2584-1184827547.ms -rw-r--r-- root/root 82 2012-01-17 04:07:57 root/.metacity/sessions/1326744477-2478-3945392455.ms [root@localhost /]# tar --exclude root.tar -cvf root1.tar /root//排除 root.tar 进行压缩 tar: 从成员名中删除开头的“/” /root/ /root/小鸟chacha初学linux.ncd /root/.metacity/ /root/.metacity/sessions/ /root/.metacity/sessions/1326903016-2435-745770735.ms /root/.metacity/sessions/1326578140-2447-2422197700.ms /root/.metacity/sessions/1326653136-2469-468411399.ms /root/.metacity/sessions/1326575460-2584-1184827547.ms /root/.metacity/sessions/1326744477-2478-3945392455.ms [root@localhost /]# tar -xvf root.tar//解压 root/ root/小鸟chacha初学linux.ncd root/.metacity/ root/.metacity/sessions/ root/.metacity/sessions/1326903016-2435-745770735.ms root/.metacity/sessions/1326578140-2447-2422197700.ms root/.metacity/sessions/1326653136-2469-468411399.ms root/.metacity/sessions/1326575460-2584-1184827547.ms root/.metacity/sessions/1326744477-2478-3945392455.ms root/.config/ root/.config/gtk-2.0/ root/.config/gtk-2.0/gtkfilechooser root/.nautilus/ [root@localhost /]# tar -xvf root.tar root/ root/小鸟chacha初学linux.ncd root/.metacity/ root/.metacity/sessions/ root/.metacity/sessions/1326903016-2435-745770735.ms root/.metacity/sessions/1326578140-2447-2422197700.ms root/.metacity/sessions/1326653136-2469-468411399.ms root/.metacity/sessions/1326575460-2584-1184827547.ms root/.metacity/sessions/1326744477-2478-3945392455.ms root/.config/ root/.config/gtk-2.0/ root/.config/gtk-2.0/gtkfilechooser root/.nautilus/ [root@localhost ~]# tar -zcvf root.tar.gz ./ //打包并且以gzip的方式压缩文件,bzip2只需要改为-jcvf ./ ./小鸟chacha初学linux.ncd ./.metacity/ ./.metacity/sessions/ ./.metacity/sessions/1326903016-2435-745770735.ms ./.metacity/sessions/1326578140-2447-2422197700.ms ./.metacity/sessions/1326653136-2469-468411399.ms ./.metacity/sessions/1326575460-2584-1184827547.ms ./.metacity/sessions/1326744477-2478-3945392455.ms ./.config/ ./.config/gtk-2.0/ ./.config/gtk-2.0/gtkfilechooser ./.nautilus/ ./.nautilus/metafiles/ ./.nautilus/metafiles/computer:%2F%2F%2F.xml ./.nautilus/metafiles/file:%2F%2F%2F.xml ./.nautilus/metafiles/file:%2F%2F%2Fmedia.xml ./.nautilus/metafiles/file:%2F%2F%2Froot%2FDesktop.xml ./.nautilus/metafiles/x-nautilus-desktop:%2F%2F%2F.xml ./.nautilus/metafiles/file:%2F%2F%2Froot.xml ./.nautilus/saved2WNYv0 ./.eggcups/