1,显示当前时间,格式为:2019-06-18 10:20:30

答案:date “+%F,%T” 或者 date “+%F %H:%M:%S”

 

2,显示前天是星期几?

答案:date -d “-2 day” +%A

知识点:一 ,date +%s 是把当前时间转化为秒数         二, date -d @”1523604170″ 把秒数转化回来

 

3,今天18:30自动关机,并提示用户。

答案:hutdown -h 18:30 “dao dian guan ji,18:30” 如果想取消此操作输入: shutdown -c

 

4,在本机字符终端登录时,除显示原有信息外,在显示当前登录终端号,主机名和当前时间。

答案:vim /etc/profile.d/kaiji.sh 进去后输入:#********************************************************************
echo your hostname is hostname
who am i

 

5,显示/var目录下所有以l开头,以一个小写字母结尾,且中间出现至少一位数字的文件或目录。

答案:ls /var/l*[0-9]*[[:lower:]]

 

6,显示/etc目录下以任意一位数字开头,且以非数字结尾的文件或目录。

答案:ls /etc/[0-9]*[^0-9]

 

7,显示/etc/目录下以非字母开头,后面跟了一个字母及其他任意长度任意字符的文件或目录。

答案:ls /etc/[^[:alpha:]][a-zA-Z]*

 

8,显示/etc/目录下所有以rc开头,并后面是0-6之间的数字,其他为任意字符的文件或目录。

答案:ls /etc/rc[0-6]*

 

9,显示/etc目录下,所有以.d结尾的文件或目录。

答案:ls /etc/*.d

 

10,显示/etc目录下,所有.conf结尾,且以m,n,r,p开头的文件或目录。

答案:ls /etc/[m,n,r,p]*.conf

 

11,只显示/root下的隐藏文件和目录。 只显示/etc下的非隐藏目录

答案:ls -d /root/.* ls /etc/[^.]*/ -d

 

12,定义别名命令baketc,每天将/etc/目录下的所有文件,备份到/app独立的子目录下,并要求子目录格式为backupYYYY-mm-dd备份过程可见。

答案:alias baketc=”cp -av /etc /data/backupdate +%F”

 

13,创建/app/rootdir目录,并复制/root下所有文件到该目录内,要求保留原有权限。

答案:mkdir -p /app/rootdir cp -a /root /app/rootdir/

 

14,如何创建/testdir/dir1/x,/testdir/dir/y,/testdir/dir/x/a,/testdir/dir/x/b,/testdir/dir/y/a,/testdir/dir/y/b.

答案:mkdir -p /testdir/dir1/{x,y}/{a,b}

 

15,如何创建/testdir/dir2/x,/testdir/dir2/y,/testdir/dir2/x/a,/testdir/dir2/x/b.

答案:mkdir -p /testdir/dir2/{x/{a,b},y}

 

16,如何创建/testdir/dir3,/testdir/dir4,/testdir/dir5,/testdir/dir5/dir6,/testdir/dir5/dir7.

答案:mkdir -p /testdir/{dir3,dir4,dir5/{dir6,dir7}}

 

17,将/etc/issue文件中的内容转化为大写后保存至/tmp/issue.out文件中。

答案:cat /etc/issue | tr “[a-z]” “[A-Z]” > /tmp/issue.out

 

18,将当前系统登录用户的信息转换为大写后保存至/tmp/who.out文件中。

答案:who | tr “[a-z]” “[A-Z]” >/tmp/who.out

 

19,一个linux用户给root发邮件,要求邮件标题为” help”,邮件正文如下:Heello,i am 用户名,The system version is here ,please help me to check it,thanks! 操作系统版本信息

答案:mail -s “help” root <<123
>Hello,I am $USER
>The system version is here,please help me to check it,thanks!
>cat /etc/centos-release
>123

 

20,将/root/下文件列表,显示成一行,并文件名之间用空格隔开。

答案:ls /root | tr “\n” ” ”

 

21,计算1+2+3+..+99+100的总和。

答案:echo {1..100}|tr ” ” “+”|bc

 

22,删除Windows文本文件中的^M字符

答案:tr -d “\15” win.txt

 

23,处理字符串 “xt.,l 1 jr#!$mn 2 c*/fe 3 uz 4” ,只保留其中的数字和空格。

答案:echo “xt.,l 1 jr#hostnamemn 2 c*/fe 3 uz 4” |tr -dc “[:digit:][:space:]”

 

24,将PATH变量每个目录显示在独立的一行。

答案:echo $PATH |tr “:” “\n”

 

25,将指定文件中0-9分别代替成a-j .

答案:先创建文件touch f1 给f1 vim 输入0-9 cat f1 | tr “[0-9]” “[a-j]”

 

26,将文件/etc/centos-release中每个单词(由字母组成)显示在独立的一行,并无空行。

