作业 2016年7月30日

关于 文件系统 用户管理的基础练习题

练习题:(使用自己现有知识做的,答案仅供参考)

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

:在家目录中.bashrc中定义别名:

alias  baketc=' cp -rpv /etc/  /testdir/backup$(date +%F)'

cp  -r: 拷贝目录;-p:保留权限;-v:显示其过程    $(date  +%F):命令引用当前时间

由于我们还没有学习定时任务,所以可以每天手工的运行以上的别名可以实现数据备份。

练习2创建/testdir/rootdir目录并复制/root所有下文件到该目录内,要求保留原有权限?  

解:mkdir  -p  /testdir/rootdir  #:创建目录中的目录需要带参数 -p

   cp -rp   /root/  /testdir/rootdir   #: cp  -r: 拷贝目录;-p:保留权限

练习3如何创建/testdir/dir1/x, /testdir/dir1/y, /testdir/dir1/x/a, /testdir/dir1/x/b, /testdir/dir1/y/a, /testdir/dir1/y/b

解:mkdir -vp /testdir/dir1/{x,y}/{a,b}

Dir1

├── x

   ├── a

   └── b

└── y

    ├── a

    └── b

[root@wCentos7 testdir]# mkdir -vp /testdir/dir1/{x,y}/{a,b}

mkdir: created directory /testdir/dir1

mkdir: created directory /testdir/di1/x

mkdir: created directory /testdir/dir1/x/a

mkdir: created directory /testdir/dir1/x/b

mkdir: created directory /testdir/dir1/y

mkdir: created directory /testdir/dir1/y/a

mkdir: created directory /testdir/dir1/y/b

 

练习4如何创建/testdir/dir2/x,/testdir/dir2/y,/testdir/dir2/x/a,/testdir/dir2/x/b

解:mkdir -vp /testdir/dir2/{x/{a,b},y}

需要得到的目录树为:

dir2

├── x

   ├── a

   └── b

└── y

 

[root@wCentos7 testdir]# mkdir -vp /testdir/dir2/{x/{a,b},y}

mkdir: created directory /testdir/dir2

mkdir: created directory /testdir/dir2/x

mkdir: created directory /testdir/dir2/x/a

mkdir: created directory /testdir/dir2/x/b

mkdir: created directory /testdir/dir2/y

练习5如何创建/testdir/dir3, /testdir/dir4, /testdir/dir5, /testdir/dir5/dir6, /testdir/dir5/dir7

 

解:mkdir -vp /testdir/{dir3,dir4,dir5/{dir6,dir7}}

需要得到的目录树结构为:

├── dir3

├── dir4

├── dir5

   ├── dir6

   └── dir7

 

[root@wCentos7 testdir]# mkdir -pv /testdir/{dir3,dir4,dir5/{dir6,dir7}}

mkdir: created directory /testdir/dir3

mkdir: created directory /testdir/dir4

mkdir: created directory /testdir/dir5

mkdir: created directory /testdir/dir5/dir6

mkdir: created directory /testdir/dir5/dir7

练习6/etc/issue文件中的内容转换为大写后保存至/tmp/issue.out文件中?

cat /etc/issue | tr 'a-z' 'A-Z' > /tmp/issue.out

解:管道的作用就是把上个命令的输出,当做下一个命令的输入。

Cat 看的到文件内容输出给 tr ,

 

tr  'a-z'  'A-Z'

tr把输入的内容所有小写“a-z”转换为 大写的“A-Z

 

[root@wCentos7 testdir]# cat /etc/issue | tr 'a-z' 'A-Z' >/tmp/issue.out

[root@wCentos7 testdir]# cat /tmp/issue.out

\S

KERNEL \R ON AN \M

 

========================

WELCOME TO MY HOSTNAME SA

WELCOME TO MY HOSTNAME SB

=========================

[root@wCentos7 testdir]# cat /etc/issue

\S

Kernel \r on an \m

 

========================

welcome to my hostname SA

welcome to my hostname SB

