正则表达式及vim、find等使用方法

1. 定义一个对所有用户都生效的命令别名,例如:lftps=lftp 172.168.0.1/pub
  • 配置用户登陆的配置文件总体上分为两类

    • profile类:交互式登陆,用于定义环境变量或运行命令和脚本;
      • 交互式登陆的所有用户生效配置文件:
        • /etc/profile
        • /etc/profile.d/*.sh
      • 交互式登陆只对个人用户生效的配置文件:
        • ~/bash_profile
    • bashrc类:非交互式登陆,用于定义本地变量或定义别名;
      • 非交互式登陆对全部用户生效的配置文件:
        • /etc/bashrc
      • 非交互式登陆对全部用户生效的配置文件:
        • ~/bash_rc
  • 题中要定义所有用户都生效的命令别名,所以在/etc/bashrc配置文件中追加别名即可:
[root@magedu ~]# echo "alias ssh133='ssh 172.16.200.133'" >> /etc/bashrc
[root@magedu ~]# tail -3 /etc/bashrc
alias lftps='lftp 172.16.0.1/pub'

alias ssh133='ssh 172.16.200.133'

验证:

[root@magedu ~]# bash
您好,请你每天加油学习,欢迎入坑
[root@magedu ~]# ssh133
The authenticity of host '172.16.200.133 (172.16.200.133)' can't be established.
ECDSA key fingerprint is 
Please type 'yes' or 'no': yes
Warning: Permanently added '172.16.200.133' (ECDSA) to the list of known hosts.
root@172.16.200.133's password: 
Last login: Mon Apr  1 14:13:12 2019 from 172.16.0.249
[root@databackup ~]# ifconfig
enp5s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.200.133  netmask 255.255.255.0  broadcast 172.16.200.255
        inet6 fe80::1a31:bfff:fed0:2846  prefixlen 64  scopeid 0x20<link>
        ether 18:31:bf:d0:28:46  txqueuelen 1000  (Ethernet)
        RX packets 316011550246  bytes 473307054516798 (430.4 TiB)
        RX errors 2  dropped 2047465  overruns 0  frame 1
        TX packets 34293001702  bytes 2399297987812 (2.1 TiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device memory 0xfa100000-fa17ffff  
2. 显示/etc/passwd文件中不以/bin/bash结尾的行。
[root@magedu ~]# grep -v "/bin/bash$" /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sssd:x:998:996:User for sssd:/:/sbin/nologin
libstoragemgmt:x:997:994:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
colord:x:996:993:User for colord:/var/lib/colord:/sbin/nologin
gluster:x:995:992:GlusterFS daemons:/var/run/gluster:/sbin/nologin
saslauth:x:994:76:Saslauthd user:/run/saslauthd:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
setroubleshoot:x:993:990::/var/lib/setroubleshoot:/sbin/nologin
rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
chrony:x:992:987::/var/lib/chrony:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
unbound:x:991:986:Unbound DNS resolver:/etc/unbound:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
geoclue:x:990:984:User for geoclue:/var/lib/geoclue:/sbin/nologin
radvd:x:75:75:radvd user:/:/sbin/nologin
qemu:x:107:107:qemu user:/:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
gnome-initial-setup:x:989:983::/run/gnome-initial-setup/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
slackware:x:1001:1001::/home/slackware:/bin/tcsh
fedora:x:4002:4002:Fedora Core:/home/fedora:/bin/tcsh
tom:x:5001:5004::/tmp/tom:/bin/zsh
nologin:x:5005:5008::/home/nologin:/sbin/nologin
3. 找出/etc/passwd文件中,包含二位数字或者三位数字的行。
[root@magedu ~]# grep "\<[[:digit:]]\{2,3\}\>" /etc/passwd
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sssd:x:998:996:User for sssd:/:/sbin/nologin
libstoragemgmt:x:997:994:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
colord:x:996:993:User for colord:/var/lib/colord:/sbin/nologin
gluster:x:995:992:GlusterFS daemons:/var/run/gluster:/sbin/nologin
saslauth:x:994:76:Saslauthd user:/run/saslauthd:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
setroubleshoot:x:993:990::/var/lib/setroubleshoot:/sbin/nologin
rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
chrony:x:992:987::/var/lib/chrony:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
unbound:x:991:986:Unbound DNS resolver:/etc/unbound:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
geoclue:x:990:984:User for geoclue:/var/lib/geoclue:/sbin/nologin
radvd:x:75:75:radvd user:/:/sbin/nologin
qemu:x:107:107:qemu user:/:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
gnome-initial-setup:x:989:983::/run/gnome-initial-setup/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
suse:x:988:982::/home/suse:/bin/bash
4. 显示/proc/meminfo文件中以大写或小写s开头的行;用三种方式实现。

1、

[root@magedu ~]# grep -i "^s" /proc/meminfo
SwapCached:         3728 kB
SwapTotal:      10485756 kB
SwapFree:       10429208 kB
Shmem:             93652 kB
Slab:             128220 kB
SReclaimable:      82892 kB
SUnreclaim:        45328 kB

2、

[root@magedu ~]# grep -E "^(s|S)" /proc/meminfo
SwapCached:         3728 kB
SwapTotal:      10485756 kB
SwapFree:       10429208 kB
Shmem:             93652 kB
Slab:             128220 kB
SReclaimable:      82892 kB
SUnreclaim:        45328 kB

3、

[root@magedu ~]# egrep ^[s,S] /proc/meminfo 
SwapCached:         3728 kB
SwapTotal:      10485756 kB
SwapFree:       10429208 kB
Shmem:             93652 kB
Slab:             128220 kB
SReclaimable:      82892 kB
SUnreclaim:        45328 kB
5. 使用echo输出一个绝对路径,使用egrep取出路径名,类似执行dirname /etc/passwd的结果。
[root@magedu ~]# echo "/etc/passwd" | grep -o "/.*/"
/etc/
6. 找出ifconfig中的ip地址;要求结果只显示ip地址。
[root@magedu ~]# ifconfig | grep -oE "\<(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]))\>"
172.16.200.253
255.255.255.0
172.16.200.255
127.0.0.1
255.0.0.0
192.168.122.1
255.255.255.0
192.168.122.255
[root@magedu ~]# ifconfig | grep -oE "(\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>\.){3}\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>"
172.16.200.253
255.255.255.0
172.16.200.255
127.0.0.1
255.0.0.0
192.168.122.1
255.255.255.0
192.168.122.255
7. vim定制自动缩进四个字符。

