Linux experiment - character interface command operation exercise

Keywords: Linux CentOS

1, Experimental purpose

(1) Familiar with terminal and shell   Command format

(2) Master the method of getting help and the use of common commands

2, Experimental content

be careful:

The experimental operations are carried out in the normal user mode, and the super user root is not used; Notice that there are spaces between each line of code

As a separator, do not connect all the commands in one line, otherwise it cannot be executed.

1. Create a directory tree in your home directory according to the structure shown in the figure below, and use relevant commands to verify the experimental results.

#Each line is a command. After typing a line of code, press enter to execute
mkdir ~/{myproj,mybook,work}
mkdir ~/mybook/{python,cpp}
mkdir ~/myproj/calc/{src,bin,doc}
mkdir ~/myproj/calc/src/{c,include,lib}
#Use the tree command to view the directory tree. The tree needs to be installed by itself in advance
tree -d -L 4 ~

  2. Get the introduction of passwd or its distribution in man help, find the location of passwd binary file, then switch the current working directory to ~ / work subdirectory, use man command to obtain the help information of passwd file and passwd command, and save it in passwd.txt file. Count the number of block devices and character devices in the / dev directory and display them on the screen.

whereis -b passwd
#Find the location of the passwd binary
cd work
#Switch to work directory
man passwd > passwd.txt
#Overwrite the information obtained from man passwd into the newly created passwd.txt file in the work directory
cd ~
#Switch to home directory
ls /dev |grep 'b' |wc -l
#List the file directory information under the / dev directory, use grep 'b' to find the block device, and then count the number of lines by wc -l
ls /dev | grep 'c' |wc -l
#Similarly, grep 'c' is the device for finding characters

 

3. Display date and time information on the screen in the following format:

Current date:   Current time:   XX:XX:XX

According to the passage of XXXXXXXX seconds at 00:00:00 on January 1, 1970

Today is the XXX day of the year, in the XX week

date +current date:%Y year%m month%d day%t current time :%H:%M:%S%n According to 1970.1.1 Day 00:00:00 time lapse%s second%n Today is the third day of the year%j God, on the second day%U week
#This command is a single line. When writing the command, pay attention to the case of the letters after%

4. Create empty directories myuser, mydata and myDoc in the ~ / work subdirectory, copy the passwd.txt file generated above to the myDoc directory, and then delete the original file.

mkdir ~/work/{myuser,mydata,myDoc}
#Create three directories at the same time
cp ~/work/passwd.txt  ~/work/myDoc
#Pay attention to the source file and destination path when copying the cp command
rm ~/work/passwd.txt
#The rm command deletes the file
ls ~/work
#Check whether passwd.txt still exists in the work directory
ls ~/work/myDoc
#Check whether there is a file passwd.txt under myDoc

  5. Obtain the list of user accounts currently logged in to the system, filter out their own account records, write to the file st.lst, and display the output on the screen at the same time.

who | grep 'xxxxx' | tee st.lst
#The who command finds the login user through the pipeline
#Search users with grep, 'xxxx' is the user to be searched
#Tee tee, print the information on the screen and enter it into the file at the same time

  6. Select the network card configuration ip address output by ifconfig command and output it to standard output. Output the ipv4 address list of all network interfaces configured locally to the ipdata file in the mydata directory, count the number of network interfaces and append them to the ipdata file.

ifconfig | grep inet | grep -v inet6 | awk '{print $2}' | tee ~/work/mydata/ipdata |wc -l >> ~/work/mydata/ipdata
#I'd like to call it the art of code, where multiple commands are piped into one line of code
#awk, SED and grep are known as the "three swordsmen" of Linux. Search for the specific usage by yourself
cat ~/work/mydata/ipdata
#The cat command checks whether the content of the ipdata file is the ip address you extracted

7. Create two empty files ex1.c and ex2.c in directory c of the above directory tree, and then input the following corresponding contents into the file and save it. Merge the contents of ex1.c and ex2.c   Is ex3.c.

 

 

 

touch ~/myproj/calc/src/c/{ex1.c,ex2.c}
#Create two new files
cd ./myproj/calc/src/c
#Switch to the path where the file is located for subsequent operations
vi ex1.c ex2.c
#Using vi text editor to operate on two files

  i open two windows for editing at the same time. Press' i 'to enter editing and esc to exit editing. How to use query vi editor.

cat ex1.c ex2.c > ex3.c
#Because the path has just been switched to the location of the file, cat the two files directly and output the contents to ex3.c. at this time, the new file is saved in the current directory
cat ex3.c
#View the contents of ex3.c file

  8. Move subdirectory c to ~ / work directory. View the directory tree structure of the user's home directory in the form of color display of different types of files.

mv ~/myproj/calc/src/c   ~/work
#Move the contents in the c directory to the work directory
tree -C ~
#Note that the upper case is C, which implements the directory tree listing the home directory in different colors

  9. Start from the user's home directory, find the file whose file name begins with ex, and copy it to the src subdirectory in the directory tree.

find -name "ex*" -exec cp {}  ~/myproj/calc/src/ \;
#The art of code has emerged again. One line of code solves problems
#The name condition expression - name and action expression - exec of find are used to solve the problem
ls ~/myproj/calc/src
#Check whether there is any content just copied in this directory

10. Package and compress the calc directory and its subdirectories and files in the directory tree into a calc.tar.gz file and save it in the backup subdirectory under work

mkdir  ~/work/backup
#Create an empty backup directory under work
tar -czvf ~/work/backup/calc.tar.gz -c ~/myproj calc
#Use the tar command to pack and compress the file. Note that the end of the packed file is. Tar and the packed compressed file is. tar.gz

3, Experimental summary

This experiment preliminarily understands the basic operation format of shell commands under Linux, how to use Linux terminal and make clever use of various commands, which can achieve very convenient operation effect. At the same time, I am familiar with the storage location of Linux directory files. Under Linux, any device exists in the form of files. I understand the storage rules of files, which is convenient for better computer management.

Posted by Rebelrebellious on Sun, 03 Oct 2021 15:35:21 -0700