Exercises :

1, Show current time , Format is :2016-06-18 10:20:30

answer :date “+%F,%T” perhaps date “+%F %H:%M:%S”

 

2, Show the day of the week before yesterday ?

answer :date -d “-2 day” +%A

Knowledge points : One ,date +%s Is to convert the current time into seconds         Two , date -d @”1523604170″ Turn the seconds back

 

3, today 18:30 Automatic shutdown , And prompt the user .

answer :hutdown -h 18:30 “dao dian guan ji,18:30” If you want to cancel this operation input : shutdown -c

 

4, When logging in the local character terminal , In addition to displaying the original information , Display the current login terminal number , Host name and current time .

answer :vim /etc/profile.d/kaiji.sh
Enter after entering :#********************************************************************
echo your hostname is hostname
who am i

 

5, display /var All l start , Ends with a lowercase letter , A file or directory with at least one digit in the middle .

answer :ls /var/l*[0-9]*[[:lower:]]

 

6, display /etc Directory starts with any number , Files or directories with non numeric endings .

answer :ls /etc/[0-9]*[^0-9]

 

7, display /etc/ Directory starts with a non letter , A file or directory followed by a letter and any other characters of any length .

answer :ls /etc/[^[:alpha:]][a-zA-Z]*

 

8, display /etc/ All rc start , And behind it 0-6 Number between , Other files or directories with any characters .

answer :ls /etc/rc[0-6]*

 

9, display /etc Directory , All .d End of file or directory .

answer :ls /etc/*.d

 

10, display /etc Directory , All .conf ending , And m,n,r,p File or directory at the beginning .

answer :ls /etc/[m,n,r,p]*.conf

 

11, Show only /root Hidden files and directories under . Show only /etc Non hidden directory under

answer :ls -d /root/.* ls /etc/[^.]*/ -d

 

12, Define alias command baketc, Every day will /etc/ All files in directory , Backup to /app Independent subdirectories , The format of subdirectory is backupYYYY-mm-dd Backup process visible .

answer :alias baketc=”cp -av /etc /data/backupdate +%F”

 

13, establish /app/rootdir catalog , And copy /root All files in this directory , Ask to keep the original permission .

answer :mkdir -p /app/rootdir cp -a /root /app/rootdir/

 

14, How to create /testdir/dir1/x,/testdir/dir/y,/testdir/dir/x/a,/testdir/dir/x/b,/testdir/dir/y/a,/testdir/dir/y/b.

answer :mkdir -p /testdir/dir1/{x,y}/{a,b}

 

15, How to create /testdir/dir2/x,/testdir/dir2/y,/testdir/dir2/x/a,/testdir/dir2/x/b.

answer :mkdir -p /testdir/dir2/{x/{a,b},y}

 

16, How to create /testdir/dir3,/testdir/dir4,/testdir/dir5,/testdir/dir5/dir6,/testdir/dir5/dir7.

answer :mkdir -p /testdir/{dir3,dir4,dir5/{dir6,dir7}}

 

17, take /etc/issue The contents of the file are converted to uppercase and saved to /tmp/issue.out In file .

answer :cat /etc/issue | tr “[a-z]” “[A-Z]” > /tmp/issue.out

 

18, Convert the information of current system login user to uppercase and save it to /tmp/who.out In file .

answer :who | tr “[a-z]” “[A-Z]” >/tmp/who.out

 

19, One linux Users to root send emails , Require message title to be ” help”, The body of the message is as follows :Heello,i am user name ,The system version
is here ,please help me to check it,thanks! Operating system version information

answer :mail -s “help” root <<123
>Hello,I am $USER
>The system version is here,please help me to check it,thanks!
>cat /etc/centos-release
>123

 

20, take /root/ Next file list , Display in one line , And file names are separated by spaces .

answer :ls /root | tr “\n” ” ”

 

21, calculation 1+2+3+..+99+100 Sum of .

answer :echo {1..100}|tr ” ” “+”|bc

 