编辑/etc/vimrc配置文件。加入参数:
set tabstop=4
即可;

[root@magedu ~]# vim /etc/vimrc 
 " 表示按一个tab之后,显示出的相当于几个空格,默认的是8个;
 70 set tabstop=4
 71 
 72 " 表示在编辑模式的时候按退格键的时候退回缩进的长度;
 73 set softtabstop=4
 74 
 75 " 表示每一级缩进的长度,一般设置成跟softtabstop一样;
 76 set shiftwidth=4
8. 编写脚本,实现自动添加三个用户,并计算这三个用户的uid之和。
#!/bin/bash
  2 
  3 #定义用户变量;
  4 user1=caiwu
  5 user2=xinxi
  6 user3=sheji
  7 
  8 #判断用户是否已创建,如果没有,则创建,
  9 id `echo $user1` > /dev/null || useradd `echo $user1`
 10 id `echo $user2` > /dev/null || useradd `echo $user2`
 11 id `echo $user3` > /dev/null || useradd `echo $user3`
 12 
 13 #提取三个用户的UID并赋值与变量
 14 
 15 user1_UID=`grep $user1 /etc/passwd | cut -d: -f3`
 16 
 17 user2_UID=`grep  $user2 /etc/passwd | cut -d: -f3`
 18 
 19 user3_UID=`grep  $user3 /etc/passwd | cut -d: -f3`
 20 
 21 echo "用户1的UID是 $user1_UID "
 22 echo "用户2的UID是 $user2_UID "
 23 echo "用户3的UID是 $user3_UID "
 24 
 25 #三个用户UID求和
 26 
 27 usersum=$[$user1_UID + $user2_UID + $user3_UID]
 28 
 29 #输出值
 30 
 31 echo "三个用户UID之和是 $usersum 。"
 32 

