關於 文件系統 用戶管理的基礎練習題

關於 文件系統 用戶管理的基礎練習題

練習題:(使用自己現有知識做的,答案僅供參考)

練習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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章