22, delete Windows In a text file ^M character

answer :tr -d “\15” win.txt

 

23, Processing strings “xt.l 1 jr#!$mn 2 c*/fe 3 uz 4” , Keep only numbers and spaces .

answer :echo “xt.l 1 jr#hostnamemn 2 c*/fe 3 uz 4” |tr -dc “[:digit:][:space:]”

 

24, take PATH Variable each directory is displayed on a separate line .

answer :echo $PATH |tr “:” “\n”

 

25, Will specify 0-9 Replace with a-j .

answer : Create file first touch f1 to f1 vim input 0-9 cat f1 | tr “[0-9]” “[a-j]”

 

26, File /etc/centos-release Each word in ( Composed of letters ) Display on a separate line , No blank lines .

answer :cat /etc/centos-release |tr -c “[:alpha:]” ” ” |tr -s ” ” “\n”

 

27, Create user gentoo, Additional groups are bin and root, default shell by /bin/csh, Note information is ”Gentoo Distribution”.

answer :useradd -G bin,root -s /bin/csh -c “Gentoo Distribution” gentoo

 

28, Create the following users , Groups and group membership
The name is webs Group of user nginx use webs As additional group user varnish, Also use webs As additional group
user mysql, Non interactive login system , And not webs Members of ,nbinx,varnish,mysql The codes are all magedu

answer : groupadd webs       useradd -G webs nginx        useradd -G webs varnish  
      useradd -s /sbin/nologin masql

echo magedu |passwd –stdin nginx;
echo magedu |passwd –stdin varnish;
echo magedu |passwd –stdin mysql;

 

29, When the user docker Yes /testdir When the directory does not have execute permission , What does it mean you can't do ?

answer : No cd go in , Unable to view file details properties , You can't access the contents of the files in the directory ( Even if you have read permission ).

 

30, When the user mongodb Yes /testdir When the directory has no read permission , What does it mean you can't do ?

answer : Cannot access files in directory .

 

31, When the user redis Yes /testdir When the directory has no write permission , Read only files in this directory file1 Whether it can be modified and deleted ?

answer : No , Because I don't have permission to the directory , So we can't . Can the file be deleted , Not determined by documents , It's up to the catalog .

 

32, When the user zabbix Yes /testdir When the directory has write and execute permissions , Read only files in this directory file1 Whether it can be modified and deleted ?

answer : Can be modified and deleted

 

33, copy /etc/fstab File to /var/tmp lower , Set file owner to tomcat read-write permission , The group is apps Group has read and write permission , Others do not have permission .

answer :( One )cp -a /etc/fstab /var/tmp ( Two ) useradd tomcat ( Three ) groupadd apps ( Four ) chown
tomcat /var/tmp ( Five ) chgrp apps /var/tmp ( Six ) chmod 660 /var/tmp

 

34, User deleted by mistake git Home directory of , Please rebuild and restore the user's home directory and corresponding permission properties .

answer :           rm -rf /home/git ;      mkdir /home/git;          cp -a
/etc/skel/.[^.]* /home/git;                                                    
    chown -R git:git  /home/git;

35, stay /testdir/dir
The new file created in automatically belongs to webs group , group apps Members of :tomcat Can read and write to these new files , group dbs Members of :mysql Only new files can be read , Other users ( Not of webs,apps,dbs) Unable to access folder .

answer :                     mkdir -p /testdir/dir                                
                                                                               
                                      chgrp webs /testdir/dir                  
                                                                               
                                               chmod g=s /testdir/dir
setfacl -m g:apps:rw /testdir/dir                                            
                                                                               
        setfacl -m g:dbs:r /testdir/dir

chmod o= /testdir/dir

 

36, backups /testdir/dir All the documents in ACL Permission to /root/acl.txt in , eliminate /testdir/dir All in ACL jurisdiction , Last restore ACL jurisdiction .

answer :                     getfacl -R /testdir/dir > /root/acl.txt              
                                                                               
                           setfacl -b /testdir/dir