=========================

 

 

 

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

解:who |  tr 'a-z' 'A-Z' >/tmp/who.out

 

Who :查看当前系统登录用户 who 命令;

转换大写: tr 'a-z' 'A-Z'  

>  :输出从定向到 /tmp/who.out文件

 

[root@wCentos7 testdir]# who  |  tr 'a-z' 'A-Z' >/tmp/who.out

[root@wCentos7 testdir]# who

root     :0           2016-07-28 08:36 (:0)

root     pts/0        2016-07-30 08:41 (10.1.16.1)

[root@wCentos7 testdir]# cat /tmp/who.out

ROOT     :0           2016-07-28 08:36 (:0)

ROOT     PTS/0        2016-07-30 08:41 (10.1.16.1)

 

 

 

练习8一个linux用户给root发邮件,要求邮件标题为”help”,邮件正文如下:

Hello, I am 用户名,the system version is here,pleasehelp me to check it ,thanks!

操作系统版本信息

解:重点在于如何实现邮件调用系统的命令:

 用户名 : id -un

 操作系统版本信息 : cat /etc/redhat-release

mail -s "hello" root <root.mail

nano  root.mail

Hello, I am `id -un`,the system version is here,pleasehelp me to check it ,thanks!

`cat /etc/redhat-release`

 

练习9/root/下文件列表,显示成一行,并文件名之间用空格隔开?

解:ls -1 /root | tr "\n" " "

重点:

ls 先让文件列表一列显示;

替换掉每行的换行符 ”\n“为 空格即可 实现我们的要求

 

 

 

[root@wCentos7 ~]# ls -1 /root

anaconda-ks.cfg

bash1.sh

bash.sh

Desktop

Documents

Downloads

dz

dz.zip

f11

f1.txt

initial-setup-ks.cfg

linux.txt

Music

Pictures

Public

Templates

Videos

windows_tr.txt

windows.txt

zzzzzzz

 

[root@wCentos7 ~]# ls -1 /root | tr "\n" " "

anaconda-ks.cfg bash1.sh bash.sh Desktop Documents Downloads dz dz.zip f11 f1.txt initial-setup-ks.cfg linux.txt Music Pictures Public Templates Videos windows_tr.txt windows.txt zzzzzzz

[root@wCentos7 ~]#

 

 

练习10file1文件的内容为:”1 2 3 4 5 6 7 8 9 10” 计算出所有数字的总和?

解:由于内容确定了,文件的格式确定了,我们可以分析:

把文件内容中每个数字之间的空格换成”+“号,再让我们的计算器程序 bc 区统计和即可

 

[root@wCentos7 ~]# cat file1_sum

1 2 3 4 5 6 7 8 9 10

[root@wCentos7 ~]# echo " $(cat file1_sum | tr " " "+") "| bc

55

 

练习11删除Windows文本文件中的'^M'字符

 

解:我们得先确定,相同的内容的文件,在windowslinux中有上面区别,

Windows.txt linux.txt

 

[root@wCentos7 ~]# cat windows.txt  #windows传到linux文件

hello!

what is your name ?

my name is  sb

 

 

how are you ?

 

 

 

bey-bey!

 

[root@wCentos7 ~]# cat linux.txt  #linux上相同内容的文件

hello!

what is your name ?

my name is sb

 

 

how are you ?

 

 

 

bey-bey!

 

[root@wCentos7 ~]# ll windows.txt linux.txt   #相同内容的文件大小不一样

 

-rw-r–r–. 1 root root 70 Jul 30 10:13 linux.txt

-rwxr–r–. 1 root root 80 Jul 30 10:11 windows.txt

 

 

[root@wCentos7 ~]# hexdump -c windows.txt  #查看16进制的windows文件

0000000   h   e   l   l   o   !  \r  \n   w   h   a   t       i   s    

0000010   y   o   u   r       n   a   m   e       ?  \r  \n   m   y    

0000020   n   a   m   e       i   s           s   b  \r  \n  \r  \n  \r

