11. head and tail commands
head command: used to display the content at the beginning of the file, and the first 10 lines of the file are displayed by default
-
Command format: head [option] file
-
Common operations:
-
-
<number of lines> specifies the number of lines displayed
-
[root@localhost ~]# head /etc/passwd [root@localhost ~]# head /etc/fstab [root@localhost ~]# head /etc/group [root@localhost ~]# head /etc/hostname [root@localhost ~]# head /etc/hosts [root@localhost ~]# head /etc/sysconfig/network-scripts/ifcfg-ens32 #View stored DNS configuration file information [root@localhost ~]# head /etc/resolv.conf #Use -n to specify how many lines to display before the file [root@localhost ~]# head -n 5 /etc/passwd [root@localhost ~]# head -n 6 /etc/passwd [root@localhost ~]# head -n 15 /etc/passwd [root@localhost ~]# head -n 20 /etc/passwd
-
tail command: used to display the content at the end of the file, by default it displays the content of 10 lines at the end of the file
-
Command format: tail [option] parameter
-
Common operations:
-
-
<number of lines> display the specified number of lines -
Common options:
-
-f dynamic display
-
[root@localhost ~]# tail /etc/passwd #Use "-n" to specify how many lines to display at the end of the file [root@localhost ~]# tail -n 5 /etc/passwd [root@localhost ~]# tail -n 5 /etc/sysconfig/network-scripts/ifcfg-ens32 IPADDR="192.168.0.50" PREFIX="24" GATEWAY="192.168.0.254" DNS1="114.114.114.114" IPV6_PRIVACY="no" #Dynamic view file content [root@localhost ~]# touch t1 root@localhost ~]# tail -f t1 #Open another terminal to write content to the file [root@localhost ~]# echo 123 > t1
12. rm delete command
The rm (English spelling: remove) command is used to delete files or directories.
-
Command format: rm [-option...] directory or file...
-
Common options
-
-f force delete
-
-r delete directory
-
*
Special characters: Commonly used symbols in the system, used to represent any and all characters
-
[root@localhost ~]# ls /opt abc abc1 abc2 abc3 anaconda-ks.cfg hello.txt home rh student t1 t2 t3 t4 xx xxoo [root@localhost ~]# ls /mnt hello home oooo student1 t1 t2 t3 t4 test test1 test2 test3 #Delete the files in the specified directory [root@localhost ~]# rm /opt/anaconda-ks.cfg rm: delete normal file "/opt/anaconda-ks.cfg"? y #Confirmation is required by default (y|n) # Check if the file was successfully deleted [root@localhost ~]# ls /opt abc abc1 abc2 abc3 hello.txt home rh student t1 t2 t3 t4 xx xxoo [root@localhost ~]# rm /opt/hello.txt rm: delete the plain empty file "/opt/hello.txt"? the y # Delete the specified files in the directory at the same time [root@localhost ~]# rm /opt/t1 /opt/t2 /opt/t3 /opt/t4 rm: delete plain empty file "/opt/t1"? the y rm: delete plain empty file "/opt/t2"? the y rm: delete plain empty file "/opt/t3"? the y rm: delete plain empty file "/opt/t4"? the y # Check if the file was successfully deleted [root@localhost ~]# ls /opt abc abc1 abc2 abc3 home rh student xx xxoo #Use "-f" to force delete the file (no need to confirm, delete directly) [root@localhost ~]# rm -f /mnt/hello [root@localhost ~]# ls /mnt home oooo student1 t1 t2 t3 t4 test test1 test2 test3 #Forcibly delete multiple files at the same time [root@localhost ~]# rm -f /mnt/t1 /mnt/t2 /mnt/t3 /mnt/t4 [root@localhost ~]# ls /mnt #delete directory [root@localhost ~]# rm -r /opt/abc rm: Do you want to delete the directory "/opt/abc"? the y [root@localhost ~]# ls /opt abc1 abc2 abc3 home rh student xx xxoo # Delete multiple directories at the same time [root@localhost ~]# rm -r /opt/abc1 /opt/abc2 /opt/abc3 rm: Do you want to delete the directory "/opt/abc1"? the y rm: Do you want to delete the directory "/opt/abc2"? the y rm: Do you want to delete the directory "/opt/abc3"? the y [root@localhost ~]# ls /opt home rh student xx xxoo #Forcibly delete multiple directories at the same time [root@localhost ~]# rm -rf /opt/home /opt/student /opt/xx /opt/xxoo [root@localhost ~]# ls /opt rh # create directories and files [root@localhost ~]# touch /opt/t1 [root@localhost ~]# mkdir /opt/test [root@localhost ~]# ls /opt rh t1 test When the #rm command deletes a directory, all data including the changed directory and the directory are deleted [root@localhost ~]# rm -rf /opt/ [root@localhost ~]# ls / [root@localhost ~]# ls /mnt home oooo student1 test test1 test2 test3 #Use "*" to wildcard all characters and delete all data in the /mnt directory [root@localhost ~]# rm -rf /mnt/* [root@localhost ~]# ls /mnt
13. Soft link and hard link
Linked files in Linux are similar to shortcuts in windows
Soft link features: cross-partition, can link to the directory, after the source file is deleted, the linked file is unavailable
-
Soft link command format: ln -s source file path target path
Note: When creating a link file, you must write an absolute path, even if it is in the current path, you must also write an absolute path.
[root@localhost ~]# touch hello.soft [root@localhost ~]# ls #Create a soft connection (absolute path must be created) [root@localhost ~]# ln -s /root/hello.soft /opt [root@localhost ~]# ls /opt #View the detailed properties of the connection file [root@localhost ~]# ls -l /opt/hello.soft lrwxrwxrwx. 1 root root 16 3月 21 14:28 /opt/hello.soft -> /root/hello.soft #Tip: The permissions of the linked file ultimately depend on the permissions of the source file # Ordinary user authentication [lisi@localhost ~]$ ls /opt hello.soft [lisi@localhost ~]$ ls -l /opt/hello.soft lrwxrwxrwx. 1 root root 16 3月 21 14:28 /opt/hello.soft -> /root/hello.soft [lisi@localhost ~]$ cat /opt/hello.soft cat: /opt/hello.soft: Insufficient permissions #Tips: Since the source files are stored in the /root directory, and ordinary users do not have any permissions to the /root directory, ordinary users cannot view them #delete source file [root@localhost ~]# rm -f /root/hello.soft [root@localhost ~]# ls #After the mountain thick source file, the soft link file is not available [root@localhost ~]# ls -l /opt/hello.soft lrwxrwxrwx. 1 root root 16 3月 21 14:28 /opt/hello.soft -> /root/hello.soft #Create a file and create a soft link [root@localhost ~]# touch hello.soft [root@localhost ~]# ln -s /root/hello.soft /opt [root@localhost ~]# ls -l /opt/hello.soft lrwxrwxrwx. 1 root root 16 3月 21 14:39 /opt/hello.soft -> /root/hello.soft # After deleting the link file, the source file is still available [root@localhost ~]# rm -f /opt/hello.soft [root@localhost ~]# ls [root@localhost ~]# cat hello.soft #Create a soft link to the directory [root@localhost ~]# ln -s /root/test1 /opt/ [root@localhost ~]# ls -ld /opt/test1 lrwxrwxrwx. 1 root root 11 3月 21 14:44 /opt/test1 -> /root/test1 3 When creating a link, you must write the absolute path of the directory or file, even if it is in the current path, you must also write the absolute path [root@localhost ~]# ln -s hello.soft /opt [root@localhost ~]# ls /opt hello.soft test1 [root@localhost ~]# ls -l /opt/hello.soft lrwxrwxrwx. 1 root root 10 3月 21 14:47 /opt/hello.soft -> hello.soft
Hard link features: Hard links cannot span partitions, and cannot link directories. After the source file is deleted, the linked file is still available (similar to making a backup of the file)
-
Hard link command format: ln source file path target path
#Create a file and create a hard link [root@localhost ~]# touch hello.hard [root@localhost ~]# ln /root/hello.hard /opt/ [root@localhost ~]# ls /opt hello.hard hello.soft test1 #Write content to the source file of the hard link root@localhost ~]# echo 123 > /root/hello.hard # View source file content [root@localhost ~]# cat /root/hello.hard 123 #View the content of the linked file to update it synchronously [root@localhost ~]# cat /opt/hello.hard 123 #Write content to the link file, view the source file to update synchronously [root@localhost ~]# echo xx >> /opt/hello.hard # Wipe the source file to update it synchronously [root@localhost ~]# cat /root/hello.hard 123 xx #The characteristics of the hard link file can keep the file attributes unchanged [root@localhost ~]# ls -l /root/hello.hard -rw-r--r--. 2 root root 7 Mar 21 14:55 /root/hello.hard [root@localhost ~]# ls -l /opt/hello.hard -rw-r--r--. 2 root root 7 Mar 21 14:55 /opt/hello.hard #And the inode number of the hard link file is the same [root@localhost ~]# ls -i /root/hello.hard 33711090 /root/hello.hard [root@localhost ~]# ls -i /opt/hello.hard 33711090 /opt/hello.hard # Hard links do not allow links to directories root@localhost ~]# ln /root/test1 /opt ln: "/root/test1": hard link to directory not allowed #After the hard link source file is deleted, the link file is still available [root@localhost ~]# rm -f /root/hello.hard [root@localhost ~]# cat /opt/hello.hard 123 xx #Write content to the hard link file [root@localhost ~]# echo abc >> /opt/hello.hard [root@localhost ~]# cat /opt/hello.hard 123 xx abc #Hard link does not allow cross-partition [root@localhost ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 20G 0 disk ├─sda1 8:1 0 1G 0 part /boot └─sda2 8:2 0 19G 0 part ├─centos-root 253:0 0 17G 0 lvm / └─centos-swap 253:1 0 2G 0 lvm [SWAP] sr0 11:0 1 4.3G 0 rom [root@localhost ~]# ln /root/hello.soft /boot ln: unable to create hard link "/boot/hello.soft" => "/root/hello.soft": invalid cross-device connection