Linux Getting Started first monthly exam

Source: Internet
Author: User
Tags echo command egrep

1.1 Create the directory/data/hao, and create the file Hao.txt in the directory, and then write the contents "inet addr:10.0.0.8bcast:10.0.0.255 mask:255.255.255.0" in the file Hao.txt (not including quotation marks). (not less than two kinds of 2 methods)

#搭建测试环境

#命令

mkdir Creating a directory Touch file

[Email protected]/]# mkdir-p/date/hao

[Email protected]/]# Touch/date/hao/hao.txt

Method One:

#命令

echo Print output > Output redirection (Append to the last line of the file to empty the original content)

[Email protected]/]# echo "inetaddr:10.0.0.8 bcast:10.0.0.255 mask:255.255.255.0" >/date/hao/hao.txt

#检查

[Email protected]/]# Cat/date/hao/hao.txt

inet addr:10.0.0.8bcast:10.0.0.255 mask:255.255.255.0

Method Two: vi vim edit text

VI or VIM editor/date/hao/hao.txt

Press I or O to edit

Press ESC

Enter Wq save to exit

Method Three:

Command:

Cat view file >> Append output redirect (append to file the last line does not empty the original content)

[email protected]/]# cat >>/date/hao/hao.txt <<efo

> inet addr:10.0.0.8bcast:10.0.0.255 mask:255.255.255.0

> EFO

1.2 The contents of the Hao.txt file in the title are filtered by command to output only the following: (no less than two 2 methods)

10.0.0.8 10.0.0.255 255.255.255.0

Method One:

Command:

The chief of the Three Musketeers is good at fetching columns-F specified delimiter $ is the first column to fetch a file row nr==2 rows

[[email protected] hao]# cat Hao.txt |awk-f "[:]" ' {print $ "" $ $ "" $7} '

10.0.0.8 10.0.0.255 255.255.255.0

Method Two:

Command:

SED regular matches all uppercase and lowercase letters replaced by empty

[email protected] hao]# cat hao.txt |sed ' s#[a-z:]# #g '

Method Three:

Command:

Egrep-o Precise search Filter

The base regular "" matches the contents of the parentheses

+ Extend regular repeating one or more of the preceding characters consecutively

[[email protected] hao]# cat hao.txt |egrep-o "[0-9.] +" | Xargs

10.0.0.8 10.0.0.255 255.255.255.0

Method Four:

Command:

Egrep

[] Regular matches the content inside

[^a-z] excludes all uppercase and lowercase letters

+ extension regular repeats the preceding character or multiple characters consecutively

[email protected] hao]# cat hao.txt |egrep-o "[^a-z:]+" | Xargs

10.0.0.8 10.0.0.255 255.255.255.0

Method Five: SED extended regular match

[Email protected] yuekao]# sed-r ' s#^.*ddr: (. *) bc.*:(. *) ma.*:(. *) $#\1 \2 \3#g ' Hao.txt

10.0.0.8 10.0.0.255 255.255.255.0

Method Six: Awk modifies the RS end flag

[[email protected] yuekao]# awk ' begin{rs= ': '}nr>=2{print $ ' hao.txt

10.0.0.8

10.0.0.255

255.255.255.0

1.3 On the basis of the above question, use the command to replace the root position and/bin/bash position in the passwd file. Will all the first and last column locations be swapped?

Cases:

Default: Root:x:0:0:root:/root:/bin/bash

Modified:/bin/bash:x:0:0:root:/root:root

Method One:

Command:

Awk is good at taking column-F to specify delimiter print

[Email protected]/]# CAT/ETC/PASSWD | Head-n 1

Root:x:0:0:root:/root:/bin/bash
[[email protected]/]# cat/etc/passwd |head-n 1 | Awk-f ":" ' {print$7 ":" $ $ ":" $ $ ":" $4 ":" $ $ ":" $6 ":" $ "

/bin/bash:x:0:0:root:/root:root

Method Two:

$NF represents the last column

Awk-f ' [:] ' {print$nf ': ' $ $ ': ' $ $ ': ' $4 ': ' $ $ ': ' $6 ': ' $ '/etc/passwd

Method Three:

Command:

SED replace regular use

()

[[email protected]/]# cat/etc/passwd |head-n 1 | Sed-r ' s# (. *) (: x.*t:) (. *) #\3\2\1#g '

/bin/bash:x:0:0:root:/root:root

1.4 Test.txt content is:

Trainning

Fanbingbing

Lidao

Please give the command that does not contain the trainning string when outputting the contents of the Test.txt file. (not less than 2 methods)

#搭建测试环境

Command:

Touch Create file Change file or directory timestamp

Cat Viewing files

[email protected]/]# Touch test.txt

