Linux basic file operation exercise (with answer analysis)

1. Creation and deletion of files

(1) Practice using - p or -- parents to create multi-level directories on the desktop at the same time, such as xx/yy/zz.

mkdir - p} xx/yy/zz or mkdir --parents xx/yy/zz

[tom@iZbp12r8eimkkdor4011j3Z ~]$ mkdir -p xx/yy/zz
[tom@iZbp12r8eimkkdor4011j3Z ~]$ ls
#Note that if you want to create a multi-level directory with one command, you need to add the - p option

(2) Delete zz, yy and xx directories in sequence

rmdir xx/yy/zz
#Delete zz file
rmdir xx/yy

#Delete yy file
rmdir  xx
#Delete xx

(3) Recreate xx/yy/zz and delete these directories at the same time

#Method 1
rmdir -p xx/yy/zz
#Method 2
rm -r xx

2. Basic operation of files

Premise: create a directory named dir1 in the user's home directory, and use the touch command to create a file named myfile in the home directory (use cat > file name, enter text, and end with Ctrl+d)

[tom@iZbp12r8eimkkdor4011j3Z ~]$ touch myfile
#'>' output redirection to output the input content to a file
[tom@iZbp12r8eimkkdor4011j3Z ~]$ cat>myfile
this is the value of myfile
[tom@iZbp12r8eimkkdor4011j3Z ~]$ mkdir dir1
[tom@iZbp12r8eimkkdor4011j3Z ~]$ ls
dir1  myfile

(1) Use the cp command to copy the myfile file in the home directory to the dir1 directory

[tom@iZbp12r8eimkkdor4011j3Z ~]$ cp myfile dir1
[tom@iZbp12r8eimkkdor4011j3Z ~]$ ls
dir1  memos  myfile
[tom@iZbp12r8eimkkdor4011j3Z ~]$ cd dir1
[tom@iZbp12r8eimkkdor4011j3Z dir1]$ ls

(2) Change the name of myfile in dir1 directory to myfile old

[tom@iZbp12r8eimkkdor4011j3Z ~]$ cd dir1
[tom@iZbp12r8eimkkdor4011j3Z dir1]$ mv myfile myfile.old
[tom@iZbp12r8eimkkdor4011j3Z dir1]$ ls

(3) Move the file myfile in the home directory to the dir1 directory

[tom@iZbp12r8eimkkdor4011j3Z ~]$ cd 
[tom@iZbp12r8eimkkdor4011j3Z ~]$ ls
dir1  memos  myfile
[tom@iZbp12r8eimkkdor4011j3Z ~]$ mv myfile dir1
[tom@iZbp12r8eimkkdor4011j3Z ~]$ ls
dir1  memos
#The file was moved, so it no longer exists in the home directory

(4) Copy the files in dir1 directory to xx directory under the home directory (xx does not exist), and then view the contents of xx

[tom@iZbp12r8eimkkdor4011j3Z ~]$ cp -r dir1 xx
#If the xx directory does not exist, it will be created automatically
[tom@iZbp12r8eimkkdor4011j3Z ~]$ ls
dir1  memos  xx
[tom@iZbp12r8eimkkdor4011j3Z ~]$ cd xx
[tom@iZbp12r8eimkkdor4011j3Z xx]$ ls
myfile  myfile.old

(5) Create the yy directory under the home directory, recursively copy the dir1 directory into the yy directory, and then view the contents of yy

[tom@iZbp12r8eimkkdor4011j3Z ~]$ ls
dir1  xx
[tom@iZbp12r8eimkkdor4011j3Z ~]$ mkdir yy
[tom@iZbp12r8eimkkdor4011j3Z ~]$ cp -r dir1 yy
[tom@iZbp12r8eimkkdor4011j3Z ~]$ ls yy
#Eventually, it will be copied to yy's directory together with dir1

(6) Change the working directory to dir1 and list all files with the extension old

cd dir1
ls *.old

(7) Use the wc command to display the number of lines, words and characters in the myfile file

[tom@iZbp12r8eimkkdor4011j3Z ~]$ cd dir1
[tom@iZbp12r8eimkkdor4011j3Z dir1]$ wc myfile
 1  6 28 myfile
#Number of lines, words and characters

3. Create a directory named memos in the user's home directory, and create files file1, file2, file3, file12, 12file, fileab, afile, BFILE and abfile in the memos directory. Enter certain contents for each file.

[tom@iZbp12r8eimkkdor4011j3Z ~]$ mkdir memos 
[tom@iZbp12r8eimkkdor4011j3Z ~]$ cd memos
[tom@iZbp12r8eimkkdor4011j3Z memos]$ touch file1 file2 file3 file12 12file fileab afileb
 bfile afile abfile
[tom@iZbp12r8eimkkdor4011j3Z memos]$ ls
12file  abfile  afile  afileb  bfile  file1  file12  file2  file3  fileab

