linux练习题

linux练习题linux

一、指定格式显示当前时间,格式:2017-11-09 10:20:30

    [root@cent6OS ~]#date "+%Y-%m-%d %H:%M:%S"

    2017-11-19 19:04:14

     date –d  “-6 day”  “+%F  %T” 指定格式显示六天前的日期

date –d  “yesterday”

date –d  “10 month”

    date  "+%s"   距离1970年1月1号的秒数时间

   `echo date  "+%s" `/ 3600/24 | bc   将秒数转化为天数

二、显示前天是星期几

  今天星期三 两天前是星期一  0或7-6  0或7表示星期日

    [root@centos7 ~]#date -d "2 day ago" "+%u"

    6

  设置当前日期为2017-11-09  16:26:00

 date -s "16:26:00 2017-11-09" "+%Y-%m-%d %H:%M:%S"

三、在本机字符终端登陆时,除显示原有信息外,再显示当前登陆终端号,主机名和当前时间
登陆的终端号 时间 主机名
Nano  /etc/issue 
The hostname is \n 主机名
Time is  \t  时间
TTy is \l  登陆终端

四、今天18:30自动关机,并提示用户
shutdown  -P(power off)  "18:30"
shutdown  -h(halt)  now  当即关机
shutdown  -r  +2  2分钟后重启 

2、文件管理

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

            ls -d  /var/1*[0-9]*[[:lower:]]
  二、显示/etc目录下以任意一位数字开头,且以非数字结尾的 文件或目录
            ls  -d  /etc/[0-9]*[^[:digit:]]       tuolefu ^
  三、显示/etc/目录下以非字母开头,后面跟了一个字母及其 它任意长度任意字符的文件或目录

            ls  -d   [^[:alpha:]][[:alpha:]]*
  四、显示/etc/目录下全部以rc开头,并后面是0-6之间的数 字,其它为任意字符的文件或目录
            ls  -d  /etc/rc[0-6]*
  五、显示/etc目录下,全部以.d结尾的文件或目录
            l s  -d  /etc/*.d
  六、显示/etc目录下,全部.conf结尾,且以m,n,r,p开头的文 件或目录
            ls  -d  [mnrp]*.conf
  七、只显示/root下的隐藏文件和目录
            ls  -d  /root/.[^.]*
             ls  -aI  /root/[^.]* 
  八、只显示/etc下的非隐藏目录
                ls  -d   /etc/[^.]*/
     总结
        *显示普通文件  压缩文件  可执行程序
        */只显示目录
    (1) 如何建立/testdir/dir1/x, /testdir/dir1/y,
        /testdir/dir1/x/a, /testdir/dir1/x/b,
        /testdir/dir1/y/a, /testdir/dir1/y/b
            mkdir -pv /testdir/dir1{x,y}/{a,b}
    (2) 如何建立/testdir/dir2/x, /testdir/dir2/y,
        /testdir/dir2/x/a, /testdir/dir2/x/b
      mkdir /testdir/dir2/{x/{a,b},y} -pv
    (3) 如何建立/testdir/dir3, /testdir/dir4, /testdir/dir5, /testdir/dir5/dir6,                 /testdir/dir5/dir7
      mkdir -pv /testdir/dir{3,4,5/dir{6,7}}

3、重定向与管道
 一、将/etc/issue文件中的内容转换为大写后保存至/tmp/issue.out文件中
   tr   'a-z'  'A-Z' <  /etc/issue   >  /tmp/issue.out
二、将当前系统登陆用户的信息转换为大写后保存至/tmp/who.out文件中
who | tr [a-z] [A-Z] > /tmp/who.out
 三、一个linux用户给root发邮件,要求邮件标题为”help”,邮件正文以下:Hello, I am 用户名,The system version is here,please help me to check it ,thanks!
操做系统版本信息

Mail   -s  "help"  root  <<EOF

>Hello, I am  `whoami` | $USER  ,The system version is here,please help me to check it ,thanks!

 >  Uname -r       系统内核信息

>Cat  /etc/centos-release    操做系统版本信息

         >  EOF

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

    ls/root/ | tr '\n'  ' '

 五、计算1+2+3+..+99+100的总和

[root@centos7 ~]#seq --separator="+" 1 100 | bc

5050

[root@centos7 ~]#seq -s '+' 1 100 | bc

5050

[root@centos7 ~]#echo {1..100} | tr " " "+" | bc

5050

[root@centos7 ~]#

  六、删除Windows文本文件中的‘^M’字符

   tr  -d   '\r'  <  win.txt    >  linux.txt  

   tr   -d   '\15'   win2.txt  > linux2.txt

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

[root@centos7 ~]#echo   'xt.,l 1 jr#!$mn 2 c*/fe 3 uz 4' |  tr -dc '[0-9]  \n'

'xt.,l 1 jr#!$mn 2 c*/fe 3 uz 4'    单引号认为!$为普通字符  ,不认为它是上个命令的最后一个参数

 八、将PATH变量每一个目录显示在独立的一行

echo $PATH | tr ':' '\n'

 九、将指定文件中0-9分别替代成a-j

tr [0-9] [a-j] < testfile.txt

