NO15 The first after-school exam

The first post-class exam:

 

1. Create a directory /data/oldboy, and create a file oldboy.txt in this directory, and then write the content in the file oldboy.txt: inet addr: 10.0.0.8 Bcast: 10.0.0.255 Mask: 255.255.255.0

[root@localhost ~]# mkdir /data/oldboy -p  (-p #Create a directory recursively, generally use -p when the first level directory does not exist. Otherwise, an error will be reported.)
[root@localhost ~]# ls -ld /data /oldboy/
drwxr-xr-x 2 root root 6 May 3 10:26 /data/oldboy/
[root@localhost ~]# touch /data/oldboy/oldboy.txt
[root@localhost ~]# echo "inet addr: 10.0.0.8 Bcast:10.0.0.255 Mask:255.255.255.0" >/data/oldboy.txt
[root@localhost ~]# cat /data/oldboy.txt
inet addr:10.0.0.8 Bcast:10.0.0.255 Mask:255.255. 255.0


2. Filter the contents of the oldboy.txt file in question 1 and output only the following contents:
10.0.0.8 10.0.0.255 255.255.255.0

[root@localhost data]# awk -F "[ :]+" '{print $3,$5,$7}' oldboy.txt
10.0.0.8 10.0.0.255 255.255.255.0
[root@localhost data]# awk -F "[ :]+" '{print $3" "$5" "$7}' oldboy.txt
10.0.0.8 10.0.0.255 255.255.255.0

 


3. Move the oldboy directory in question 1 to the /tmp directory, and copy the /etc/passwd file to /tmp/oldboy:

[root@localhost ~]# mv /data/oldboy /tmp
[root@localhost ~]# cp /etc/passwd /tmp/oldboy
[root@localhost ~]# ls /tmp/oldboy
oldboy.txt  passwd

 


4. On the basis of question 3, use awk to take the third column of lines 10 to 20 of the passwd file and redirect them to the /tmp/oldboy/test.txt folder:
[root@localhost oldboy]# awk 'NR> 9 && NR<21' passwd
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
abrt:x:173:173::/etc/abrt:/ sbin/nologin
libstoragemgmt:x:998:997:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
colord:x:997:996:User for colord:/var/lib/colord:/sbin/nologin
[root@localhost oldboy]# awk 'NR>9 && NR<21' passwd|cat -n
     1  operator:x:11:0:operator:/root:/sbin/nologin
     2  games:x:12:100:games:/usr/games:/sbin/nologin
     3  ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
     4  nobody:x:99:99:Nobody:/:/sbin/nologin
     5  systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
     6  dbus:x:81:81:System message bus:/:/sbin/nologin
     7  polkitd:x:999:998:User for polkitd:/:/sbin/nologin
     8  abrt:x:173:173::/etc/abrt:/sbin/nologin
     9  libstoragemgmt:x:998:997:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
    10  rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
    11  colord:x:997:996:User for colord:/var/lib/colord:/sbin/nologin
[root@localhost oldboy]# awk -F ":" 'NR>9 && NR<21{print $3}' passwd
11
12
14
99
192
81
999
173
998
32
997
[root@localhost oldboy]# awk -F ":" 'NR>9 && NR<21{print $3}' passwd >test.txt
[root@localhost oldboy]# cat test.txt
11
12
14
99
192
81
999
173
998
32
997

 


5. On the basis of question 3: When using the command rm to delete a file, the following prompts are prompted to prohibit the use of rm, and the modification effect will take effect permanently:
[root@localhost oldboy]# alias rm='echo Do not use rm command. '
[root@localhost oldboy]# ls
oldboy.txt passwd test.txt
[root@localhost oldboy]# rm test.txt
Do not use rm

[root@localhost oldboy]# ls /etc/profile /etc/bashrc (要rm改动永久生效要改这个俩个配置文件)
/etc/bashrc  /etc/profile
[root@localhost oldboy]# echo "alias rm='echo Do not use rm command.'" >>/etc/bashrc
[root@localhost oldboy]# echo "alias rm='echo Do not use rm command.'" >>/etc/profile
[root@localhost oldboy]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='echo Do not use rm command.'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

 


6. On the basis of question 3, delete other files except passwd under /tmp/oldboy:
[root@localhost oldboy]# cd
[root@localhost ~]# tree /tmp/oldboy
/tmp/oldboy
├── oldboy .txt
├── passwd
└── test.txt

0 directories, 3 files
[root@localhost ~]# find /tmp/oldboy -type f ! -name "passwd"   (! stands for negation)
/tmp/oldboy/oldboy.txt
/tmp/oldboy/test.txt
[root @localhost ~]# find /tmp/oldboy -type f ! -name "passwd"|xargs \rm -f   (Because rm was aliased just now, it needs to be escaped with "\", that is, escaped to rm itself)
[root@ localhost ~]# tree /tmp/oldboy
/tmp/oldboy
└── passwd