验证结果:

[root@magedu scripts]# bash uidsum.sh 
用户1的UID是 5014 
用户2的UID是 5015 
用户3的UID是 5016 
三个用户UID之和是 15045 。
9. find用法及常用用法的实例演示。

find 命令 :
NAME

  • find - search for files in a directory hierarchy(在层级目录中搜索文件)

SYNOPSIS

  • find [OPTIONS] [查找起始路径] [查找条件] [处理动作]

  • [查找起始路径]:指定具体搜索路径,不指定情况下为当前路径;
  • [查找条件]--分两种:表达式、测试
  • [测试]:
    根据文件名查找:
  • -name pattern:指定查找名,[PATTERN]指的是通配符,不是正则表达式;
    例如:查找/etc中文件名含有passwd的文件:

    [root@magedu ~]# find /etc -name "*passwd*"
    /etc/passwd
    /etc/security/opasswd
    /etc/pam.d/passwd
    /etc/passwd-
  • -iname pattern:指定的名字不区分大小写

例如:查找/tmp/的passwd字符的文件不区分大小写

[root@magedu tmp]# find /tmp -iname "passwd"
/tmp/passwd
/tmp/mytest1/pam.d/passwd
/tmp/mytest1/passwd
/tmp/PASSwd
/tmp/Passwd
/tmp/PassWD
/tmp/PASSWD
根据文件从属关系查找:
  • -user USERNAME:根据文件属主查找;
  • 例如:查找/home目录下查找属主是lijianzhi的文件;

    [root@magedu tmp]# find /home -user "lijianzhi"
    /home/lijianzhi
    /home/lijianzhi/.mozilla
    /home/lijianzhi/.mozilla/extensions
    /home/lijianzhi/.mozilla/plugins
    /home/lijianzhi/.bash_logout
    /home/lijianzhi/.bash_profile
    /home/lijianzhi/.bashrc
    /home/lijianzhi/.cache
    /home/lijianzhi/.cache/gdm
    /home/lijianzhi/.cache/gdm/session.log
  • -group GRPNAME:根据属组找查文件
  • 例如:查找/tmp目录下属组是tom的文件
  • [root@magedu tmp]# find . -group tom

    ./tom
    ./tom/.mozilla
    ./tom/.mozilla/extensions
    ./tom/.mozilla/plugins
    ./tom/.bash_logout
    ./tom/.bash_profile
    ./tom/.bashrc
  • -uid UID :根据用户的UID查找文件;
  • 例如查找/etc/目录下uid是0的文件;
  • [root@magedu ~]# find /etc/ -uid 0 -ls

  • -gid GID:根据用户的GID查找;
  • 例如查找/etc/目录中属组 是1000的文件:
  • [root@magedu ~]# find /etc -gid 1000 -ls

  • -nouid:找出系统中没有属主的文件;
    [root@magedu ~]# find / -nouser -ls
  • -nogroup:查出系统中没有属组的文件:
    [root@magedu ~]# find / -nogroup -ls
    根据文件类型查找:

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

例:

[root@magedu ~]# find /etc/ -type f -ls
[root@magedu ~]# find /etc/ -type d -ls
[root@magedu ~]# find /dev -type b -ls
[root@magedu ~]# find /dev -type c -ls
[root@magedu ~]# find /etc -type l -ls
[root@magedu ~]# find /var -type p -ls
   112    0 prw-------   1 root     root            0 Apr  3 15:58 /var/lib/nfs/rpc_pipefs/gssd/clntXX/gssd
[root@magedu ~]# find /var -type s -ls
根据文件大小查找:
  • 格试:-size [+|-] #Unit
  • Unit常用的有:K,M,G
    * 范围的三种表达方式:
  • #Unit:表示的范围是(#-1,#)例如:查找/etc目录下的5K的文件,命令如下,而查找的范围是4K-5K之间的数;
 [root@magedu ~]# find /etc/ -size 5k  -ls
