Find files and text processing

Find files and text processing

  • Define an alias commands into force all users
    example: lftps = 'lftp 172.168.0.1/pub'
    [root@localhost /]# echo "alias lftps='lftp 172.168.0.1/pub'" >> /etc/bashrc
    [root@localhost /]# source /etc/bashrc 
    [root@localhost /]# alias
    alias cp='cp -i'
    alias egrep='egrep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias grep='grep --color=auto'
    alias l.='ls -d .* --color=auto'
    alias lftps='lftp 172.168.0.1/pub'
    alias ll='ls -l --color=auto'
    alias ls='ls --color=auto'
    alias mv='mv -i'
    alias rm='rm -i'
    alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
  • Show / etc / passwd file, not to / bin / bash at the end of the line
    [root@localhost /]# grep -v "/bin/bash$" /etc/passwd

  • Line find / etc / passwd file, contains the two-digit or three-digit
    [root@localhost /]# grep "\<[1-9]\{2,3\}\>" /etc/passwd

  • Display / proc / meminfo file to start with a lowercase or uppercase S lines; implemented in three ways
    (1)[root@localhost /]# grep  -i '^s' /proc/meminfo
    (2)[root@localhost /]# grep   '^[sS]' /proc/meminfo 
    (3)[root@localhost /]# grep -E "^(s|S)" /proc/meminfo 
    SwapCached:            0 kB
    SwapTotal:       2097148 kB
    SwapFree:        2097148 kB
    Shmem:              7980 kB
    Slab:              64752 kB
    SReclaimable:      28244 kB
    SUnreclaim:        36508 kB
  • Echo out an absolute path using the path name egrep removed,
    similar to the result of performing dirname / etcpasswd of
    [root@localhost /]# echo "/etc/sysconfig/netscript " |  egrep -o  '^/.*/'
    /etc/sysconfig/
  • Find ip address in the ifconfig required results show only the IP address
    [root@localhost /]# ifconfig  |  egrep -o  '([0-9]{1,3}\.){3}[0-9]{1,3}' 
    172.20.10.7
    255.255.255.240
    172.20.10.15
    127.0.0.1
    255.0.0.0
  • vim customized automatic indentation four characters
    打开文件/etc/vim/vimrc最后加入,保存
    set tabstop=4
    set shiftwidth=4
  • Write a script, automatically add three users, and calculate the uid of these three users and

    #!/bin/bash
    id user1 &> /dev/null || useradd user1
    id user2 &> /dev/null || useradd user2
    id user3 &> /dev/null || useradd user3
    
    user1_id=$(id -u user1)
    user2_id=$(id -u user2)
    user3_id=$(id -u user3)
    
    id_num=$[$user1_id + $user2_id + $user3_id]
    
    echo "The id sum is $id_num"
  • find Usage:
    Usage:
    find [the OPTIONS] [find the opening path] [search criteria] [Processing operation]
    find the opening path: Specifies initial search target specific path; default is the current directory;
    search criteria: specified search criteria, may be according to file name, size, type, affiliation, permissions, and so the standard; the default is to find all the files in the specified path;
    actions: operations on files that meet the search criteria to make, for example, delete and other operations; default output to standard output;
    Search Terms:
    (1) Find the file name:

           -name  "pattern"
           -iname "pattern"
          支持glob风格的通配符;*, ?, [], [^]
           -regex pattern:基于正则表达式模式查找文件,匹配是整个路径,而非其名;

    (2) According to locate files affiliation:

          -user USERNAME:查找属主指定用户的所有文件;
          -group GRPNAME:查找属组指定组的所有文件;
          -uid UID:查找属主指定的UID的所有文件;
         -gid GID:查找属组指定的GID的所有文件;
          -nouser:查找没有属主的文件;
          -nogroup:查找没有属组的文件;

    (3) Find the type of file:

     -type TYPE:
        f: 普通文件
        d: 目录文件
        l:符号链接文件
        b:块设备 文件
        c:字符设备文件
        p:管道文件
        s:套接字文件

    (4) depending on the size of the lookup file:

    -size [+|-]#UNIT
        常用单位:k, M, G
         #UNIT:(#-1, #]
        -#UNIT:[0,#-1]
        +#UNIT:(#, oo)

    (5) According to the time stamp to find:

    以“天”为单位:
        -atime  [+|-]#
            #:[#, #-1)
            -#:(#, 0]
            +#:(oo, #-1]
        -mtime
        -ctime
    
    以“分钟”为单位:
        -amin
        -mmin
        -cmin

    (6) under the authority find:

      -perm  [/|-]mode
          mode:精确权限匹配;
          /mode:任何一类用户(u,g,o)的权限中的任何一位(r,w,x)符合条件即满足;
              9位权限之间存在“或”关系;
          -mode:每一类用户(u,g,o)的权限中的每一位(r,w,x)同时符合条件即满足;
            9位权限之间存在“与”关系;

    Actions:

        -print:输出至标准输出;默认的动作;
        -ls:类似于对查找到的文件执行“ls -l”命令,输出文件的详细信息;
        -delete:删除查找到的文件;
        -fls /PATH/TO/SOMEFILE:把查找到的所有文件的长格式信息保存至指定文件中;
        -ok COMMAND {} \;   :对查找到的每个文件执行由COMMAND表示的命令;每次操作都由用户进行确认;
       -exec COMMAND {} \;  :对查找到的每个文件执行由COMMAND表示的命令;
    注意:find传递查找到的文件路径至后面的命令时,是先查找出所有符合条件的文件路径,并一次性传递给后面的命令;
    但是有些命令不能接受过长的参数,此时命令执行会失败;另一种方式可规避此问题:
        find | xargs COMMAND
  • Examples of common usage of the Find:
    (1) Find the / var directory subordinate to the main root, and belong to all files or directories group of mail;
    [root@localhost /]# find /var -user root -a -group mail  -ls
    5002    0 drwxrwxr-x   2 root     mail          224 Aug 13 14:13 /var/spool/mail
    3360  152 -rw-------   1 root     mail       154132 Jul 19 16:12 /var/spool/mail/root

    (2) Find / does not belong to any file or directory root, bin or under mysql usr directory;

    [root@localhost /]# find /usr -not -user  root -a -not -user bin -a -not -user mysql
    /usr/share/polkit-1/rules.d
    /usr/libexec/abrt-action-install-debuginfo-to-abrt-cache
    (3)查找当前系统上没有属或属组,且最近一周内曾被访问过的文件或目录;
    [root@localhost /]# find  /  \( -nouser -o -nogroup \)  -atime  -7  -ls
    find: ‘/proc/2710/task/2710/fd/6’: No such file or directory
    find: ‘/proc/2710/task/2710/fdinfo/6’: No such file or directory
    find: ‘/proc/2710/fd/6’: No such file or directory
    find: ‘/proc/2710/fdinfo/6’: No such file or directory

    (4) Find / etc directory greater than 1M and common files for all types of files;

    [root@localhost /]# find /etc/ -size +1M -type f  -exec ls -lh {} \;
      -r--r--r--. 1 root root 7.2M Jul  8 17:32 /etc/udev/hwdb.bin
      -rw-r--r--. 1 root root 3.6M Aug  6  2017 /etc/selinux/targeted/active/policy.kern
      -rw-r--r--. 1 root root 1.4M Aug  6  2017 /etc/selinux/targeted/contexts/files/file_contexts.bin
      -rw-r--r--. 1 root root 3.6M Aug  6  2017 /etc/selinux/targeted/policy/policy.30
      -rw-r--r--. 1 root root 1.4M Aug  7  2017 /etc/brltty/zh-tw.ctb

    (5) to find files in the / etc directory of all users do not have write access;
    [root@localhost /]# find /etc -not -perm /222 -type f -ls
    (6) Find / etc directory has at least file a class of user does not have permission to perform;
    [root@localhost /]# find /etc/ -not -perm -111 -type f -ls
    (7) Find /etc/init.d/ directory, all users have execute permissions, and other users have write access to all files;
    [root@localhost /]# find /etc -perm -113 -type f -ls

Guess you like

Origin blog.51cto.com/14418331/2432468