[email protected]/]# cat >>test.txt <<efo

> trainning

> fanbingbing

> Lidao

> Efo

Method One:

Command: Tail fetch the end of the file.

[email protected]/]# cat Test.txt |tail-n 2

Fanbingbing

Lidao

Method Two:

Command: grep filter character-v exclusion

[email protected]/]# Cat Test.txt | Grep-v "Trainning"

Fanbingbing

Lidao

Method Three:

Command: The chief of awk is good at fetching NR representative lines; separators

[email protected]/]# Cat Test.txt | awk "nr==2; Nr==3 "

Fanbingbing

Lidao

Method Four:

Command: SED The Three Musketeers are good at going.-N cancels the default output

[email protected]/]# Cat Test.txt | Sed-n ' 2,3p '

Fanbingbing

Lidao

Method Five:

Sed excels at fetching columns to delete rows

[email protected]/]# Cat Test.txt | Sed '/trainning/d '

Fanbingbing

Lidao

#方法很多

Grep–v ' ^t ' test.txt #正则匹配t开头的字符 ^ with what begins

Grep–v ' ning$ ' test.txt #正则匹配ning $ end of the character $ with what end

1.5 Remove the contents of the 30th to 40th line of the file ett.txt.

Note: ett.txt by seq 120>ett.txt to create old Boys Linux operation and maintenance combat education

Build test environment command SEQ generate serial number > output redirect

[email protected]/]# seq >ett.txt

Method One:

Command: SED excels at fetching rows-N to cancel default output

[Email protected]/]# sed-n ' 30,40p ' ett.txt

Method Two:

Command: Head view the first few lines of the file tail view the file after a few lines-n Specify the number of rows displayed

[email protected]/]# Cat Ett.txt | Head-n |tail-n11

Method Three:

Command awk to be good at fetching NR representative rows

[email protected]/]# Cat Ett.txt | awk ' nr>=30 && nr<=40 '

Method Four:

Command: awk, space meaning

[Email protected]/]# awk ' nr==30,nr==40 ' ett.txt

1.5 Find all files ending in. txt in the/data directory and modify the trainning in the file to Hao. (not less than 2 methods)

Build a test environment

[[email protected] date]# Touch {1..10}.txt

[Email protected] date]# echo "trainning" >1.txt

[Email protected] date]# echo "trainning" >2.txt

[Email protected] date]# echo "trainning" >3.txt

Method One:

Command: Find file-type type F file d directory-name name-size size-mtime days-perm Find permission-maxdepth maximum depth (directory structure)

[Email protected]/]# find/date/-type f-name "*.txt" | Xargs sed-i ' s#trainning#hao#g '

[Email protected]/]# Cat/date/1.txt

Hao

Method Two:

$ () executes the contents of the parentheses first sed-i replace the contents of the file

[[email protected]/]# sed-i ' s#hao#trainning#g ' $ (find/date/-type f-name "*.txt")

[Email protected]/]# Cat/date/1.txt

Training #根据上面替换回来

1.6 Requirements When using RM command prompt commandnot found, how to implement?

[Email protected]]$ rm passwd

Command not found passwd

Alias rm= ' echo command not found ' temporary modification

echo "aliasrm= ' echo Command not found '" >>/etc/profile permanently modified

echo "aliasrm= ' echo Command not found '" >>/ETC/BASHRC permanently modified

The RM in the. bashrc file in the home directory is commented out

Source/etc/profile Effective etc

Command:

Alias alias setting Unalias alias

1.7 On the basis of Title 3, delete all files except the passwd file in the/tmp/hao/directory.

/tmp/hao/

|--hao.txt

|--passwd

'--test.txt

0 directories, 3 files

Find/tmp/hao–type f! –name "passwd" |xargs rm-f

Command: Find Files! Take the anti-exclusion

1.8 Please say what you know the following characters in Linux can represent the meaning of

User home Directory

. Current directory:

Top level Directory | pipe character, or (and)

> Output redirection

>> Append output:::

< input:::

<< Append input:::

#root用户, notes

1.9 describes several of the startup/RunLevel levels of Linux and what they mean.

7 Levels of operation

Init0 shutdown mode

1 Single User:

2 No NFS multi-user

3 command-line mode or full multi-user with NFS:

4 No Use

5 graphical interface

6 Restart:

1.10 Find all files in the/hao directory that are 7 days old, ending with log and larger than 1M, and move the files to/tmp. (not less than 2 methods)

Command MV to move files or directories