0000030  \n   h   o   w       a   r   e       y   o   u       ?  \r  \n

0000040  \r  \n  \r  \n  \r  \n   b   e   y   –   b   e   y   !  \r  \n

0000050

[root@wCentos7 ~]# hexdump -c linux.txt  #查看16进制的linux文件

0000000   h   e   l   l   o   !  \n   w   h   a   t       i   s       y

0000010   o   u   r       n   a   m   e       ?  \n   m   y       n   a

0000020   m   e       i   s       s   b  \n  \n  \n   h   o   w       a

0000030   r   e       y   o   u       ?  \n  \n  \n  \n   b   e   y   –

0000040   b   e   y   !  \n  \n                                        

0000046

[root@wCentos7 ~]# cat windows.txt | tr -d  "\r">windows_tr.txt

通过观察发现:windows的文件多出了 \r的字符,所以删除掉 \r字符即可,删除后文件的内容还是有一点不一样,就是\n的数量问题,linux\n数量比windows的要多些,现在我们处理不了,了解windowslinux文本文件的有什么区别即可。

 

 

[root@wCentos7 ~]# ll

total 36

-rw——-. 1 root root        1417 Jul 21 11:34 anaconda-ks.cfg

-rw-r–r–. 1 chen ChenJiaShun    0 Jul 28 16:33 bash1.sh

-rw-r–r–. 1 root root           0 Jul 28 16:33 bash.sh

drwxr-xr-x. 2 root root           6 Jul 21 11:54 Desktop

drwxr-xr-x. 3 root root          17 Jul 27 23:14 Documents

drwxr-xr-x. 2 root root          22 Jul 28 11:39 Downloads

-rw-r–r–. 1 root root          52 Jan 12  2016 dz

-rwxrw-rw-. 1 root root         154 Jul 29 18:13 dz.zip

-rw-r–r–. 1 root root          12 Jul 27 10:19 f11

-rwxrw-rw-. 1 root root          15 Jul 27 10:15 f1.txt

-rw——-. 1 root root        1465 Jul 21 11:35 initial-setup-ks.cfg

-rw-r–r–. 1 root root          70 Jul 30 10:13 linux.txt

drwxr-xr-x. 2 root root           6 Jul 21 11:54 Music

drwxr-xr-x. 2 root root           6 Jul 21 11:54 Pictures

drwxr-xr-x. 2 root root           6 Jul 21 11:54 Public

drwxr-xr-x. 2 root root           6 Jul 21 11:54 Templates

drwxr-xr-x. 2 root root           6 Jul 21 11:54 Videos

-rw-r–r–. 1 root root          70 Jul 30 10:17 windows_tr.txt

-rwxr–r–. 1 root root          80 Jul 30 10:11 windows.txt

-rw-r–r–. 1 root root           0 Jul 27 23:24 zzzzzzz

[root@wCentos7 ~]# hexdump -c linux.txt

0000000   h   e   l   l   o   !  \n   w   h   a   t       i   s       y

0000010   o   u   r       n   a   m   e       ?  \n   m   y       n   a

0000020   m   e       i   s       s   b  \n  \n  \n   h   o   w       a

0000030   r   e       y   o   u       ?  \n  \n  \n  \n   b   e   y   –

0000040   b   e   y   !  \n  \n                                        

0000046

[root@wCentos7 ~]# hexdump windows_tr.txt

0000000 6568 6c6c 216f 770a 6168 2074 7369 7920

0000010 756f 2072 616e 656d 3f20 6d0a 2079 616e

0000020 656d 6920 2073 7320 0a62 0a0a 6f68 2077

0000030 7261 2065 6f79 2075 0a3f 0a0a 620a 7965

0000040 622d 7965 0a21                         

0000046

[root@wCentos7 ~]# hexdump -c  windows_tr.txt

0000000   h   e   l   l   o   !  \n   w   h   a   t       i   s       y

0000010   o   u   r       n   a   m   e       ?  \n   m   y       n   a

