第三周

第三周

1、定义一个对所有用户都生效的命令别名,例如:lftps=‘lftp 172.168.0.1/pub’

编辑/etc/bashrc文件,在末行添加alias lftps=‘lftp 172.16.0.1/pub’

alias lftps='lftp 172.16.0.1/pub'

2、显示/etc/passwd文件中不以/bin/bash结尾的行

~]# grep -v "/bin/bash$" /etc/passwd
~]# egrep -v "/bin/bash$" /etc/passwd
~]# cat /etc/passwd | grep -v "/bin/bash$"
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
libstoragemgmt:x:998:997:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
chrony:x:997:995::/var/lib/chrony:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
slackware:x:1002:1002::/home/slackware:/bin/tcsh
tom:x:5001:1006::/tmp/tom:/bin/zsh
user5:x:5007:5007::/home/user5:/bin/chroot

3、找出/etc/passwd文件中,包含二位数字或者三位数的行。

~]# grep  "\<[0-9]\{2,3\}\>"  /etc/passwd
~]# grep  "\<[[:digit:]]\{2,3\}\>"  /etc/passwd
~]# egrep  "\<[0-9]{2,3}\>"  /etc/passwd
~]# egrep "\<[[: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
libstoragemgmt:x:998:997:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
chrony:x:997:995::/var/lib/chrony:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin

4、显示/proc/meminfo文件中以大写或小写S开头的行;用三种方式实现。

~]# grep -i ^s /proc/meminfo
~]# grep ^[sS] /proc/meminfo
~]# egrep "^(s|S)" /proc/meminfo
~]# egrep "^s|^S" /proc/meminfo
SwapCached:            0 kB
SwapTotal:       2097148 kB
SwapFree:        2097148 kB
Shmem:              9780 kB
Slab:              62060 kB
SReclaimable:      22888 kB
SUnreclaim:        39172 kB

5、使用echo输出一个绝对路径,使用egrep取出路径名,类似执行dirname /etc/passwd的结果。

~]# echo /var/log/message | egrep -o "^/.*(/[[:alnum:]])" | egrep -o "^/.*/" | egrep -o  "^.*"
/var/log/

6、找出ifconfig中的ip地址。要求结果只显示IP地址。

~]# ifconfig | grep -oE "([0-9]{1,3}\.){3}[0-9]{1,3}"
192.168.88.102
255.255.255.0
192.168.88.255
127.0.0.1
255.0.0.0

7、vim定制自动缩进四个字符。

编辑/etc/vimrc文件,添加set tabstop=4,保存退出

set nocompatible    " Use Vim defaults (much better!)
set bs=indent,eol,start     " allow backspacing over everything in insert mode
"set ai         " always set autoindenting on
"set backup     " keep a backup file
set viminfo='20,\"50    " read/write a .viminfo file, don't store more
            " than 50 lines of registers
set history=50      " keep 50 lines of command line history
set ruler       " show the cursor position all the time
set tabstop=4	//新增参数

8、编写脚本,实现自动添加三个用户,并计算这三个用户的uid之和。

#!/bin/bash
read -p "Enter three username:" UserName1 UserName2 UserName3

id $UserName1 &> /dev/null || useradd $UserName1
id $UserName2 &> /dev/null || useradd $UserName2
id $UserName3 &> /dev/null || useradd $UserName3

Uid1=$(id $UserName1 | cut -b 5-8)
Uid2=$(id $UserName2 | cut -b 5-8)
Uid3=$(id $UserName3 | cut -b 5-8)

UidSum=$(($Uid1 + $Uid2 + $Uid3))

echo "The sum of newuser's uid is:$UidSum"

9、find用法以及常用用法的实例延时。

实时查找工具,通过遍历指定起始路径下文件系统层级结构完成文件查找;

工作特性:

  • 查找速度略慢;
  • 精确查找;
  • 实时查找;

用法:

  • find [OPTIONS] [查找起始路径] [查找条件] [处理动作]
  • 查找起始路径:指定具体搜索目标起始路径;默认为当前目录;
  • 查找条件:指定的查找标准,可以根据文件名、大小、类型、从属关系、权限等等标准进行;默认为找出指定路径下的所有文件;
  • 处理动作:对符合查找条件的文件做出的操作,例如删除等操作;默认为输出至标准输出;