setfacl -R –set-file=acl.txt /testdir/dir

 

37, find ifconfig “ adapter name ” Local in command result IPv4 address .

answer :( Method 1 )ifconfig ens33 | grep netmask | tr -s ” ” “:” |cut -d: -f3

( Method 2 )ifconfig ens33 |egrep -o
\<“(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])”\>

( Method 3 )ifconfig ens33 | sed -n “2p” | sed -r s’@(.*inet)(.*)( netmask.*)@\2@’

 

38, Find out the maximum percentage of the utilization rate of the partition space .

answer :( Method 1 )df | grep ^/dev | tr -s ” ” “:” | cut -d: -f5 |cut -d% -f1 | sort -nr
| head -n1

( Method 2 ) df | grep -o “[0-9]\{1,3\}%” |grep -o “[0-9]\+” |sort -nr |head -n1

 

39, Find out users UID Maximum worth user name ,UID and shell type .

answer :cat  /etc/passwd |sort -nr -t: -k3 |head -n1 |cut -d: -f1,3,7

 

40, Find out /tmp Permissions for , Digital display

answer :stat  /tmp |head -n4|tail -n1|cut -d/ -f1|cut -d'(‘ -f2

 

41, Count each remote host currently connected to this machine IP Number of connections for , And sort from large to small .

answer : Get it from the desktop first rz get files , Reprocessing .                                                      
                                                           cat access_log