135123672    8 -rw-r--r--   1 root     root         4963 Apr 27  2018 /etc/libreport/events.d/abrt_event.conf
135123741    8 -rw-r--r--   1 root     root         4815 Apr 27  2018 /etc/libreport/events.d/ccpp_event.conf
779982    8 -rw-------   1 root     root         4586 Apr 27  2018 /etc/libreport/cert-api.access.redhat.com.pem
707874    8 -rwxr-xr-x   1 root     root         4334 Jan  3  2018 /etc/rc.d/init.d/netconsole
67326628    8 -rw-r--r--   1 root     root         4849 Apr 11  2018 /etc/idmapd.conf
...
  • +#Unit:代表的范围是#~∞这个范围;例如要找系统里大于200M的文件,命令如下:
[root@magedu ~]# find / -size 10M -ls
203179248 9872 -rw-r--r--   1 root     root     10107428 Nov 12 22:49 /var/cache/yum/x86_64/7/base/packages/webkitgtk4-plugin-process-gtk2-2.20.5-1.el7.x86_64.rpm
202479205 9524 -rw-r--r--   1 root     root      9749231 Mar 15  2018 /usr/lib64/firefox/omni.ja
  • -#Unit:代表的范围是从0到#-1的范围,例如要找/etc目录下小于5的文件,命令如下,要找的范围是0-4K的文件:

    [root@magedu ~]# find /etc/ -size -5k -ls
    根据时间戳查找:
  • 分两种:以天为单位,或者以分钟为单位;
    以天为单位又分三种时间:访问时间、修改时间、改变时间:和以大小单位一样,分三个范围:
    -atime--(访问时间):
  • #:[#,#-1)代表的范围是#天前的24小时之内,例如要找3天以前当天访问过的文件:

    [root@magedu ~]# find /  -atime 3 -ls
  • -#:(#,0]代表的范围是#天前到现在的时间范围;例如:找出前3天所有访问过的文件:

    [root@magedu ~]# find /  -atime -3 -ls
  • +#:(无穷大,#-1]代表的范围是#-1天以前的,例如。找出系统中3天以前访问过的文件;
[root@magedu ~]# find /  -atime +3 -ls

-mtime(修改时间)和访问时间一样,只是选项不一样;
-ctime(改为时间)和访问时间一样,只是选项不一样;
以“分钟”为单位:
和以“天”为单位一样:
-amin:访问时间;
-mmin:修改时间;
-cmin:改变时间;