查找条件:
根据文件名查找:
-name “pattern”
-iname “pattern”
支持glob风格的通配符;
*, ?, [], [^]

-regex pattern:基于正则表达式模式查找文件,匹配是整个路径,而非其名;

例:查找/etc/目录下以gr开头的文件

~]# find /etc/ -name "gr*"
/etc/grub.d
/etc/default/grub
/etc/prelink.conf.d/grub2.conf
/etc/group
/etc/group-
/etc/sysconfig/grub
/etc/groff
/etc/security/group.conf
/etc/iproute2/group
/etc/grub2.cfg

~]# find /etc/ -iname "gr*"
/etc/grub.d
/etc/default/grub
/etc/prelink.conf.d/grub2.conf
/etc/group
/etc/group-
/etc/sysconfig/grub
/etc/GREP_COLORS
/etc/groff
/etc/security/group.conf
/etc/iproute2/group
/etc/grub2.cfg

根据文件从属关系查找:
-user USERNAME:查找属主指定用户的所有文件;
-group GRPNAME:查找属组指定组的所有文件;

-uid UID:查找属主指定的UID的所有文件;
-gid GID:查找属组指定的GID的所有文件;

-nouser:查找没有属主的文件;
-nogroup:查找没有属组的文件;

~]# ll /tmp
-rw-rw-r--. 1 user1  user1       0 Feb 19 05:13 1.txt
-rw-rw-r--. 1 user1  user1       0 Feb 19 05:13 2.txt
-rw-rw-r--. 1 user1  user1       0 Feb 19 05:13 3.txt
-rw-rw-r--. 1 centos centos      0 Feb 19 05:10 a.txt
-rw-rw-r--. 1 centos centos      0 Feb 19 05:10 b.txt
-rw-rw-r--. 1 centos centos      0 Feb 19 05:10 c.txt
-rw-rw-r--. 1   5012   5013      0 Feb 19 05:14 x.txt
-rw-rw-r--. 1   5012   5013      0 Feb 19 05:14 y.txt
-rw-rw-r--. 1   5012   5013      0 Feb 19 05:14 z.txt

例1:查找/tmp/目录下属主为centos的文件

~]# find /tmp/ -user centos
/tmp/a.txt
/tmp/b.txt
/tmp/c.txt

例2:查找/tmp/目录下uid为centos的文件

~]# id user1
uid=5011(user1) gid=5012(user1) groups=5012(user1)
~]# find /tmp/ -uid 5011
/tmp/1.txt
/tmp/2.txt
/tmp/3.txt

例3:查找/tmp/目录下没有属主的文件

~]# find /tmp/ -nouser
/tmp/x.txt
/tmp/y.txt
/tmp/z.txt

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

例:查找/dev/目录下的块设备文件

~]# find /dev/ -type b
/dev/dm-1
/dev/dm-0
/dev/sda2
/dev/sda1
/dev/sdb
/dev/sda
/dev/sr0

组合测试:
与:-a, 默认组合逻辑;
或:-o
非:-not, !

根据文件的大小查找:
-size [+|-]#UNIT
常用单位:k, M, G

#UNIT:(#-1, #]
-#UNIT:[0,#-1]
+#UNIT:(#, oo)

例:分别查找/tmp/目录下大小等于、小于、大于18K的文件

~]# find /tmp/ -size 18k
/tmp/a.txt
~]# find /tmp/ -size -18k
/tmp/b.txt
/tmp/1.txt
/tmp/2.txt
/tmp/3.txt
/tmp/x
/tmp/y
/tmp/z
~]# find /tmp/ -size +18k
/tmp/c.txt

根据时间戳查找:
以“天”为单位:
-atime [+|-]#
#:[#, #-1)
-#:(#, 0]
+#:(oo, #-1]
-mtime
-ctime

例1:查找/tmp/目录下8天前被访问过的文件

~]# find /tmp/ -atime 8
/tmp/2.txt

例2:查找/tmp/目录下超过8天未被访问过的文件