答案:cat /etc/centos-release |tr -c “[:alpha:]” ” ” |tr -s ” ” “\n”

 

27,创建用户gentoo,附加组为bin和root,默认shell为/bin/csh,注释信息为”Gentoo Distribution”.

答案:useradd -G bin,root -s /bin/csh -c “Gentoo Distribution” gentoo

 

28,创建下面的用户,组和组成员关系
名字为webs的组 用户nginx使用webs作为附加组 用户varnish,也使用webs作为附加组
用户mysql,不可交互登录系统,且不是webs的成员,nbinx,varnish,mysql密码都是magedu

答案: groupadd webs       useradd -G webs nginx        useradd -G webs varnish         useradd -s /sbin/nologin masql

echo magedu |passwd –stdin nginx;
echo magedu |passwd –stdin varnish;
echo magedu |passwd –stdin mysql;

 

29,当用户docker对/testdir 目录无执行权限时,意味着无法做哪些操作?

答案: 不能cd进去,不能查看文件详细属性,也不能去访问目录里的文件内容(即使有读权限)。

 

30,当用户mongodb对/testdir 目录无读权限时,意味着无法做哪些操作?

答案:不能对目录下的文件进行访问。

 

31, 当用户redis 对/testdir 目录无写权限时,该目录下的只读文件file1是否可修改和删除?

答案:不能,因为对目录没有权限,所以不能。文件能不能删,不由文件决定,而由目录决定。

 

32,当用户zabbix对/testdir 目录有写和执行权限时,该目录下的只读文件file1是否可修改和删除?

答案:可以修改和删除

 

33,复制/etc/fstab 文件到/var/tmp 下,设置文件所有者为tomcat 读写权限,所属组为apps组有读写权限,其他人无权限。

答案:(一)cp -a /etc/fstab /var/tmp (二) useradd tomcat (三) groupadd apps (四) chown tomcat /var/tmp (五) chgrp apps /var/tmp (六) chmod 660 /var/tmp

 

34,误删除了用户git的家目录,请重建并恢复该用户家目录及相应的权限属性。

答案:           rm -rf /home/git ;      mkdir /home/git;          cp -a /etc/skel/.[^.]* /home/git;                                                         chown -R git:git  /home/git;

35,在/testdir/dir 里创建的新文件自动属于webs组,组apps的成员如:tomcat能对这些新文件有读写权限,组dbs的成员如:mysql只能对新文件有读权限,其他用户(不属于webs,apps,dbs)不能访问文件夹。

答案:                     mkdir -p /testdir/dir                                                                                                                                                       chgrp webs /testdir/dir                                                                                                                                                  chmod g=s /testdir/dir
setfacl -m g:apps:rw /testdir/dir                                                                                                                                     setfacl -m g:dbs:r /testdir/dir

chmod o= /testdir/dir

 

36,备份/testdir/dir 里所有文件的ACL权限到/root/acl.txt中,清除/testdir/dir中所有ACL权限,最后还原ACL权限。

答案:                     getfacl -R /testdir/dir > /root/acl.txt                                                                                                                          setfacl -b /testdir/dir

setfacl -R –set-file=acl.txt /testdir/dir

 

37, 找出ifconfig “网卡名” 命令结果中本机的IPv4地址。

答案:(方法一)ifconfig ens33 | grep netmask | tr -s ” ” “:” |cut -d: -f3