(1) Lists files in the memos directory whose file names begin with file

ls file*

(2) View the i node numbers of all files in the memos directory

[tom@iZbp12r8eimkkdor4011j3Z memos]$ ls -i
83393 12file  83430 afile   83429 bfile  83392 file12  83361 file3
83431 abfile  83404 afileb  83337 file1  83338 file2   83396 fileab

(3) Show file contents

1) Displays the contents of the file1 file

cat file1

2) display the contents of files file1, File2 and file3 at the same time

cat file1 file2 file3

3) Display the contents of file fileab with line number

cat -n fileab

(4) Delete file

1) Delete a file in the memos directory

rm file1

2) delete two files in the memo directory at the same time

rm file2 file3

3) use the - i option when deleting and experience the usage of the - i option

[tom@iZbp12r8eimkkdor4011j3Z memos]$ rm -i afile
rm: remove regular empty file 'afile'? y
#Enter y to confirm the deletion and n to cancel the deletion

4. Add / etc / Conf is copied to the yumfile file on the desktop of the admin user. Create a hard link file and a symbolic link file for the yumfile, and then view the index node number and file content of the linked file. Next, modify the source file, hard link file and symbolic link file to see the changes in the contents of the other two files. Then delete the source file and observe the changes of hard link file and symbolic link file.

[tom@iZbp12r8eimkkdor4011j3Z ~]$ cp /etc/yum.conf yumfile
[tom@iZbp12r8eimkkdor4011j3Z ~]$ ln yumfile ylinkfile  #Create hard link
[tom@iZbp12r8eimkkdor4011j3Z ~]$ ln -s yumfile softlinkfile #Create soft link (symbolic link)

[tom@iZbp12r8eimkkdor4011j3Z ~]$ ll
total 8
lrwxrwxrwx. 1 tom tom   7 Dec 24 23:24 softlinkfile -> yumfile     #Soft link files have arrow guidance
drwxrwxr-x. 2 tom tom  38 Dec 24 23:01 xx
-rw-r--r--. 2 tom tom 149 Dec 24 23:23 ylinkfile                   #The number of i nodes of hard link and source file is 149
-rw-r--r--. 2 tom tom 149 Dec 24 23:23 yumfile
drwxrwxr-x. 3 tom tom  18 Dec 24 23:05 yy

  • The size of the hard link file is the same as that of the source file, but the size of the soft link file is usually the number of bytes of the source file name
  • No matter hard link file or soft link file, the content is the same as the source file yumfile
  • Modify the hard link file or symbolic link file, and the other two files will also change
  • When the source file is deleted, the hard link file still exists and will not be affected, but the soft link file becomes a dead link and the file cannot be opened

5. Usage of tail head

#Example: if you want to print 11-20 lines
1.First take the first 20 lines of the file, and then take out the last 10 lines, which is 11-20 The content of the line
head -n 20 hello.txt | tail -n 10
 If you also want to list line numbers for verification:
cat -n hello.txt | head -n 20 | tail -n 10

6. File search

(1) Look for a file named passwd in / etc

find /etc -name passwd

(2) Find the file named myfile in the home directory and its subdirectories

[tom@iZbp12r8eimkkdor4011j3Z ~]$ find ~ -name myfile 

(3) Look in the home directory for all files and directories whose name contains "D"

[tom@iZbp12r8eimkkdor4011j3Z ~]$ find ~ -name "*D*"

(4) Find all directory files in the home directory

[tom@iZbp12r8eimkkdor4011j3Z ~]$ find ~ -type d

(5) Start at / home to find all files belonging to admin

find /home -user admin

7. File content search

(1) Find the mail user information from the / etc/passwd file

[tom@iZbp12r8eimkkdor4011j3Z ~]$ grep mail /etc/passwd

(2) Find all accounts with shells of / bin/bash from the / etc/passwd file

[tom@iZbp12r8eimkkdor4011j3Z ~]$ grep "bin/bash" /etc/passwd

(3) Find all accounts with UID or GID (third or fourth column) of 5 from the / etc/passwd file

[tom@iZbp12r8eimkkdor4011j3Z ~]$ grep :5: /etc/passwd

(4) Find all accounts with UID and GID between 70-79 from the / etc/passwd file

[tom@iZbp12r8eimkkdor4011j3Z ~]$ grep :7[0-9]:7[0-9]: /etc/passwd
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

(5) Show / etc / NFS All non comment lines in the conf file (comment lines start with #)

grep  -v  ^#  /etc/nfs.conf

(6) Show / etc / Lines in the conf file that begin with gpg

[tom@iZbp12r8eimkkdor4011j3Z ~]$ grep ^gpg /etc/yum.conf 

Keywords: Linux Operation & Maintenance server

Added by londonjustin on Fri, 24 Dec 2021 21:01:51 +0200