组合测试查找:
  • -a:’与‘运算的过程,同时都满足,才符保条件;例如查找/etc/目录下属主是root的普通文件:

    [root@magedu ~]# find /etc -user root -a -type f -ls
    67108930    4 -rw-r--r--   1 root     root          465 Oct 16 10:22 /etc/fstab
    67108931    0 -rw-------   1 root     root            0 Oct 16 10:22 /etc/crypttab
    67976179    4 -rw-r--r--   1 root     root           68 Apr  3 15:58 /etc/resolv.conf
    67719504    4 -rw-r--r--   1 root     root         1160 Feb 24  2017 /etc/fonts/conf.d/25-no-bitmap-fedora.conf
    67719517    4 -rw-r--r--   1 root     root          978 Aug 31  2013 /etc/fonts/conf.d/README
    930378    8 -rw-r--r--   1 root     root         5582 Aug  2  2017 /etc/fonts/fonts.conf
    201797909   12 -rwxr-xr-x   1 root     root         8702 Oct 21  2017 /etc/grub.d/00_header
    201797910    4 -rwxr-xr-x   1 root     root          232 Oct 21  2017 /etc/grub.d/01_users
    201797911   12 -rwxr-xr-x   1 root     root        10781 Oct 21  2017 /etc/grub.d/10_linux
    201797912   12 -rwxr-xr-x   1 root     root        10275 Oct 21  2017 /etc/grub.d/20_linux_xen
    201797913    4 -rwxr-xr-x   1 root     root         2559 Oct 21  2017 /etc/grub.d/20_ppc_terminfo
    201797914   12 -rwxr-xr-x   1 root     root        11169 Oct 21  2017 /etc/grub.d/30_os-prober
    201797915    4 -rwxr-xr-x   1 root     root          214 Oct 21  2017 /etc/grub.d/40_custom
    201797916    4 -rwxr-xr-x   1 root     root          216 Oct 21  2017 /etc/grub.d/41_custom
    ...
  • -o:或运算,只符合一个条件,就可以匹配;例如,查找/tmp目录下三天以前修改的目录文件:

    [root@magedu ~]# find /tmp -mtime 3 -o -type d -ls
    67108936    4 drwxrwxrwt  37 root     root         4096 Apr  7 03:25 /tmp
    621611    0 drwxrwxrwt   2 root     root            6 Oct 16 10:26 /tmp/.font-unix
    134940554    0 drwxrwxrwt   2 root     root           16 Apr  3 15:58 /tmp/.X11-unix
    201644770    0 drwxrwxrwt   2 root     root            6 Oct 16 10:26 /tmp/.Test-unix
    621614    0 drwxrwxrwt   2 root     root           18 Apr  3 15:59 /tmp/.ICE-unix
    67449719    0 drwxrwxrwt   2 root     root            6 Oct 16 10:26 /tmp/.XIM-unix
    67975346    0 drwx------   2 root     root            6 Mar 22 17:22 /tmp/test.WBnpqFy
    67975347    0 drwx------   2 root     root            6 Mar 22 17:32 /tmp/test.GTmUtS4
    34882    0 drwxr-xr-x   2 root     root            6 Mar 26 19:15 /tmp/q_z
    34959    0 drwx------   3 tom      tom            22 Mar 26 21:38 /tmp/tom
    ...

    -not:非运算,相当天条件拟定后取反,例如,找出/etc目录下属主不是root的文件;

    [root@magedu ~]# find /etc -not -user root -ls
    201681376    0 drwx--x--x   3 sssd     sssd           20 Oct 16 10:27 /etc/sssd
    707606    0 drwx--x--x   2 sssd     sssd            6 Apr 13  2018 /etc/sssd/conf.d
    134940568    0 drwx------   2 polkitd  root           65 Oct 16 10:26 /etc/polkit-1/rules.d
    67597073    8 -rw-------   1 tss      tss          7046 Aug  4  2017 /etc/tcsd.conf
    find命令的处理动作:

    -print:输出至标准输出,默认动作,不需要指定;例如:找出/usr目录大于30M的文件,find /usr -size +30M -print 和 find /usr -size +30M的执行结果是一样的:

    [root@magedu ~]# find /usr -size +30M -print
    /usr/lib/locale/locale-archive
    /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.171-2.6.13.2.el7.x86_64/jre/lib/rt.jar
    /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.161-2.b14.el7.x86_64/jre/lib/rt.jar
    /usr/lib64/libLLVM-5.0-rhel.so
    /usr/lib64/libwebkit2gtk-4.0.so.37.19.9
    /usr/lib64/firefox/libxul.so
    /usr/lib64/libwebkitgtk-3.0.so.0.22.17
    /usr/libexec/webkit2gtk-4.0/WebKitPluginProcess2
    [root@magedu ~]# find /usr -size +30M 
    /usr/lib/locale/locale-archive
    /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.171-2.6.13.2.el7.x86_64/jre/lib/rt.jar
    /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.161-2.b14.el7.x86_64/jre/lib/rt.jar
    /usr/lib64/libLLVM-5.0-rhel.so
    /usr/lib64/libwebkit2gtk-4.0.so.37.19.9
    /usr/lib64/firefox/libxul.so
    /usr/lib64/libwebkitgtk-3.0.so.0.22.17
    /usr/libexec/webkit2gtk-4.0/WebKitPluginProcess2
  • -ls:显示详细信息:

    [root@magedu ~]# find /usr -size +30M -ls
    134425284 103556 -rw-r--r--   1 root     root     106070960 Oct 16 10:25 /usr/lib/locale/locale-archive
    67737849 66888 -rw-r--r--   1 root     root     68490757 Apr 11  2018 /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.171-2.6.13.2.el7.x86_64/jre/lib/rt.jar
    135190478 71520 -rw-r--r--   1 root     root     73232502 Apr 11  2018 /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.161-2.b14.el7.x86_64/jre/lib/rt.jar
    191293 36392 -rwxr-xr-x   1 root     root     37261600 Apr 11  2018 /usr/lib64/libLLVM-5.0-rhel.so
    1289550 41552 -rwxr-xr-x   1 root     root     42546544 Apr 11  2018 /usr/lib64/libwebkit2gtk-4.0.so.37.19.9
    202479204 73584 -rwxr-xr-x   1 root     root     75349784 Mar 15  2018 /usr/lib64/firefox/libxul.so
    1421038 34592 -rwxr-xr-x   1 root     root     35421344 Aug  6  2017 /usr/lib64/libwebkitgtk-3.0.so.0.22.17
    1289548 35864 -rwxr-xr-x   1 root     root     36721392 Apr 11  2018 /usr/libexec/webkit2gtk-4.0/WebKitPluginProcess2
  • -delete:把找出来的文件删除掉,慎重操作:
  • 例如:找出/tmp目录下非root文件删掉:

    [root@magedu ~]# find /tmp -not -user root 
    /tmp/.ICE-unix/1574
    /tmp/tom
    /tmp/tom/.mozilla
    /tmp/tom/.mozilla/extensions
    /tmp/tom/.mozilla/plugins
    /tmp/袁腾飞
    /tmp/袁腾飞/.mozilla
    /tmp/袁腾飞/.mozilla/extensions
    /tmp/袁腾飞/.mozilla/plugins
    /tmp/袁腾飞/.bash_logout
    /tmp/袁腾飞/.bash_profile
    /tmp/袁腾飞/.bashrc
    /tmp/.esd-1002
    [root@magedu ~]# find /tmp -not -user root -delete
    [root@magedu ~]# find /tmp -not -user root 
    [root@magedu ~]# 
  • -fls /PATH/TO/SOMEFILE:把查找出来的文件的长格式信息保存到指定文件;

    [root@magedu ~]# find /tmp -user root -fls /tmp/find.test1
    [root@magedu ~]# cat /tmp/find.test1
    67108936    4 drwxrwxrwt  34 root     root         4096 Apr  7 22:09 /tmp
    621611    0 drwxrwxrwt   2 root     root            6 Oct 16 10:26 /tmp/.font-unix
    134940554    0 drwxrwxrwt   2 root     root           16 Apr  3 15:58 /tmp/.X11-unix
    135940342    0 srwxrwxrwx   1 root     root            0 Apr  3 15:58 /tmp/.X11-unix/X0
    201644770    0 drwxrwxrwt   2 root     root            6 Oct 16 10:26 /tmp/.Test-unix
    621614    0 drwxrwxrwt   2 root     root            6 Apr  7 22:06 /tmp/.ICE-unix
    67449719    0 drwxrwxrwt   2 root     root            6 Oct 16 10:26 /tmp/.XIM-unix
    67975346    0 drwx------   2 root     root            6 Mar 22 17:22 /tmp/test.WBnpqFy
    67975347    0 drwx------   2 root     root            6 Mar 22 17:32 /tmp/test.GTmUtS4
  • -ok COMMAND {} \;:对查找到的每个文件执行由COMMAND表示的命令;每次操作都有用户确认;

    [root@magedu ~]# find /tmp -name "find.test*" 
    /tmp/find.test1
    /tmp/find.test2
    /tmp/find.test3
    [root@magedu ~]# find /tmp -name "find.test*" -ok rm  {} \;
    < rm ... /tmp/find.test1 > ? y
    < rm ... /tmp/find.test2 > ? y
    < rm ... /tmp/find.test3 > ? y
    [root@magedu ~]# find /tmp -name "find.test*" 
  • -exec COMMAND {} \;:对查找到的每个文件执行由COMMAND表示的命令;每次操作不需要用户确认;
[root@magedu ~]# find /tmp -name "man" -exec rm {} \;
rm: cannot remove ‘/tmp/man’: Is a directory
[root@magedu ~]# find /tmp -name "man" 
[root@magedu ~]# 

转载于:https://blog.51cto.com/9229045/2375120

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值