(方法二)ifconfig ens33 |egrep -o \<“(([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])”\>

(方法三)ifconfig ens33 | sed -n “2p” | sed -r s’@(.*inet)(.*)( netmask.*)@\2@’

 

38,查出分区空间使用率的最大百分比值。

答案:(方法一)df | grep ^/dev | tr -s ” ” “:” | cut -d: -f5 |cut -d% -f1 | sort -nr | head -n1

(方法二) df | grep -o “[0-9]\{1,3\}%” |grep -o “[0-9]\+” |sort -nr |head -n1

 

39,查出用户UID最大值得用户名,UID及shell类型。

答案:cat  /etc/passwd |sort -nr -t: -k3 |head -n1 |cut -d: -f1,3,7

 

40,查出/tmp的权限,以数字方式显示

答案:stat  /tmp |head -n4|tail -n1|cut -d/ -f1|cut -d'(‘ -f2

 

41, 统计当前连接本机的每个远程主机IP的连接数,并从大到小排序。

答案: 先从桌面获取rz 获取文件,再进行处理。                                                                                                                  cat access_log |egrep -o “\<(([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])\>”|sort|uniq -dc|sort -nr

 

 

42,显示/proc/meminfo 文件中以大小s开头的行(要求:使用两种方法)

答案:(方法一)cat /proc/meminfo |egrep -oi ^s.*

(方法二)cat /proc/meminfo |egrep ^[Ss].*

 

43,显示/etc/passwd文件中不以/bin/bash结尾的行。

答案:cat  /etc/passwd |egrep -v /bin/bash$

 

44,显示用户rpc默认的shell程序。

答案:(方法一)cat  /etc/passwd |egrep rpc|cut -d: -f1,7

(方法二)cat  /etc/passwd |egrep rpc|sed -r ‘s/(.*:)([^:]+:?$)/\2/’

 

45,找出/etc/passwd 中的两位或三位数

答案:cat  /etc/passwd | egrep -o “[0-9]{2,3}”

 

46,显示Centos7的/etc/grub2.cfg文件中,至少以一个空白字符开头的且后面有非空白字符的行。

答案:cat  /etc/grub2.cfg |egrep ^[[:space:]][^[:space:]].*$

 

47,找出”netstat -tan” 命令结果中以LISTEN后跟任意多个空白字符结尾的行。

答案:netstat -tan |egrep .*LISTEN[[:space:]]+

 

48, 显示Centos7上所有系统用户的用户名和UID。

答案:cat  /etc/passwd |egrep .*/sbin/nologin$ |cut -d: -f1,3

 

49,添加用户bash,testbash,basher,sh,nologin(其shell为/sbin/nologin),找出/etc/passwd用户名和shell同名的行。

答案:cat  /etc/passwd | egrep “^(.*)(:.*)\1$”

 

50,利用df和grep,去出磁盘各分区利用率,并从大到小排序。

答案:df |grep ^/dev |tr -s ” ” “:”|cut -d: -f5 |cut -d% -f1 |sort -nr|head -n1

 

51,显示三个用户root,mage,wang的UID和默认shell.

答案:cat /etc/passwd |egrep ^”(root|mage|wang)” |cut -d: -f1,3,7

 

52,找出/etc/rc.d/init.d/functions文件中行首为某单词(包括下划线)后面跟一个小括号的行。

答案:cat /etc/rc.d/init.d/functions | egrep “^[a-zA-Z_]+

.*”

 

53,使用egrep取出/etc/rc.d/init.d/functions中其基名。

答案:echo /etc/rc.d/init.d/functions |egrep -o “[^/]*/?$”

 

54,使用egrep取出上面 路径的目录名。

答案:(方法一)echo /etc/rc.d/init.d/functions |egrep -o “/.*/”

(方法二)echo /etc/rc.d/init.d/functions |egrep -o “(/).*\1”

 

55,统计last命令中以root登录的每个主机IP地址登录次数。

答案:                  last |egrep root |egrep “\<(([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])\>”|tr -s ” ” “:”|sort -t: -k3|cut -d: -f3|uniq -dc

 

56,利用扩展正则表达式分别表示0-9,10-99,100-199,200-249,250-255.

答案:              [0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]

 

57,显示ifconfig命令结果中所有IPV4地址。

答案:               ifconfig | egrep -o “\<(([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])\>”

 

58,将此字符串:welcome to magedu linux 中的每个字符去重并排序,重复次数多的放在最前面。

答案:         echo “welcometomagedulinux” |grep -o “.”|sort|uniq -c|sort -nr

 

59,复制/etc/profile至/tmp/目录,用查找替换命令删除/tmp/profile文件中的行首的空白字符。

答案:                                 cp /etc/profile /tmp/                                                                                                                                                            vim /tmp/profile                                                                                                                                               命令模式下按“:”进入扩展模式输入 %s/^[[:space:]]*//g

 

60, 复制/etc/rc.d/init.d/functions文件至/tmp目录,用查找替换命令为/tmp/functions的每行开头为空白字符的行的行首添加一个#号。

答案:                                    cp /etc/rc.d/init.d/functions /tmp                                                                                                                                 vim /tmp/functions                                                                                                                                                命令模式下按“:”进入扩展模式输入         %s/^[[:space:]] */#&/                                                                                                                                            或者           %s/[[:space:]]\+.∗

/#\1/g

 

61, 在VIM中设置tab缩进为4个字符。

答案:                vim /etc/vimrc
在文件最后添加:
set ts=4
set expandtab
set autoindent
:wq

 

62,复制/etc/rc.d/init.d/functions文件至/tmp目录,替换/tmp/functions文件中的/etc/sysconfig/init为/var/log.

答案:                                            cp /etc/rc.d/init.d/functions /tmp                                                                                                                              vim /tmp/functions                                                                                                                      命令模式下按“:”进入扩展模式输入               %s@\/etc\/sysconfig\/init@\/var\/log@

 

63, 删除/tmp/functions文件中所有以#开头,且#后面至少有一个空白字符的行的行首的#号。

答案:                              vim /tmp/functions                                                                                                                                                      命令模式下按“:”进入扩展模式输入          %s@^#”“\+.∗

@\1@

 

64, 编写脚本/root/bin/systeminfo.sh,显示当前主机系统信息,包括主机名,IPV4,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小。

答案: vim /root/bin/systeminfo.sh
#********************************************************************
echo hostname
echo ifconfig ens33 | egrep -o "\<(([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])\>"|head -n1
echo cat /etc/centos-release
echo uname -r
echo lscpu |grep "^Model name.*" |cut -d: -f2|tr -s " "
echo cat /proc/meminfo |head -n1
echo lsblk |grep '^sda'|tr -s " " "%"|cut -d% -f4
:wq

 

65, 编写脚本/root/bin/backup.sh,可实现每日将/etc/目录备份到/root/etcYYY-mm-dd中。

vim /root/bin/backup.sh                     cp -a /etc /root/etcdate +%F          :wq

 

66,编写脚本/root/bin/disk.sh,显示当前硬盘分区中空间利用率最大的值。

答案:       e=df|egrep ^/dev |tr -s " " ":"|cut -d: -f5|cut -d% -f1|sort -nr|head -n1
echo $e                                                                                                                                                                             :wq

 

67, 编写脚本/root/bin/links.sh ,显示正连接本主机的每个远程主机的IPV4地址和连接数,并按连接数从大到小排序。

      答案:                  vim /root/bin/linsk.sh                                                                                                                                                     a=cat access_log |egrep -o "\<(([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])\>"|sort|uniq -c|sort -nr                                                                           echo $a

68, 编写脚本/root/bin/sumid.sh ,计算/etc/passwd 文件中的第10个用户和第20用户的ID之和。

答案:              vim /root/bin/sumid.sh                                                                                                                                                 a=cat /etc/passwd | head -n10 |tail -n1|cut -d: -f3
b=cat /etc/passwd | head -n20 |tail -n1|cut -d: -f3
let c=a+b 或 d=$[ a+b ]                                                                                                                                                  echo $d

 

69, 编写脚本/root/bin/sumspace.sh ,传递两个文件路径作为参数给脚本,计算这两个文件中所有空白行之和。

答案:                                 vim /root/bin/sumspace.sh                                                                                                                                         a=cat f1 |egrep ^[[:space:]]*$ |wc -l                                                                                                                         b=cat f2 |egrep ^[[:space:]]*$ |wc -l

let c=a+b

 

70, 编写脚本/root/bin/sumfile.sh ,统计/etc ,/var,/usr 目录中共有多少个一级子目录和文件。

答案:vim /root/bin/sumfile.sh                       a=ls /etc/ |wc -l                                                                                                                                                           b=ls /var/ |wc -l c=ls /usr/ |wc -l
let d=a+b+c

 

71, 编写脚本/root/bin/argsnum.sh ,接受一个文件路径作为参数;如果参数个数小于1,则提示用户 “至少应该给一个参数”,并立即退出;如果参数个数不少于1,则显示第一个参数所指向的文件中的空白行数。

答案:         vim /root/bin/argsnum.sh                                                                                                                                             [ $# -lt 1] &&  echo “At least one parameter should be given” && exit
[ $# -ge 1] &&  echo egrep "^[[:space:]]*$"  $1|wc -l

 

73, 编写脚本/root/bin/hostping.sh ,接受一个主机的IPV4地址做为参数,测试是否可连通。如果能ping通,则提示用户 “该IP地址可以访问” ;如果不可ping通,则提示用户 “该IP地址不可访问”。

答案:                vim /root/bin/hostping.sh                                                                                                                                         [[ $1 =~ “\<(([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])\>” ]] || echo { “IP error”;exit; }                                                                                    ping $1 && echo “This address can be accessed”|| echo “This address cannot be accessed”

74, 编写脚本/root/bin/checkdisk.sh , 检查磁盘分区空间和inode使用率,如果超过80%,就发广播警告空间将满。

答案:                    vim /root/bin/checkdisk.sh                                                                                                                                       a=df |egrep ^/dev |tr -s " " ":" |cut -d: -f5 |cut -d% -f1|sort -nr|head -n1
[[ $a -ge 80 ]] && echo “zhao huo la ” || echo { “yi qie zheng chang”;exit; }

 

75, 编写脚本/bin/per.sh ,判断当前用户对指定参数文件,是否不可读并且不可写。

答案:           [ -not -r $1 -a -not -w $1 ] && echo “bu ke du ”
[ −r$1−o−w$1

] || echo “ke du ”

 

 

76,编写脚本/root/bin/excute.sh ,判断参数文件是否为sh后缀的普通文件,如果是,添加所有人可执行权限,否则提示用户非脚本文件。

答案:            vim /root/bin/excute.sh                                                                                                                                                   [[ $1 =~ .*sh$ ]] && chmod +x $1 || echo "bu shi jiao ben wen jian "
Last modification:September 2, 2019
If you think my article is useful to you, please feel free to appreciate