~]# find /tmp/ -atime +8
/tmp/1.txt

例3:查找/tmp/目录下8天内被访问过的文件

~]# find /tmp/ -atime -8
/tmp/a.txt
/tmp/b.txt
/tmp/c.txt
/tmp/3.txt
/tmp/x
/tmp/y
/tmp/z			

以“分钟”为单位:
-amin
-mmin
-cmin

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

~]# ll
total 76
-rwxrw-r--. 1 user1  user1      0 Feb 10 00:00 1.txt
-rw-rwxr--. 1 user1  user1      0 Feb 11 00:00 2.txt
-rw-rw-r-x. 1 user1  user1      0 Feb 19 05:13 3.txt
-rw-rw-r--. 1 centos centos 18281 Feb 19 05:49 a.txt
-rw-rw-r--. 1 centos centos 17227 Feb 19 05:49 b.txt
-rw-rw-r--. 1 centos centos 34202 Feb 19 05:52 c.txt
-rw-rw-r--. 1   5012   5013     0 Feb 19 05:14 x
-rw-rw-r--. 1   5012   5013     0 Feb 19 05:14 y
-rw-rw-r--. 1   5012   5013     0 Feb 19 05:14 z

例1:精确查找/tmp/目录下权限为764的文件

~]# find /tmp/ -perm 764
/tmp/1.txt

例2:查找/tmp/目录下有执行权限的文件

~]# find /tmp/ -perm /111
/tmp/1.txt
/tmp/2.txt
/tmp/3.txt

例3:查找/tmp/目录下属主&属组为读写权限、其他人有读和执行权限的文件

~]# find /tmp/ -perm -665
/tmp/3.txt

处理动作:
-print:输出至标准输出;默认的动作;
-ls:类似于对查找到的文件执行“ls -l”命令,输出文件的详细信息;
-delete:删除查找到的文件;
-fls /PATH/TO/SOMEFILE:把查找到的所有文件的长格式信息保存至指定文件中;
-ok COMMAND {} ; :对查找到的每个文件执行由COMMAND表示的命令;每次操作都由用户进行确认;
-exec COMMAND {} ; :对查找到的每个文件执行由COMMAND表示的命令;

例1:查找/tmp/目录下属主为centos的用户,并通过ls方式展示

~]# find /tmp/ -user centos -ls
16781097   20 -rw-rw-r--   1 centos   centos      18281 Feb 19 05:49 /tmp/a.txt
16781741   20 -rw-rw-r--   1 centos   centos      17227 Feb 19 05:49 /tmp/b.txt
16781753   36 -rw-rw-r--   1 centos   centos      34202 Feb 19 05:52 /tmp/c.txt

例2:查找/tmp/目录下权限为664的文件,并对其增加属主可执行权限

~]# find /tmp/ -perm 664 -ok chmod u+x {} \;
< chmod ... /tmp/a.txt > ? y
< chmod ... /tmp/b.txt > ? y
< chmod ... /tmp/c.txt > ? y
< chmod ... /tmp/x > ? y
< chmod ... /tmp/y > ? y
< chmod ... /tmp/z > ? y
//需要用户对每次操作进行确认
~]# find /tmp/ -perm 664 -exec chmod u+x {} \;
//无需用户对操作进行确认
~]# ll /tmp/
total 76
-rwxrw-r--. 1 user1  user1      0 Feb 10 00:00 1.txt
-rw-rwxr--. 1 user1  user1      0 Feb 11 00:00 2.txt
-rw-rw-r-x. 1 user1  user1      0 Feb 19 05:13 3.txt
-rwxrw-r--. 1 centos centos 18281 Feb 19 05:49 a.txt
-rwxrw-r--. 1 centos centos 17227 Feb 19 05:49 b.txt
-rwxrw-r--. 1 centos centos 34202 Feb 19 05:52 c.txt
-rwxrw-r--. 1   5012   5013     0 Feb 19 05:14 x
-rwxrw-r--. 1   5012   5013     0 Feb 19 05:14 y
-rwxrw-r--. 1   5012   5013     0 Feb 19 05:14 z
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值