Linux运维系统工程师系列07

本文发布时间: 2019-Mar-22
文件管理链接:两种:硬链接:软链接:符号链接(类似windows里的快捷方式)硬链接:[[email protected] ~]# mkdir hlink[[email protected] ~]# ll -d hlink/drwxr-xr-x. 2 root root 4096 Oct 16 15:04 hlink/上面的2就是表示硬链接数是2。[[email protected] ~]# mkdir hlink[[email protected] ~]# ll -d hlink/drwxr-xr-x. 2 root root 4096 Oct 16 15:04 hlink/[[email protected] ~]# /root/hlink/^C[[email protected] ~]# cd hlink/[[email protected] hlink]# ls -a. ..2 : /root/hlink/root/hlink/.[[email protected] hlink]# mkdir lk[[email protected] hlink]# ll -d /root/hlink/drwxr-xr-x. 3 root root 4096 Oct 16 15:07 /root/hlink/3:/root/hlink/root/hlink/./root/hlink/lk/..[[email protected] hlink]# cd lk/[[email protected] lk]# ls -a. ..[[email protected] lk]# pwd/root/hlink/lk——人为创建硬链接:ln命令:[[email protected] hlink]# touch hl[[email protected] hlink]# ll hl-rw-r--r--. 1 root root 0 Oct 16 15:15 hl[[email protected] hlink]# ln hl /test/hard[[email protected] hlink]# ll hl-rw-r--r--. 2 root root 0 Oct 16 15:15 hl[[email protected] test]# ls -li hard1197063 -rw-r--r--. 2 root root 0 Oct 16 15:15 hard[[email protected] test]# ls -li /root/hlink/hl lk/[[email protected] test]# ls -li /root/hlink/hl1197063 -rw-r--r--. 2 root root 0 Oct 16 15:15 /root/hlink/hli —— inode i节点,里面存文件的一些属性等等。i节点相同表示同一个文件软链接: symbolic link(符号链接)ln -s 源 目的[[email protected] ~]# ln -s /root/hlink/ /test/sl[[email protected] ~]# ll -d /test/sllrwxrwxrwx. 1 root root 12 Oct 16 15:23 /test/sl -> /root/hlink/[[email protected] ~]# echo hello > /root/hlink/hello.txt[[email protected] ~]# cd /testtest/ test1/[[email protected] ~]# cd /test/sl[[email protected] sl]# lltotal 8-rw-r--r--. 1 root root 6 Oct 16 15:24 hello.txt-rw-r--r--. 2 root root 0 Oct 16 15:15 hldrwxr-xr-x. 3 root root 4096 Oct 16 15:12 lk[[email protected] sl]# pwd/test/sl[[email protected] sl]# cd /root/hlink/[[email protected] hlink]# pwd/root/hlink[[email protected] hlink]# lltotal 8-rw-r--r--. 1 root root 6 Oct 16 15:24 hello.txt-rw-r--r--. 2 root root 0 Oct 16 15:15 hldrwxr-xr-x. 3 root root 4096 Oct 16 15:12 lk[[email protected] hlink]# ll -d /test/sllrwxrwxrwx. 1 root root 12 Oct 16 15:23 /test/sl -> /root/hlink/[[email protected] hlink]# ll -d /root/hlinkdrwxr-xr-x. 3 root root 4096 Oct 16 15:27 /root/hlink符号链接文件的大小不是文件实际大小,而是它所指向的路径的长度。/root/hlink/=12软硬链接的区别:1、软链接和源文件的权限不一样;硬链接与源文件的权限一致。2、软链接可以跨分区;硬链接不可以跨分区。3、软链接可以对目录做链接;硬链接不可以对目录做链接。文件的搜索which 可执行命令——能够显示shell命令的绝对路径[[email protected] ~]# which passwd/usr/bin/passwd[[email protected] ~]# which vim/usr/bin/vim[[email protected] ~]# which vi/bin/vi——仅能在PATH变量中指定的路径中搜索可执行的命令PATH:命令的搜索路径[[email protected] ~]# echo $PATH/usr/lib/qt-3.3/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/root/bin[[email protected] sgid]# which ugid_vi/usr/bin/which: no ugid_vi in (/usr/lib/qt-3.3/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/root/bin)——添加路径到PATH:[[email protected] sgid]# PATH=$PATH:/sgid[[email protected] sgid]# echo $PATH/usr/lib/qt-3.3/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/root/bin:/sgid[[email protected] sgid]# which ugid_vi/sgid/ugid_vilocate:——通过文件名检索,检索到的东西都会存在数据库里——局限性:有的文件系统、文件、目录不搜索比如/tmp不搜索[[email protected] ~]# locate vim[[email protected] ~]# locate myvi明明有,但是没搜到,why?是因为没有更新数据库更新数据库的命令是:updatedb[[email protected] sgid]# locate ugid_file/sgid/ugid_file[[email protected] sgid]# touch abcdefg 新建的文件,想要搜索到,需要先更新库[[email protected] sgid]# locate abcdefg[[email protected] sgid]# updatedb[[email protected] sgid]# locate abcdefg/sgid/abcdefg数据库数据文件存放位置:/var/lib/mlocate/mlocate.db数据库的配置文件:[[email protected] mlocate]# cat /etc/updatedb.confPRUNE_BIND_MOUNTS = "yes"PRUNEFS = "9p afs anon_inodefs auto autofs bdev binfmt_misc cgroup cifs coda configfs cpuset debugfs devpts ecryptfs exofs fuse fusectl gfs gfs2 hugetlbfs inotifyfs iso9660 jffs2 lustre mqueue ncpfs nfs nfs4 nfsd pipefs proc ramfs rootfs rpc_pipefs securityfs selinuxfs sfs sockfs sysfs tmpfs ubifs udf usbfs vmhgfs"PRUNENAMES = ".git .hg .svn"PRUNEPATHS = "/afs /media /net /sfs /tmp /udev /var/cache/ccache /var/spool/cups /var/spool/squid /var/tmp"由于配置文件设置,不能搜索/tmp路径下的文件[[email protected] tmp]# cd suid/[[email protected] suid]# lltotal 708-rwsr-xr-x. 1 xh root 720792 Oct 16 10:09 myvi-rw-rw-r--. 1 xh xh 9 Oct 16 10:11 xh.file[[email protected] suid]# locate myvi[[email protected] suid]# pwd/tmp/suidwhereis:不常用,查找一个命令的二进制文件、源代码及有没有man手册[[email protected] suid]# whereis lsls: /bin/ls /usr/share/man/man1p/ls.1p.gz /usr/share/man/man1/ls.1.gz[[email protected] suid]# whereis pwdpwd: /bin/pwd /usr/share/man/man1p/pwd.1p.gz /usr/share/man/man1/pwd.1.gzfind:很强大全局搜索文件的,工作方式就是沿着文件层次结构遍历,找到符合条件的可以执行相应的操作。1、基本语法格式:find 路径 搜索选项 参数 执行的动作2、基本例子:[[email protected] test]# find /etc -name passwd/etc/pam.d/passwd/etc/passwd看下查询效率[[email protected] test]# mkdir -p /find/a/b/c/d/e/f/g/h/i/j/k/l/m/n[[email protected] test]# touch /find/a/b/c/d/e/f/g/h/i/j/k/l/m/n/google.txt[[email protected] test]# find /find -name google.txt/find/a/b/c/d/e/f/g/h/i/j/k/l/m/n/google.txt3、按条件搜索1)-name:文件名搜索[[email protected] ~]# find /etc/ -name ntp.conf/etc/ntp.conf通配符:*搜索/etc下,所有以.conf结尾的文件[[email protected] ~]# find /etc/ -name *.conf-iname:文件名搜索,忽略大小写[[email protected] find]# find . -iname passwd./PASSWD./passwd2)-type:按照文件类型查找b:块设备c:字符设备d:目录文件l:软链接文件s:套接字文件p:管道文件-:普通文件查找/var目录下的软链接文件[[email protected] ~]# find /var/ -type l/var/lib/gdm/.pulse/e532a814bb540a5d385d60a300000019-runtime/var/mail[[email protected] ~]# ll `find /var/ -type l`lrwxrwxrwx. 1 gdm gdm 23 Oct 9 16:36 /var/lib/gdm/.pulse/e532a814bb540a5d385d60a300000019-runtime -> /tmp/pulse-o9Qfwuf6KGhjlrwxrwxrwx. 1 root root 10 Oct 9 16:14 /var/mail -> spool/mail[[email protected] ~]# find /tmp -type d注意:搜索条件是可以同时使用多个的例子:查找/find目录下,文件名为pass,文件类型是软链接的文件[[email protected] ~]# cd /find/[[email protected] find]# ln -s passwd pass[[email protected] find]# find /find -name pass -type l/find/pass3)按照时间查找什么时候会用到呢?增量备份。-atime n: 表示n*24小时,也就是说以小时为单位的-ctime n:-mtime n:+n:大于-n:小于n:n到n+1之间-mtime 3:图[[email protected] ~]# mkdir /test[[email protected] ~]# cp /etc/passwd /test/[[email protected] ~]# cp /etc/passwd /test/passwd1[[email protected] ~]# cp /etc/passwd /test/passwd2[[email protected] ~]# cp /etc/passwd /test/passwd3[[email protected] ~]# dateThu Oct 17 11:23:02 CST 2013[[email protected] ~]# echo "hello 17" > /test/passwd[[email protected] ~]# date -s "2013-10-18 11:24:00"Fri Oct 18 11:24:00 CST 2013[[email protected] ~]# echo "hello 18" > /test/passwd1[[email protected] ~]# date -s "2013-10-19 11:24:20"Sat Oct 19 11:24:20 CST 2013[[email protected] ~]# echo "hello 19" > /test/passwd2[[email protected] ~]# date -s "2013-10-20 11:25:00"Sun Oct 20 11:25:00 CST 2013[[email protected] ~]# echo "hello 20" > /test/passwd3[[email protected] ~]# ll /test/passwd*-rw-r--r--. 1 root root 9 Oct 17 11:23 /test/passwd-rw-r--r--. 1 root root 9 Oct 18 11:24 /test/passwd1-rw-r--r--. 1 root root 9 Oct 19 11:24 /test/passwd2-rw-r--r--. 1 root root 9 Oct 20 11:25 /test/passwd3——查找距离当前时间24小时之内的[[email protected] ~]# find /test/ -mtime 0/test/passwd3——查找距离当前时间大于24*n小时,小于24*(n+1)小时之间被修改过的文件这里是24~48之间[[email protected] ~]# find /test -mtime 1/test/passwd2[[email protected] ~]# find /test/ -type f -mtime +1/test/passwd1/test/passwd——查找的是距离当前时间(n+1)*24小时之前修改过的文件[[email protected] ~]# find /test/ -type f -mtime +1——查找的是距离当前时间n*24小时之内被修改的文件[[email protected] ~]# find /test/ -type f -mtime -2/test/passwd3/test/passwd2-amin n ——以分钟为单位,minute(分钟)-cmin n-mmin n[[email protected] ~]# ll /test/passwd*-rw-r--r--. 1 root root 9 Oct 17 11:23 /test/passwd-rw-r--r--. 1 root root 9 Oct 18 11:24 /test/passwd1-rw-r--r--. 1 root root 9 Oct 19 11:24 /test/passwd2-rw-r--r--. 1 root root 9 Oct 20 11:25 /test/passwd3[[email protected] ~]# dateSun Oct 20 11:41:24 CST 2013[[email protected] ~]# find /test/ -type f -mmin -20/test/passwd3距离当前时间20分钟之前修改的文件[[email protected] ~]# find /test/ -type f -mmin +20/test/passwd1/test/passwd2/test/passwd4)按照用户和组来查找-user 用户名:查找所有者是某人的文件-group 组名:查找所属组是某个组的文件-nouser: 没有所有者的文件,孤儿文件-nogroup:没有所属组的文件-uid uid:查找uid是某某的文件-gid gid:查找gid是某某的文件[[email protected] ~]# useradd uplooking[[email protected] ~]# su - uplooking[[email protected] ~]$ touch file[[email protected] ~]$ exitlogout查找所有者是uplooking的文件[[email protected] ~]# find /home -user uplooking查找所属组是uplooking的文件[[email protected] ~]# find /home -group uplooking查找uid是528的文件[[email protected] ~]# find /home -uid 528查找gid是528的文件[[email protected] ~]# find /home -gid 528演示nouser和nogroup[[email protected] ~]# userdel uplooking[[email protected] ~]# ll -d /home/uplooking/drwx------. 4 528 528 4096 Oct 20 11:49 /home/uplooking/[[email protected] ~]# find /home -nouser/home/uplooking/home/uplooking/.bash_history/home/uplooking/.bash_profile/home/uplooking/.bashrc/home/uplooking/.bash_logout/home/uplooking/file/home/uplooking/.gnome2/home/uplooking/.mozilla/home/uplooking/.mozilla/plugins/home/uplooking/.mozilla/extensions[[email protected] ~]# find /home -nogroup/home/uplooking/home/uplooking/.bash_history/home/uplooking/.bash_profile/home/uplooking/.bashrc/home/uplooking/.bash_logout/home/uplooking/file/home/uplooking/.gnome2/home/uplooking/.mozilla/home/uplooking/.mozilla/plugins/home/uplooking/.mozilla/extensions5)按照权限查找-perm mode —— 精确匹配-perm -mode —— 模糊匹配-perm +mode —— 模糊匹配[[email protected] test]# touch p{r,w,x}_{1,2,3}[[email protected] test]# lspr_1 pr_2 pr_3 pw_1 pw_2 pw_3 px_1 px_2 px_3[[email protected] test]# chmod 400 pr_1[[email protected] test]# chmod 440 pr_2[[email protected] test]# chmod 444 pr_3[[email protected] test]# chmod 200 pw_1[[email protected] test]# chmod 220 pw_2[[email protected] test]# chmod 222 pw_3[[email protected] test]# chmod 100 px_1[[email protected] test]# chmod 110 px_2[[email protected] test]# chmod 111 px_3查找的是只有组上有写权限的[[email protected] test]# find /test -perm g=w (020)[[email protected] test]# touch px_4[[email protected] test]# chmod 020 px_4[[email protected] test]# find /test -perm g=w/test/px_4-perm +mode表示组必须有写权限,其它的不限制[[email protected] test]# find /test -perm +020/test/pw_3/test/px_4/test/pw_2表示的是只要有写权限就符合要求[[email protected] test]# find /test -perm +222/test/test/pw_3/test/px_4/test/pw_1/test/pw_2-perm -mode表示的是最少要拥有222权限的文件[[email protected] test]# find /test -perm -222/test/pw_3[[email protected] test]# ll /test/pw_3--w--w--w-. 1 root root 0 Oct 20 13:37 /test/pw_3思考:查找所有具有suid权限的文件6)按照文件大小查找-size n[cwbkMG]+nk:大于多少k的-nk:小于多少k的nk:等于多少k的dd命令:转换和拷贝文件[[email protected] test]# ll /dev/zerocrw-rw-rw-. 1 root root 1, 5 Oct 9 16:33 /dev/zerodd格式:[[email protected] test]# dd if=/dev/zero of=f1M bs=1M count=1if:input file输入文件of:output file输出文件bs:block size 块的大小count:数量[[email protected] test]# dd if=/dev/zero of=f4M bs=1M count=4[[email protected] test]# dd if=/dev/zero of=f7M bs=1M count=7[[email protected] test]# lltotal 12288-rw-r--r--. 1 root root 1048576 Oct 20 14:05 f1M-rw-r--r--. 1 root root 4194304 Oct 20 14:07 f4M-rw-r--r--. 1 root root 7340032 Oct 20 14:08 f7M[[email protected] test]# find /test -type f -size +1M/test/f4M/test/f7M[[email protected] test]# find /test -type f -size -7M/test/f4M/test/f1M-and与-a:并且-o:或者-not与!:非查找/test目录下文件大小大于等于4M的文件[[email protected] test]# find /test/ -size +4M -o -size 4M/test/f4M/test/f7M执行动作actions:-exec action——不提示直接将动作执行-ok action——执行动作之前先提示查找到文件大小为4M的文件,列出其详细信息[[email protected] test]# find /test -size 4M -exec ls -l {} \;-rw-r--r--. 1 root root 4194304 Oct 20 14:07 /test/f4M{}:用你find到的结果来替代\;:表示命令结束[[email protected] test]# find /test -size 4M -exec cp {} {}.bak \;


(以上内容不代表本站观点。)
---------------------------------
本网站以及域名有仲裁协议。
本網站以及域名有仲裁協議。

2020-Jul-13 01:14am
栏目列表