0000020   m   e       i   s           s   b  \n  \n  \n   h   o   w    

0000030   a   r   e       y   o   u       ?  \n  \n  \n  \n   b   e   y

0000040   –   b   e   y   !  \n                                        

0000046

 

练习12处理字符串“xt.,l 1 jr#!$mn2 c*/fe3 uz4”,只保留其中的数字和空格

 解:分析下字符串中相当与要删除字母,删除标点符号等字符;

我们用tr -d 删除字母  r -d "[[:alpha:]]"  

我们用tr -d 删除标点符号 r -d "[[:punct:]]"

 

echo "xt.,l 1 jr#!$mn2 c*/fe3 uz4"  | tr -d "[[:alpha:]]" | tr -d  "[[:punct:]]"

 

[root@wCentos7 testdir]# echo "xt.,l 1 jr#"[[:alpha:]]"mn2 c*/fe3 uz4 5    6   7   8"  | tr -d "[[:alpha:]]" | tr -d  "[[:punct:]]"

 1 2 3 4 5    6   7   8

[root@wCentos7 testdir]#

 

练习13PATH变量每个目录显示在独立的一行

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

解:分析下 $PATH是以 : 为分隔符,所以把 : 替换成换行 \n即可

 

[root@wCentos7 testdir]# echo $PATH | tr ':' '\n'

/usr/local/sbin

/usr/local/bin

/usr/sbin

/usr/bin

/root/bin

[root@wCentos7 testdir]# echo $PATH

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

 

练习14删除指定文件的空行

例如: file1

解 :cat /etc/issue  | tr -s "\n" \S

 #原文件中 换行为 \n 两个重复的\n删除一个保留一个 即可以实现删除空行的效果

 

[root@wCentos7 testdir]# cat /etc/issue  | tr -s "\n" \S

Kernel \r on an \m

========================

welcome to my hostname SA

welcome to my hostname SB

=========================

 

[root@wCentos7 testdir]# cat /etc/issue   

\S

Kernel \r on an \m

 

========================

welcome to my hostname SA

welcome to my hostname SB

=========================

 

练习15将文件中每个单词(字母)显示在独立的一行,并无空行

解:cat /etc/issue |tr "[[:space:]]" "\n" | tr -s "\n"

分析下文件内容:

需要每个字母的尾部空白行 替换为换行符 \n,tr "[[:space:]]" "\n"

再删除掉有重复换行符的即可:tr -s "\n"

 

 

[root@wCentos7 testdir]# cat /etc/issue

\S

Kernel \r on an \m

 

========================

welcome to my hostname SA

welcome to my hostname SB

=========================

 

 

[root@wCentos7 testdir]# cat /etc/issue |tr "[[:space:]]" "\n" | tr -s "\n"

\S

Kernel

\r

on

an

\m

========================

welcome

to

my

hostname

SA

welcome

to

my

hostname

SB

=========================

 

 

练习16创建用户gentoo,附加组为binroot,默认shell/bin/csh,注释信息为"Gentoo Distribution"

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

Useradd :添加用户的命令;

-G :设置用户的附加组

-s: 设置用户的shell环境

-c: 设置用户的描述 说明

 

[root@wCentos7 ~]# getent passwd gentoo #查看用户的说明

gentoo:x:1002:1002:Gentoo Distribution:/home/gentoo:/bin/csh

[root@wCentos7 ~]# getent group gentoo

gentoo:x:1002:

[root@wCentos7 ~]# cat /etc/group  #查看附加组中是否有gentoo用户信息

root:x:0:gentoo

bin:x:1:gentoo

…………..

gentoo:x:1002:

[root@wCentos7 ~]#

 

 

练习17创建下面的用户、组和组成员关系

名字为admins 的组

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

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

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

 

解:groupadd admins

#单独添加群组的命令

useradd -G admins natasha

#添加用户 natasha 并且设置附加组为 admins

useradd -G admins harry

#添加用户 harry并且设置附加组为 admins