MV $ (find/hao–type f–mtime +7–name "*log" –size1m)/tmp

Find/hao–type f–mtime +7–name "*log" –size 1M |MV $ (xargs)/tmp

1.11 Common system Files FAQ 1.12 By modifying the file (/etc/fstab), you can set the file system to mount automatically when booting. 1.13 In a Linux system, when there is no condition in LAN (LAN) to remember the case DNS server, but also want to let users within the LAN can use the computer name to access each other, should be configured (/etc/hosts) file (please write the full path) 1.14 Linux system boot loading is complete, The kernel launches a program called (INIT), which is the first program that the kernel runs after the boot process is complete. We can modify the default boot level to (3), which allows the system to automatically log in with command line mode after rebooting.

1.12.4 write out the full path of the configuration file for the first Linux network card ()

/etc/sysconfig/network-scripts/ifcfg-eth0

1.15 count all files in the/var/log directory

#命令find Find Files WC statistics file

Find/var/log–type F | Wc–l

Method Two:

[Email protected] yuekao]# tree/var/log/| Tail-n 1

6 Directories, files

1.16 How to package all directories containing Hao in the/data directory (directories with directories of possible directories and Hao directories). 1.17 Requirements: Unpack the directory structure after packaging can not be changed. Old boy Linux operation and maintenance combat education

Command: Tar compressed file

Tar zcf/1.tar.gz $ (find/date–type d–name "*hao*")

1.18 list the common packaging tools under Linux and write out the appropriate compression decompression parameters.

Packaging Tool Tar

Z Compress in gzip format

C Create

V Display creation process

F Specify the destination file (where to compress)

T View compressed Package

X decompression

–C specifying the decompression path

--exclude= exclude (Connect files)

Relative path--exclude-from= (connect file)

Relative path

1.19 How do I see if I turn on port 80 and see if the sshd process exists?

Netstat network Port SS Port Query PS View process

ss–lnutp| Grep...

netstat–lnutp| Grep...

Ps–ef | Grep...

1.20 the modified time in the/hao directory is 7 days old, and files larger than 100k are moved to the/tmp directory.

Command: Find file find common parameters

MV $ (find/hao–mtime +7–size +100k)/tmp

Find/hao–mtime +7–size +100k-exec MV {}/tmp; \

Find/hao–mtime +7–size +100k | Xargs MV-T/tmp

Find/hao–mtime +7–size +100k | XARGS-I/tmp

1.21 find files that are larger than 50k and less than 100k in the system and delete them.

Command: RM Delete file or directory Find file

Rm–f $ (find/-type f–size +50k–a-100k)

Find/-type f–size +50k–a-100k | Xargs RM-RF

1.22 Simple description of Linux boot process

Briefly

1 MBR Post

2 reading the MBR boot program

3 Enter Glub Menu

4 Loading the kernel

5 Running the INIT process

6 reading the/etc/inittab configuration file

7 Execute/etc/rc.d/rc.sysinit Script

8 Execute/ETC/RC.D/RC (n). D Script

9 Running the Mingetty process

1.23 If you write data to disk, the following error is indicated: No space left on device, through Df-h view disk space, found not full, may I ask what is the reason?

The Inode is full.

1.24 Execute the command to take out the IP address of the eth0 in Linux (at least 3 ways)

Command: Ifconfig View NIC Information Etho is the first NIC

ifconfig eth0 |awk–f ' [:]+ ' nr==2 {print $4} '

Ifconfig eth0 |sed–nr ' 2S#^.*DR: (. *) BC.*#\1#GP '

Ifconfig eth0 |sed–nr ' 2s# (^.*dr:|bc.*$) # #gp '

1.25 I would like to display the date in the following format, please give the specific command separately.

Command: Date view time

2017-02-26

Date +%f

Saturday Output is 6

Date +%w

1.26 Add a scheduled task every night 12 o'clock, back up the/etc/hosts/etc/services file to the/data directory

Build a test environment

Timed Task Service Crond command crontab log/var/log/cron

Local Test command

Write script

Test scripts

Write timed Task Crontab-e

Check Scheduled Tasks

Modify timed Task time

Mkdir/data

Cat >/1.sh<<zgy

CD/&&/bin/tar zcf/data/$ (date+%f-%h) etc/hosts/&&/bin/tarzcf/data/$ (date +%f-%h-%m) etc/services

Zgy

Crontab-e

XX * * * */bin/sh/1.sh &>/dev/null

ESC Save exit

This article is from "Ah Kai" blog, please be sure to keep this source http://kaile.blog.51cto.com/12459560/1920625

Getting Started with Linux first monthly exam

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.