|egrep -o
“\<(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>”|sort|uniq
-dc|sort -nr

 

 

42, display /proc/meminfo Size in file s Line at the beginning ( requirement : Use two methods )

answer :( Method 1 )cat /proc/meminfo |egrep -oi ^s.*

( Method 2 )cat /proc/meminfo |egrep ^[Ss].*

 

43, display /etc/passwd Not in file /bin/bash Line at end .

answer :cat  /etc/passwd |egrep -v /bin/bash$

 

44, show user rpc default shell program .

answer :( Method 1 )cat  /etc/passwd |egrep rpc|cut -d: -f1,7

( Method 2 )cat  /etc/passwd |egrep rpc|sed -r ‘s/(.*:)([^:]+:?$)/\2/’

 

45, find /etc/passwd Two or three digits in

answer :cat  /etc/passwd | egrep -o “[0-9]{2,3}”

 

46, display Centos7 Of /etc/grub2.cfg In file , A line that starts with at least one white space character and is followed by a non white space character .

answer :cat  /etc/grub2.cfg |egrep ^[[:space:]][^[:space:]].*$

 

47, find ”netstat -tan” In the command result LISTEN Line followed by any number of white space characters .

answer :netstat -tan |egrep .*LISTEN[[:space:]]+

 

48, display Centos7 User names and UID.

answer :cat  /etc/passwd |egrep .*/sbin/nologin$ |cut -d: -f1,3

 

49, Add user bash,testbash,basher,sh,nologin( his shell by /sbin/nologin), find /etc/passwd User name and shell Row with the same name .

answer :cat  /etc/passwd | egrep “^(.*)(:.*)\1$”

 

50, utilize df and grep, Go to disk partition utilization , And sort from large to small .

answer :df |grep ^/dev |tr -s ” ” “:”|cut -d: -f5 |cut -d% -f1 |sort -nr|head -n1

 

51, Show three users root,mage,wang Of UID And default shell.

answer :cat /etc/passwd |egrep ^”(root|mage|wang)” |cut -d: -f1,3,7

 

52, find /etc/rc.d/init.d/functions The first line in the file is a word ( Include underline ) A line followed by a parenthesis .

answer :cat /etc/rc.d/init.d/functions | egrep “^[a-zA-Z_]+\(\).*”

 

53, use egrep take out /etc/rc.d/init.d/functions Base name of .

answer :echo /etc/rc.d/init.d/functions |egrep -o “[^/]*/?$”

 

54, use egrep Take it out Directory name of the path .

answer :( Method 1 )echo /etc/rc.d/init.d/functions |egrep -o “/.*/”

( Method 2 )echo /etc/rc.d/init.d/functions |egrep -o “(/).*\1”

 

55, Statistics last In the command root Every host logged in IP Number of address logins .

answer :                  last |egrep root |egrep
“\<(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>”|tr
-s ” ” “:”|sort -t: -k3|cut -d: -f3|uniq -dc

 

56, Using extended regular expressions to express 0-9,10-99,100-199,200-249,250-255.

answer :              [0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]

 

57, display ifconfig All in the command result IPV4 address .

answer :               ifconfig | egrep -o
“\<(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>”

 

58, Put this string :welcome to magedu linux De duplicate and sort each character in , Put those with more repetitions at the top .

answer :         echo “welcometomagedulinux” |grep -o “.”|sort|uniq -c|sort -nr

 

59, copy /etc/profile to /tmp/ catalog , Delete with find replace command /tmp/profile Whitespace at the beginning of a line in a file .

answer :                                 cp /etc/profile /tmp/                    
                                                                               
                                                       vim /tmp/profile        
                                                                               
                                                      Press in command mode “:” Enter extended mode input
%s/^[[:space:]]*//g

 

60,
copy /etc/rc.d/init.d/functions File to /tmp catalog , Replace the command with find /tmp/functions One at the beginning of each line of # Number .

answer :                                    cp /etc/rc.d/init.d/functions /tmp    
                                                                               
                                            vim /tmp/functions                
                                                                               
                                                Press in command mode “:” Enter extended mode input        
%s/^[[:space:]] */#&/                                                          
                                                                               
  perhaps           %s/\(^[[:space:]]\+.*\)/#\1/g

 

61, stay VIM Setting in tab Indent to 4 Characters .

answer :                vim /etc/vimrc
Add at the end of the file :
set ts=4
set expandtab
set autoindent
:wq

 

62, copy /etc/rc.d/init.d/functions File to /tmp catalog , replace /tmp/functions In the file /etc/sysconfig/init by /var/log.

answer :                                            cp /etc/rc.d/init.d/functions
/tmp                                                                          
                                                   vim /tmp/functions          
                                                                               
                            Press in command mode “:” Enter extended mode input              
%s@\/etc\/sysconfig\/init@\/var\/log@

 

63, delete /tmp/functions All # start , And # At the beginning of a line followed by at least one white space character # Number .

answer :                              vim /tmp/functions                          
                                                                               
                                            Press in command mode “:” Enter extended mode input          %s@^#\(”
“\+.*\)@\1@

 

64,
Script /root/bin/systeminfo.sh, Display current host system information , Include host name ,IPV4, Operating system version , Kernel version ,CPU model , Memory size , Hard disk size .

answer : vim /root/bin/systeminfo.sh
#********************************************************************
echo hostname
echo ifconfig ens33 | egrep -o
"\<(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]
)\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>"|head -n1
echo cat /etc/centos-release
echo uname -r
echo lscpu |grep "^Model name.*" |cut -d: -f2|tr -s " "
echo cat /proc/meminfo |head -n1
echo lsblk |grep '^sda'|tr -s " " "%"|cut -d% -f4
:wq

 

65, Script /root/bin/backup.sh, Can realize daily /etc/ Catalog backup to /root/etcYYY-mm-dd in .

vim /root/bin/backup.sh                     cp -a /etc /root/etcdate +%F      
   :wq

 

66, Script /root/bin/disk.sh, Displays the maximum space utilization in the current hard disk partition .

answer :       e=df|egrep ^/dev |tr -s " " ":"|cut -d: -f5|cut -d% -f1|sort
-nr|head -n1
echo $e                                                                      
                                                                               
                      :wq

 

67, Script /root/bin/links.sh , Displays the IPV4 Address and number of connections , And sort by the number of connections .

      answer :                  vim /root/bin/linsk.sh                            
                                                                               
                                        a=cat access_log |egrep -o
"\<(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>"|sort|uniq
-c|sort -nr                                                                    
      echo $a

68, Script /root/bin/sumid.sh , calculation /etc/passwd No 10 Users and 20 User's ID Sum of .

answer :              vim /root/bin/sumid.sh                                      
                                                                               
                          a=cat /etc/passwd | head -n10 |tail -n1|cut -d: -f3
b=cat /etc/passwd | head -n20 |tail -n1|cut -d: -f3
let c=a+b or d=$[ a+b ]                                                        
                                                                               
         echo $d

 

69, Script /root/bin/sumspace.sh , Pass two file paths as parameters to the script , Calculate the sum of all blank lines in the two files .

answer :                                 vim /root/bin/sumspace.sh                
                                                                               
                                        a=cat f1 |egrep ^[[:space:]]*$ |wc -l  
                                                                               
                                      b=cat f2 |egrep ^[[:space:]]*$ |wc -l

let c=a+b

 

70, Script /root/bin/sumfile.sh , Statistics /etc ,/var,/usr How many level 1 subdirectories and files are there in the directory .

answer :vim /root/bin/sumfile.sh                       a=ls /etc/ |wc -l          
                                                                               
                                                                b=ls /var/ |wc
-l c=ls /usr/ |wc -l
let d=a+b+c

 

71, Script /root/bin/argsnum.sh , Accept a file path as a parameter ; If the number of parameters is less than 1, Prompt the user
“ At least one parameter should be given ”, And quit now ; If the number of parameters is not less than 1, The number of blank lines in the file pointed to by the first parameter is displayed .

answer :         vim /root/bin/argsnum.sh                                          
                                                                               
                  [ $# -lt 1] &&  echo “At least one parameter should be given”
&& exit
[ $# -ge 1] &&  echo egrep "^[[:space:]]*$"  $1|wc -l

 

73, Script /root/bin/hostping.sh , Accept a host's IPV4 Address as parameter , Test connectivity . If you can ping through , Prompt the user
“ The IP Address accessible ” ; If not ping through , Prompt the user “ The IP Address not accessible ”.

answer :                vim /root/bin/hostping.sh                                  
                                                                               
                      [[ $1 =~
“\<(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]]])\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>”
]] || echo { “IP error”;exit; }                                                
                                   ping $1 && echo “This address can be