useradd -s /sbin/nologin sarah

#添加用户 sarah 不让其登录设置shell为:/sbin/nologin即可

echo centos | passwd –stdin natasha

#passwd –stdin接收标准输入的密码 centos ,这样可以避免用户交互

echo centos | passwd –stdin harry

#passwd –stdin接收标准输入的密码 centos ,这样可以避免用户交互

echo centos | passwd –stdin sarah

#passwd –stdin接收标准输入的密码 centos ,这样可以避免用户交互

 

 

 

[root@wCentos7 ~]# groupadd admins

[root@wCentos7 ~]# useradd -G admins natasha

[root@wCentos7 ~]# useradd -G admins harry

[root@wCentos7 ~]# useradd -s /sbin/nologin sarah

[root@wCentos7 ~]# echo ecntos | passwd –stdin natasha

Changing password for user natasha.

passwd: all authentication tokens updated successfully.

[root@wCentos7 ~]# echo ecntos | passwd –stdin harry

Changing password for user harry.

passwd: all authentication tokens updated successfully.

[root@wCentos7 ~]# echo ecntos | passwd –stdin sarah

Changing password for user sarah.

passwd: all authentication tokens updated successfully.

[root@wCentos7 ~]#

 

 

   

 

[root@wCentos7 ~]# getent group admins

admins:x:1003:natasha,harry

[root@wCentos7 ~]# getent passwd sarah

sarah:x:1005:1006::/home/sarah:/sbin/nologin

[root@wCentos7 ~]# getent passwd natasha

natasha:x:1003:1004::/home/natasha:/bin/bash

[root@wCentos7 ~]# getent passwd harry

harry:x:1004:1005::/home/harry:/bin/bash

 

 

[root@wCentos7 ~]# w

 10:56:53 up 23:15,  5 users,  load average: 0.02, 0.04, 0.05

USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT

root     :0       :0               Thu08   ?xdm?  19:20   0.44s gdm-session-worker [pam/gdm-autologin]

root     pts/1    10.1.16.1        10:52    4:21   0.03s  0.03s -bash

root     pts/0    10.1.16.1        08:41    5.00s  2.77s  0.03s w

natasha  tty2                      10:53    3:25   0.02s  0.02s -bash

harry    tty3                      10:56   21.00s  0.02s  0.02s -bash

 

原创文章,作者:linux_root,如若转载,请注明出处:http://www.178linux.com/27699

(0)
上一篇 2016-08-05 16:20
下一篇 2016-08-05 16:20

相关推荐

  • 马哥教育网络班22期+第9周课程练习 忍者乱太郎喻成

    1、写一个脚本,判断当前系统上所有用户的shell是否为可登录shell(即用户的shell不是/sbin/nologin);分别这两类用户的个数;通过字符串比较来实现; #!/bin/bash declare -i user_can_not_login_count; declare -i user_can_login…

    Linux干货 2017-01-03
  • nginx

    http http协议:web服务器(类似于httpd)、http reverse proxy(类似于httpd)、imap/pop3 reverse proxy NGINX is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/P…

    Linux干货 2017-06-25
  • MAN 手册各章节功能介绍及快捷键键位整理

    MAN 手册各章节功能介绍及快捷键键位整理   Linux提供了丰富的帮助手册,当你需要查看某个命令的参数时不必到处上网查找,只要man一下即可。  1、Linux 的man手册共有以下几个章节:         1章节:用户命令     …

    Linux干货 2016-10-19
  • hello 北京&Linux启程

    北京初感&Linux启程

    Linux干货 2018-03-26
  • MBR实验

    实验一  MBR修复 破坏MBR lsblk  /*确认磁盘名*/ hexdump -C /dev/sda -n 512  /*查看MBR分区*/ dd if=/dev/sda of=/date/mbr.bak bs=1 count=512   /*复制MBR分区*/ scp /date/mbr.bak 192.168.213.129:/root/   /*…

    Linux笔记 2018-05-13