0 directories, 1 file

 

 

7. On the basis of question 3, please print lines 2-5 in the /etcpasswd file (no less than three methods):

方法一:
[root@localhost ~]# head -5 /etc/passwd |tail -4
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

Method 2:
[root@localhost ~]# sed -n '2,5p' /etc/passwd  (-n means cancel the default output)
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

方法三:
[root@localhost ~]# awk 'NR>1 && NR<6' /etc/passwd
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

 


8. On the basis of question 3, use the command to exchange the root location and the /bin/bash location in the passwd file, that is, to exchange the positions of all the first and last columns:

[root@localhost ~]# awk 'NR==1{print $0}' /etc/passwd
root:x:0:0:root:/root:/bin/bash
[root@localhost ~]# awk -F " :" 'NR==1{print $7":"$2":"$3":"$4":"$5":"$6":"$1}' /etc/passwd
/bin/bash:x:0:0 :root:/root:root
[root@localhost ~]# awk -F ":" '{print $7":"$2":"$3":"$4":"$5":"$6":"$1}' /etc/passwd      (remove NR==1, that is, display all lines like this)

 

 

9. Replace all the strings containing oldgirl in all files ending with the extension .txt in the /data directory and its subdirectories with oldboy:

先创建环境:
[root@localhost ~]# mkdir /data/3306
[root@localhost ~]# mkdir /data/3307
[root@localhost ~]# ls /data
123.txt  3306  3307  456.txt  oldboy.txt
[root@localhost ~]# echo "oldgirl1" >/data/123.txt
[root@localhost ~]# echo "oldgirl1" >/data/456.txt
[root@localhost ~]# echo "oldgirl1" >/data/3306/aa.txt
[root@localhost ~]# echo "oldgirl1" >/data/3307/bb.txt
[root@localhost ~]# tree /data
/data
├── 123.txt
├── 3306
│   └── aa.txt
├── 3307
│   └── bb.txt
├── 456.txt
└── oldboy.txt

2 directories, 5 files
[root@localhost ~]# find /data -type f -name "*.txt"
/data/oldboy.txt
/data/123.txt
/data/456.txt
/data/3306/aa.txt
/data/3307/bb.txt
[root@localhost ~]# find /data -type f -name "*.txt" |xargs cat
oldgirl1
oldgirl1
oldgirl1
oldgirl1
[root@localhost ~]# find /data -type f -name "*.txt" |xargs sed 's#oldgirl#oldboy#g'
oldboy1
oldboy1
oldboy1
oldboy1
[root@localhost ~]# find /data -type f -name "*.txt" |xargs sed -i 's#oldgirl#oldboy#g'     (加i生效了,结果就不会显示列在屏幕上了)
[root@localhost ~]# find /data -type f -name "*.txt"|xargs cat
oldboy1
oldboy1
oldboy1
oldboy1

 

 

10. Find all files larger than 1M ending in log 7 days ago under /oldboy and move to /tmp:

创建环境:
[root@localhost ~]# mkdir /oldboy
[root@localhost ~]# cd /oldboy
[root@localhost oldboy]# cat /var/log/dmesg >>123.log
[root@localhost oldboy]# cat /var/log/dmesg >>123.log
[root@localhost oldboy]# cat /var/log/dmesg >>123.log
[root@localhost oldboy]# cat /var/log/dmesg >>123.log
[root@localhost oldboy]# cat /var/log/dmesg >>123.log
[root@localhost oldboy]# cat /var/log/dmesg >>123.log
[root@localhost oldboy]# ls /oldboy
123.log
[root@localhost oldboy]# 11 -h
bash: 11: command not found...
[root@localhost oldboy]# ll -h
total 960K
-rw-r--r-- 1 root root 696K Apr  1 00:16 123.log
[root@localhost oldboy]# cat 123.log >>456.log
[root@localhost oldboy]# cat 123.log >>456.log
[root@localhost oldboy]# cat 123.log >>456.log
[root@localhost oldboy]# cat 123.log >>456.log
[root @localhost oldboy]# ll -h
total 4.6M
-rw-r--r-- 1 root root 696K Apr 1 00:16 123.log
-rw-r--r-- 1 root root 2.8M Apr 1 00: 17 456.log
[root@localhost oldboy]# ls -lh /oldboy    (there are 2 files in the oldboy directory here)
total 4.6M
-rw-r--r-- 1 root root 696K Apr 1 00:16 123.log
-rw-r--r-- 1 root root 2.8M Apr 1 00:17 456.log

Define the file time:
[root@localhost oldboy]# date -s 20180601 (the system time is adjusted later, the actual time is actually 2018-5-3)
Fri Jun 1 00:00:00 CST 2018
[root@localhost oldboy]# stat 123 .log (check file creation time)
  File: '123.log'
  Size: 712614 Blocks: 1392 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 2706511 Links: 1