十、将文件中每一个单词(由字母组成)显示在独立的一行,并没有空行

    tr  -sc  'a-z大A到Z'  '\n'   <  /etc/centos-release

4、用户组和权限管理

一、建立用户gentoo,附加组为bin和root,默认shell为/bin/csh,注释信息为"Gentoo Distribution"

Useradd  -G  bin,root  -s /bin/csh  -c  "Gentoo Distribution"  gentoo

二、建立下面的用户、组和组成员关系

名字为admins 的组

用户natasha,使用admins 做为附属组

用户harry,也使用admins 做为附属组

用户sarah,不可交互登陆系统,且不是admins 的成员,natasha,harry,sarah密码都是centos

    groupadd  admins;

     useradd   -G   admins  natasha;  useradd   -G   admins  harry;

     useradd   -s  /sbin/nologin  sarah ;

      echo  centos  |  passwd  --stdin natasha;

        echo  centos  |  passwd  --stdin harry;

       echo  centos  |  passwd  --stdin sarah;

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

不能cd进入该目录  目录下的文件什么都不能作(即便文件有777最高权限)

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

     不能查看目录的文件列表

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

         file1文件是不能删除的,不能修改

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

     file1文件能够删除,可是不能修改

七、复制/etc/fstab文件到/var/tmp下,设置文件全部者为wangcai读写权限,所属组为sysadmins组有读写权限,其余人无权限

cp  /etc/fstab   /var/tmp/ ;

chmod

u=rw,g=rw,o=  fstab

chown

wangcai:sysadmins  fstab

八、误删除了用户haha的家目录,请重建并恢复该用户家目录及相应的权限属性、全部者

cp -r /etc/skel /home/haha  (复制目录并更名)

chown -R haha:haha /home/haha

chmod 700 /home/haha

haha用户的全部者和全部组为haha,因此需更改

九、在/testdir/dir里建立的新文件自动属于g1组,组g2的成员如:alice能对这些新文件有读写权限,组g3的成员如:tom只能对新文件有读权限,其它用户(不属于g1,g2,g3)不能访问这个文件夹。

[root@centos7 app]#mkdir /testdir/dir

[root@centos7 app]#groupadd g1

[root@centos7 app]#groupadd g2

[root@centos7 app]#groupadd g3

[root@centos7 app]#chgrp g1  /testdir/dir

[root@centos7 app]#chmod g+s  /testdir/dir

建立用户并添加到相应组

[root@centos7 ~]#setfacl -m d:g:g2:rw,d:g:g3:r,o::- /testdir/dir  (新建立目录不须要加-R递归设置默认权限)

sed文本处理

一、删除centos7系统/etc/grub2.cfg文件中全部以空白开头的行行首的空白字符

[root@centos7 ~]$ sed -nr 's@^([[:space:]]+)(.*)@\2@p' /etc/grub2.cfg

二、删除/etc/fstab文件中全部以#开头,后面至少跟一个空白字符的行的行首的#和空白字符

[root@centos7 ~]$ sed -nr 's@(^#[[:space:]])(.*)@\2@p' /etc/fstab

三、在centos6系统/root/install.log每一行行首增长#号

[root@centos6 ~]# sed -nr 's@.*@#\0@p' /root/install.log

四、在/etc/fstab文件中不以#开头的行的行首增长#号

[root@centos6 ~]# sed -nr 's@^[^#].*@#\0@p' /etc/fstab

五、处理/etc/fstab路径,使用sed命令取出其目录名和基名

[root@centos6 ~]# echo "/etc/fstab"   |sed -nr 's@(.*/)([^/]+)/?@\2@p'
fstab
[root@centos6 ~]# echo "/etc/fstab/"   |sed -nr 's@(.*/)([^/]+)/?@\2@p'
fstab
[root@centos6 ~]# echo "/etc/"   |sed -nr 's@(.*/)([^/]+)/?@\1@p'

六、利用sed 取出ifconfig命令中本机的IPv4地址

[root@centos7 ~]$ ifconfig ens33 |sed -nr '2s@.*inet (.*) netmask.*@\1@p'

v七、统计centos安装光盘中Package目录下的全部rpm文件的以.分隔倒数第二个字段的重复次数

root@centos7 ~]$ ls /misc/cd/Packages/ |sed -nr 's@.*\.([^.]*)\.rpm@\1@p' |sort  |uniq -c
2141 i686
3076 noarch
4374 x86_64

八、统计/etc/init.d/functions文件中每一个单词的出现次数,并排序(用grep和sed两种方法分别实现)

sed方法

[root@centos7 ~]#sed -r 's/[^[:alpha:]]/\n/g'

/etc/init.d/functions | sed '/^$/d' | uniq -c | sort -n | wc -l

grep方法(注意非正则符号前加斜杠)

egrep -o  "[[:alpha:]]+" /etc/init.d/functions | uniq -c | sort | wc -l

九、将文本文件的n和n+1行合并为一行,n为奇数行

[root@centos7 ~]#seq 1 10 | sed 'N;s/\n/ /'

1 2

3 4

5 6

7 8

9 10

[root@centos7 ~]#seq 1 10 | xargs -n3 (设置最多的参数个数)

1 2 3

4 5 6

7 8 9

10
相关文章
相关标签/搜索