美文网首页
第五周:Linux文件查找与压缩

第五周:Linux文件查找与压缩

作者: 还没帮马里奥再闯一关 | 来源:发表于2020-12-30 00:00 被阅读0次

    1、编写脚本 createuser.sh,实现如下功能:使用一个用户名做为参数,如果 指定参数的用户存在,就显示其存在,否则添加之;显示添加的用户的id号等信息

    ---------------------------------------------if条件判断
    [root@Top ~]# cat creatuser.sh 
    #!/bin/bash
    read -p "Please enter a username:" USER_NAME
    if `getent passwd $USER_NAME > /dev/null`;then
        echo "This user already exists!"
    else useradd $USER_NAME
    echo `getent passwd $USER_NAME`
    fi
    
    [root@Top ~]# bash creatuser.sh 
    Please enter a username:user5
    user5:x:2007:2007::/home/user5:/bin/bash
    
    [root@Top ~]# bash creatuser.sh 
    Please enter a username:user1
    This user already exists!
    
    ----------------------------------------------短路与短路或条件判断
    #!/bin/bash
    getent passwd $1 > /dev/null && echo "This user already exists!" || (useradd $1; getent passwd $1)
    
    [root@Top ~]# bash creatuser1.sh user1
    This user already exists!
    
    [root@Top ~]# bash creatuser1.sh user7
    user7:x:2009:2009::/home/user7:/bin/bash
    

    2、编写生成脚本基本格式的脚本,包括作者,联系方式,版本,时间,描述等

    set ignorecase
    set cursorline
    set autoindent
    set nu
    set paste
    set et
    set tabstop=4
    autocmd BufNewFile *.sh exec ":call SetTitle()"
    func SetTitle()
            if expand("%:e") == 'sh'
                    call setline(1,"#!/bin/bash")
                    call setline(2,"#")
                    call setline(3,"#******************************************************************************")
                    call setline(4,"#Author:                            LinXiaoLong")
                    call setline(5,"#Phone:                             18509163451")
                    call setline(6,"#Mail:                              18509163451@163.com")
                    call setline(7,"#Date:                              ".strftime("%Y-%m-%d"))
                    call setline(8,"#FileName:                          ".expand("%"))
                    call setline(9,"#******************************************************************************")
                    call setline(10,"set -ue")
                    call setline(11,"")
                    
            endif
    endfunc
    autocmd BufNewFile * normal G
    

    3、查找/etc目录下大于1M且类型为普通文件的所有文件

    ⏱ 20:25:17-root:~# find /etc -size +1M -a -type f -exec ls -lh {} \;
    -rw-r--r-- 1 root root 8.2M Sep 22 06:56 /etc/selinux/targeted/policy/policy.31
    -r--r--r-- 1 root root 8.8M Dec 13 02:08 /etc/udev/hwdb.bin
    

    4、打包/etc/目录下面所有conf结尾的文件,压缩包名称为当天的时间,并拷贝到/usr/local/src目录备份。

    find /etc/ -name "*.conf" | xargs tar czvf `date +%F`.tar.gz && cp -a `date +%F`.tar.gz /usr/local/src
    find /etc/ -name "*.conf" | xargs tar czvf `date +%F`.tar.gz;cp -a `date +%F`.tar.gz /usr/local/src
    #两种方式均可,第一种方法似乎更好
    

    5、查找当前系统上没有属主或属组,且最近一个周内曾被访问过的文件或目录

    ⏱ 22:36:13-root:/date# find / \( -nouser -o -nogroup \) -atime -7 -exec stat {} \;
    find: ‘/proc/3712/task/3712/fd/9’: No such file or directory
    find: ‘/proc/3712/task/3712/fdinfo/9’: No such file or directory
    find: ‘/proc/3712/fd/8’: No such file or directory
    find: ‘/proc/3712/fdinfo/8’: No such file or directory
      File: /opt/home/mage
      Size: 62          Blocks: 0          IO Block: 4096   directory
    Device: fd00h/64768d    Inode: 134859146   Links: 2
    Access: (0700/drwx------)  Uid: ( 1007/ UNKNOWN)   Gid: ( 1007/ UNKNOWN)
    Access: 2020-12-29 22:08:36.192285090 +0800         #访问时间
    Modify: 2020-11-15 18:21:00.000000000 +0800
    Change: 2020-12-06 15:03:05.263408864 +0800
     Birth: -
      File: /opt/home/wang
      Size: 62          Blocks: 0          IO Block: 4096   directory
    Device: fd00h/64768d    Inode: 201330291   Links: 2
    Access: (0700/drwx------)  Uid: ( 1008/ UNKNOWN)   Gid: ( 1008/ UNKNOWN)
    Access: 2020-12-29 22:08:36.192285090 +0800     #访问时间
    Modify: 2020-11-15 18:21:05.000000000 +0800
    Change: 2020-12-06 15:03:05.263408864 +0800
     Birth: -
      File: /opt/home/hc
      Size: 62          Blocks: 0          IO Block: 4096   directory
    Device: fd00h/64768d    Inode: 827204      Links: 2
    Access: (0700/drwx------)  Uid: ( 1011/ UNKNOWN)   Gid: ( 1011/ UNKNOWN)
    Access: 2020-12-29 22:25:21.882249589 +0800    #访问时间
    Modify: 2020-12-29 22:25:09.742310375 +0800
    Change: 2020-12-29 22:25:09.742310375 +0800
     Birth: -
      File: /opt/home/hc/.bash_logout
      Size: 18          Blocks: 8          IO Block: 4096   regular file
    Device: fd00h/64768d    Inode: 827205      Links: 1
    Access: (0644/-rw-r--r--)  Uid: ( 1011/ UNKNOWN)   Gid: ( 1011/ UNKNOWN)
    Access: 2020-12-29 22:25:07.040323903 +0800     #访问时间
    Modify: 2019-11-09 00:21:40.000000000 +0800
    Change: 2020-12-06 15:03:05.263408864 +0800
     Birth: -
    

    6、查找/etc目录下至少有一类用户没有执行权限的文件

    ⏱ 23:54:06-root:/date# find /etc ! -perm /111 -exec ls -l {} \;|more (说实话,题干还是似懂非懂)
    -rw-r--r--. 1 root root 655 Nov  8 13:09 /etc/fstab
    -rw-------. 1 root root 0 Nov  8 13:09 /etc/crypttab
    -rw-r--r-- 1 root root 266 Dec 29 19:46 /etc/resolv.conf
    -rw-r--r--. 1 root root 80 Nov  8 13:10 /etc/dnf/modules.d/satellite-5-client.module
    ......
    

    相关文章

      网友评论

          本文标题:第五周:Linux文件查找与压缩

          本文链接:https://www.haomeiwen.com/subject/cnlcoktx.html