Access: (0644/-rw-r--r --) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2018-04-01 00:17:08.001906285 +0800
Modify: 2018-04-01 00:16:06.678311769 +0800
Change: 2018-04 -01 00:16:0 6.678311769 +0800

[root@localhost oldboy]# find /oldboy -type f -name "*.log" -size +1M -mtime +7   (find files that meet the conditions of the title)
/oldboy/456.log
[root@localhost oldboy]# find /oldboy -type f -name "*.log" -size +1M -mtime +7 -exec mv {} /tmp \;    (no error is the best news, haha)
[root@localhost oldboy]# ls -lh /tmp (check if there is a 456.log file under /tmp)
total 3.6M
-rw-r--r-- 1 root root 2.8M Apr 1 00:17 456.log

 

 

11. What is the Linux run level, please describe the meaning of the different numbers of the Linux run level (additional question):

Default runlevel.The runlevels used are:
0-halt(Do NOT set initdefault to this) Shutdown.
1-Single user mode is a single user state, which is used when the server needs to be maintained, such as password loss.
2-Multiuser,without NFS(The same as 3,if you not have networking) Multiuser mode.
3-Full multiuser mode Complete multi-user mode, command line mode. The working environment generally uses 3 levels.
4-unused Not used, reserved.
5-Xll Mode with desktop.
6-reboot (Do NOT set initdefault to this) reboot.

Check the current Linux running mode:
[root@localhost ~]# runlevel
N 5
run level can be switched:
[root@localhost ~]# init Enter numbers 0-6 to switch to the mode you want.

 

12. Please describe the difference between buffer and cache (additional question):

Write data to memory, and the memory space of this data becomes a buffer: buffer . write buffer.
Read data from memory, the memory space where the data is stored becomes the cache area: cache . memory buffer.

 

 

13. The meaning of the following characters in Linux:


1 > or l> #Output redirection: Input the previous output into the following file, and the original content of the file will be clear.
2 >> or l>> #Append redirection: append the previous output to the end of the following file, it will not clear the original content of the file.
3 0<or< #Input redirection: Input redirection is used to change the input of the command, followed by the input content, followed by the file name.
4 0<< or << #Append input redirection: followed by a string to indicate "end of input", and ctrl+d can also be used to end input.
5 2> #Error redirection: Enter the error information into the following file, and the original content of the file will be deleted.
6 2>> #Error append redirection: Append the error message to the following file without deleting the original content of the file.
7 * # wildcard, representing all.
8 | # means pipeline
9 .. # upper level directory
10 .
#Current directory 11 = #In linux, one = sign means assignment, and two == signs mean equal.
12 / # In linux, it represents the root, the vertex of all directories, and the separator of the path.
13; #Command separator
14 {} #Generate a sequence of characters or numbers, similar to seq, generally used with commands such as echo. The {} in find means the result of the previous command.
                  [root@localhost ~]# rm -fr /data
                  [root@localhost ~]# mkdir /data/{3306,3307}/data -p
                  [root@localhost ~]# tree /data
                    /data
                  ├── 3306
                  │ └── data
                  └── 3307
                  └── data
                  4 directories, 0 files
15 ! #1. !+Letter: Indicates to call up the most recent command starting with this letter.
               2. !!: Indicates the command that uses the last operation.
               3. !+Number: Indicates which command in the history is called.
                      178 cd /data
                     179 mkdir /data/{3306.3307}/data -p
                     181 history
                     [root@localhost ~]# !178
                     cd /data
              Fourth, negate (find, shell programming) such as find a file, add! is to list all files except which one is found.
                     [root@localhost data]# cd /oldboy
                     [root@localhost oldboy]# ll
                     total 8
                     -rw-r--r--. 1 root root 0 Apr 25 17:02 a
                     -rw-r--r--. 1 root root 0 Apr 25 17:02 b
                     -rw-r--r--. 1 root root 0 Apr 25 17:02 c.txt
                     drwxr-xr-x. 3 root root 29 Apr 25 17:19 test
                     -rw -r--r--. 1 root root 7 Apr 25 17:19 test.sh
                     -rw-r--r--. 1 root root 7 Apr 25 17:19 t.sh
                     [root@localhost oldboy]# find /oldboy -type f -name "test.sh"
                     /oldboy/test.sh
                     [root@localhost oldboy]# find /oldboy -type f ! -name "test.sh" (! Take the reverse result)
                      /oldboy/test/del.sh
                     /oldboy/.sh
                     /oldboy/t.sh
                     /oldboy/ a
                     /oldboy/b
                     /oldboy/c.txt
16 ~ #The home directory of the current user.
17 - #The directory where the user was last. Controlled by the OLDPWD variable.
                      cd - : Go back to the previous directory.
18 \ # Escape characters, let meaningful characters, take off the meaning it represents. For example: In regular expressions, $ means that it ends with ..., you can use \$ to mean the $ symbol itself.
19 && #And, and.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325208396&siteId=291194637