accessed”|| echo “This address cannot be accessed”

74, Script /root/bin/checkdisk.sh , Check partition space and inode Utilization rate , If more than 80%, Broadcast a warning that the space will be full .

answer :                    vim /root/bin/checkdisk.sh                            
                                                                               
                          a=df |egrep ^/dev |tr -s " " ":" |cut -d: -f5 |cut
-d% -f1|sort -nr|head -n1
[[ $a -ge 80 ]] && echo “zhao huo la ” || echo { “yi qie zheng chang”;exit; }

 

75, Script /bin/per.sh , Judge whether the current user has specified parameter file , Is it unreadable and not writable .

answer :           [ -not -r $1 -a -not -w $1 ] && echo “bu ke du ”
[ \( -r $1 -o -w $1 \) ] || echo “ke du ”

 

 

76, Script /root/bin/excute.sh , Judge whether the parameter file is sh Generic file with suffix , If it is , Add everyone executable rights , Otherwise, prompt the user for non script files .

answer :            vim /root/bin/excute.sh                                        
                                                                               
                          [[ $1 =~ .*sh$ ]] && chmod +x $1 || echo “bu shi jiao
ben wen jian “

Technology
©2019-2020 Toolsou All rights reserved,
First knowledge python Skills summary GDOI2019 travels c Linguistic 5 Three common sorting methods Python Basic knowledge and notes " Cephalosporin wine Say go and go "? DANGER ! Don't drink alcohol when taking these drugs Thorough explanation from Zhongtai vue The component page height is adaptive according to the screen size Classical algorithm - recursion ( The case of raw rabbit ) Big data tells you , How tired are Chinese women about keras use fit_generator Encountered in StopIteration