Art is long, life is long
Directory management
Absolute path and relative path
We know that Linux has a tree directory structure, with the top level being the root directory /.
Other directories can be mounted to add them to the tree, and unmounted to remove them.
Before starting this tutorial we need to know what absolute and relative paths are.
Absolute path:
The path is written from the root directory /, for example, /usr/share/doc.
Relative path:
/usr/share/man /usr/share/man /usr/share/man /man, that’s the relative path.
Common commands for working with directories
Let’s take a look at some common commands for handling directories:
- Ls: lists directories
- CD: Switch the directory
- PWD: Displays the current directory
- Mkdir: creates a new directory
- Rmdir: Delete an empty directory
- Cp: copies files or directories
- Rm: Deletes a file or directory
- Mv: moves a file or directory or changes the file or directory name
You can use man [command] to view the documentation for each command, e.g. Man cp.
Ls (lists directories)
On Linux, the ls command is probably the most commonly run.
Grammar:
[root@www ~]# ls [-aadffhilnrrst] Directory nameCopy the code
Options and parameters:
- – A: List all files, along with hidden files (files starting with.) (often used)
- -l: lists a long string of data, including file attributes and permissions. (common)
List all files in the directory (including properties and hidden files)
[root@www ~]# ls -al ~
Copy the code
CD (Change directory)
CD is short for Change Directory, which is the command used to Change working directories.
Grammar:
CD [relative path or absolute path]Copy the code
Testing:
#Switch to the user directory
[root@jiangwang /]# cd home
#Use the mkdir command to create the jiangwang directory
[root@jiangwang home]# mkdir jiangwang
#Enter the jiangwang directory
[root@jiangwang home]# cd jiangwang
#Go back one level
[root@jiangwang jiangwang]# cd ..
#Back to the root
[root@jiangwang jiangwang]# cd /
#To return to your home directory, which is /root
[root@jiangwang jiangwang]# cd ~
Copy the code
I think you’ll get a good idea of the CD command if you do it a few more times.
PWD (show current directory)
PWD is short for Print Working Directory.
[root@jiangwang jiangwang]#pwd [-P]
Copy the code
Options and parameters: -p: Displays the actual path instead of using the link path.
Testing:
#Simply display the current working directory
[root@jiangwang ~]# pwd
/root
#If it is a link, you can use the -p argument to display the real address
[root@jiangwang /]# cd bin
[root@jiangwang bin]# pwd -P
/usr/bin
Copy the code
Mkdir (create a new directory)
If you want to create a new directory, use mkdir (make directory).
Mkdir [-mp] Specifies the directory nameCopy the code
Options and parameters:
- -m: Configuration file permissions oh! Direct configuration, do not need to see the default permission (umask) face ~
- -p: helps you recursively create the required directory (including the upper level directory) directly.
Testing:
#Go to our user directory
[root@jiangwang /]# cd /home
#To create atestfolder
[root@jiangwang home]# mkdir test
#Create a multi-level directory[root@jiangwang home]# mkdir test1/test2/test3/test4: cannot create directory 'test1/test2/test3/test4' : No such file or directory # <==
#Add this -p option, you can help you create a multi-layer directory!
[root@jiangwang home]# mkdir -p test1/test2/test3/test4
#Create a directory with permissions RWX --x--x.
[root@jiangwang home]# mkdir -m 711 test2
[root@jiangwang home]# ls -l
drwxr-xr-x 2 root root 4096 Mar 12 21:55 test
drwxr-xr-x 3 root root 4096 Mar 12 21:56 test1
drwx--x--x 2 root root 4096 Mar 12 21:58 test2
Copy the code
Rmdir (Delete empty directories)
Grammar:
Rmdir [-p] Indicates the directory nameCopy the code
Options and parameters: **-p: ** Is deleted along with the “empty” directory of the previous level
Testing:
#How many directories exist?
[root@jiangwang home]# ls -l
drwxr-xr-x 2 root root 4096 Mar 12 21:55 test
drwxr-xr-x 3 root root 4096 Mar 12 21:56 test1
drwx--x--x 2 root root 4096 Mar 12 21:58 test2
#You can just delete it. No problem
[root@jiangwang home]# rmdir test
#Because still have content, so cannot delete![root@jiangwang home]# rmdir test1 RMdir: failed to remove 'test1' : Directory not empty
#Test1 /test2/test3/test4 can be deleted immediately with the -p option.
[root@jiangwang home]# rmdir -p test1/test2/test3/test4
Copy the code
Note: this rmdir can only delete empty directories. You can use the rm command to delete non-empty directories.
Cp (copy files or directories)
Grammar:
[root@www ~]# cp [-adFilprsu] Source file destination file [root@www ~]# cp [options] source1 source2 source3.... directoryCopy the code
Options and parameters:
- -a: Equivalent to -PDR. For PDR, please refer to the following instructions. (common)
- -p: copy the file with its properties instead of using the default properties (usually used for backup).
- -d: If the source file is a link file, copy the link file properties instead of the file itself;
- -r: indicates recursive continuous replication, used for directory replication. (common)
- -f: force. If the target file already exists and cannot be opened, remove it and try again.
- -i: if the destination file already exists, the action will be asked before overwriting.
- -l: creates a hard link file instead of copying the file itself.
- -s: the file is copied as a symbolic link, or “shortcut” file.
- -u: Upgrades destination only if destination is older than source.
Testing:
#Find a directory that has files, so I'm going to find root
[root@jiangwang home]# cd /root
[root@jiangwang ~]# ls
install.sh
[root@jiangwang ~]# cd /home
#Copy install.sh from root to home
[root@jiangwang home]# cp /root/install.sh /home
[root@jiangwang home]# ls
install.sh
#Copy again, add -i parameter, add override query?[root@jiangwang home]# cp -i /root/install.sh /home cp: overwrite '/home/install.sh'? Y # n is not covered, y is coveredCopy the code
Rm (Remove file or directory)
Grammar:
Rm [-fir] File or directoryCopy the code
Options and parameters:
- -f: indicates force. If a file does not exist, no warning message will be displayed.
- -i: In interactive mode, the system asks the user whether to take an action before deleting the user
- -r: Recursive deletion! Most commonly used in directory deletion! This is a very dangerous option!!
Testing:
#Delete the install.sh you just created in the cp instance!
[root@jiangwang home]# rm -i install.sh
rm: remove regular file ‘install.sh’? y
#If you add the -i option will be asked oh, to avoid you delete to the wrong file name!
#Try not to use rm -rf/on the server
Copy the code
Mv (Move files and directories, or change names)
Grammar:
[root@www ~]# mv [-fiu] source destination
[root@www ~]# mv [options] source1 source2 source3 .... directory
Copy the code
Options and parameters:
- -f: force Indicates the force. If the target file already exists, the system overwrites the file directly.
- -i: If the destination file already exists, the system asks whether to overwrite it.
- -u: Update only if the target file already exists and the source is new
Testing:
#Copy a file to the current directory
[root@jiangwang home]# cp /root/install.sh /home
#Create a foldertest
[root@jiangwang home]# mkdir test
#Move the copied file to the directory we created and view it
[root@jiangwang home]# mv install.sh test
[root@jiangwang home]# ls
test
[root@jiangwang home]# cd test
[root@jiangwang test]# ls
install.sh
#Rename the folder and look again!
[root@jiangwang test]# cd ..
[root@jiangwang home]# mv test mvtest
[root@jiangwang home]# ls
mvtest
Copy the code
Basic attributes
Understand file properties
Linux system is a typical multi-user system, where different users are in different positions and have different permissions. To protect system security, The Linux system has different permissions for different users to access the same file (including directory files).
In Linux we can use ll or ls -l to display the properties of a file and the user and group to which the file belongs, for example:
In this example, the first property of the boot file is represented by “d”. D “in Linux means that the file is a directory file.
In Linux, the first character indicates whether the file is a directory, file, link file, etc. :
- [d] indicates a directory
- [-] is a file.
- If [L], it is a link file.
- If [b] represents a storage interface device (random access device) in the device file;
- [c] indicates a serial port device in a device file, such as a keyboard or mouse (a one-time reader device).
The following characters are a group of three characters that are the combination of the three parameters of “RWX”.
[r] indicates read, [w] indicates write, and [x] indicates execute.
Note that the positions of the three permissions do not change; if there is no permission, a minus sign [-] will appear.
The attributes of each file are determined by 10 characters in the first part on the left (see figure below) :
From left to right, the numbers are 0 to 9.
Bits 0 determine the file type, and bits 1-3 determine that the owner (the owner of the file) has permissions on the file. Bits 4-6 determine that the owner group (the owner’s group of users) has permissions to the file, and bits 7-9 determine that other users have permissions to the file.
Among them:
The first, fourth, and seventh digits indicate the read permission. If r is used, the read permission is available. If – is used, the read permission is unavailable.
The second, fifth, and eighth digits indicate the write permission. If “W” is used, the write permission is available. If “-” is used, the write permission is unavailable.
The third, sixth, and ninth digits indicate the execution permission. If an “X” character is used, the user has the execution permission. If a “-” character is used, the user does not have the execution permission.
For a file, it has a specific owner, that is, the user who has ownership of the file.
Also, in Linux, users are classified by group, with one user belonging to one or more groups.
Users other than the file owner can be divided into group users of the file owner and other users.
As a result, The Linux system assigns different file access rights to file owners, file owner group users, and other users.
In the above example, the boot file is a directory file, and both the owner and owner group are root.
Modifying file Attributes
1. CHGRP: Changes the file owner group
CHGRP [-r] Indicates the name of the genus groupCopy the code
-r: changes the file ownership group recursively. When changing the ownership group of a file in a directory, if the -r parameter is added, the ownership group of all files in the directory will be changed.
2. Chown: Change the file owner or file owner group
Chown [-r] Indicates the name of the owner group. Chown [-r] Indicates the name of the owner groupCopy the code
3. Chmod: Change 9 attributes of the file
Chmod [-r] xyz file or directoryCopy the code
Linux file attributes can be set in two ways, numeric and symbolic.
Linux files have nine basic permissions: Owner, Group, and others each have their own read, write, and execute permissions.
The file permissions are “-rwxrwxrwx”, and the nine permissions are grouped in groups of three. Among them, we can use numbers to represent each authority. The comparison table of scores of each authority is as follows:
r:4 w:2 x:1
Copy the code
The r, W, and x permissions of each identity (owner/group/others) must be added. For example, if the permission is [-rwxrwx–], the score is:
- owner = rwx = 4+2+1 = 7
- group = rwx = 4+2+1 = 7
- others= — = 0+0+0 = 0
chmod 770 filename
Copy the code
You can go down and test more yourself!
View file Contents
An overview of the
In Linux, run the following command to view the contents of a file:
- Cat displays the contents of the file starting with the first line
- Tac is displayed from the last line, and you can see that TAC is written cat backwards!
- Nl display when, incidentally output line number!
- More Displays file contents one page at a time
- Less is similar to more, but better than more, it can move on!
- Head only reads the first few lines
- Tail only looks at a few lines of the tail
You can use *man [command]* to view the documentation for each command, e.g. Man cp.
Cat displays the contents of the file starting with the first line
Grammar:
cat [-AbEnTv]
Copy the code
Options and parameters:
- -a: integration option equivalent to -vet, listing special characters instead of whitespace;
- -b: lists the line numbers. Only non-blank lines are displayed. Blank lines are not marked.
- -e: displays the ending line breaking byte $.
- -n: Prints the travel number, with a blank line number, as opposed to the -b option;
- -t: displays the [TAB] key as ^I.
- -v: lists some special characters that cannot be seen
Testing:
#Check the network configuration: /etc/sysconfig/network-scripts/
[root@jiangwang ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
BOOTPROTO=dhcp
ONBOOT=yes
Copy the code
tac
Tac is the opposite of cat. The file starts from the last line. Tac is written cat backwards! Such as:
[root@jiangwang ~]# tac /etc/sysconfig/network-scripts/ifcfg-eth0
ONBOOT=yes
BOOTPROTO=dhcp
DEVICE=eth0
Copy the code
Nl Displays the line number
Grammar:
Nl - BNW fileCopy the code
Options and parameters:
- -b: specifies the line number. There are two ways to specify the line number: -b a: lists the line number regardless of whether the line is empty (similar to cat -n). -b t: If there are empty lines, do not list the line number of the empty line (default value).
- -n: line numbers are displayed in the following three ways: -n ln: line numbers are displayed on the left of the screen. -n rn: The row number is displayed to the right of its column without adding 0. -n rz: The line number is displayed on the right of the column and 0 is added.
- -w: indicates the number of digits occupied by the row number column.
Testing:
[root@jiangwang ~]# nl /etc/sysconfig/network-scripts/ifcfg-eth0
1DEVICE=eth0
2BOOTPROTO=dhcp
3ONBOOT=yes
Copy the code
Turn page by page
There are several buttons you can press during the running of more:
- Blank key (space) : represents the next page;
- Enter: indicates to scroll down a row.
- / string: indicates that in the displayed content, search down for the keyword “string”;
- :f: Immediately displays the file name and the number of lines currently displayed;
- Q: Indicates that the more file is not displayed immediately.
- B or [CTRL]-b: page back, but this action is only useful for files, not pipes.
[root@jiangwang etc]# more /etc/csh.login .... ....--More--(28%) # Focus on this line! Your cursor will also wait here for your command
Copy the code
Less page by page, the following example outputs the contents of the /etc/man.config file:
The following commands can be entered during the less runtime:
- Blank key: scroll down a page;
- [pagedown] : turn down a page;
- [pageUp] : Turn up a page;
- / string: The ability to search down “string”;
- ? String: The ability to search up “string”;
- N: Repeat the previous search (and/or? About!)
- N: Repeat the previous search in reverse (and/or? About!)
- Q: Leave less;
[root@jiangwang etc]# more /etc/csh.login .... .... Here you can wait for you to enter the command!Copy the code
Head fetches the first few lines of the file
Grammar:
Head [-n number] fileCopy the code
Options and parameters: -n followed by a number, on behalf of the display of several lines of meaning!
By default, the first 10 lines are displayed! To display the first 20 lines, look like this:
[root@jiangwang etc]# head -n 20 /etc/csh.login
Copy the code
Tail fetches the last few lines of the file
Grammar:
Tail [-n number] FileCopy the code
Options and parameters:
- -n: indicates how many lines are displayed followed by a number
By default, the last 10 lines are displayed! To display the last 20 lines, look like this:
[root@jiangwang etc]# tail -n 20 /etc/csh.login
Copy the code
Extension: Linux link concept
There are two types of Linux links, one called Hard Link and the other called Symbolic Link.
In this case, the ln command generates a hard link.
Hard to connect
Hard connection refers to connecting through index nodes. In Linux file systems, files stored in disk partitions are assigned a number, called the Inode Index, regardless of their type. In Linux, it is possible to have multiple file names pointing to the same index node. Such as: If A is A hard link of B (both A and B are file names), the inode of A is the same as the inode of B. That is, one inode corresponds to two different file names, and the two file names refer to the same file. A and B are completely equal to the file system. Deleting either does not affect access to the other.
The purpose of hard links is to allow a file to have multiple valid pathnames, so that users can establish hard links to important files to prevent “accidental deletion” function. The reason for this is as described above, because there is more than one connection to the index node that should be the directory. Deleting only one connection does not affect the index node itself or other connections, and only when the last connection is deleted will the file’s data blocks and directory connections be released. In other words, a file can only be deleted if all hardlinked files associated with it are deleted.
Soft connection
The other type of Link is called a Symbolic Link, also known as a soft Link. Soft link files have shortcuts similar to Windows. It’s actually a special file. In symbolic links, a file is actually a text file that contains information about the location of another file. For example, A is the soft link of B (both A and B are file names), and the inode number of the directory entry of A is different from the inode number of the directory entry of B. A and B point to two different inodes, which in turn point to two different data blocks. But the data block of A holds only the path name of B (from which you can find the directory entry of B). There is A “master-slave” relationship between A and B. If B is deleted, A still exists (because the two are different files) but points to an invalid link.
Testing:
[root@jiangwang /]# CD home/ [root@jiangwang home]# touch f1 # Create a test file f1 [root@jiangwang home]# ls f1 Jiangwang WWW [root@jiangwang home]# ln f1 f2 # Create a hard connection file for F1 f2 [root@jiangwang home]# ln -s f1 f3 # Create a symbolic connection file for F1 f3 [root@jiangwang 923359-rw-r --r-- 2 root root 0 Mar 8 15:11 f1 923359-rw-r --r-- 2 root root 0 Mar 8 15:11 f2 923360 lrwxrwxrwx 1 root root 2 Mar 8 15:12 f3 -> f1Copy the code
As can be seen from the above results, the inode node of hardlink file F2 is the same as the original file F1 (923359), but the inode node of symbolic link file is different.
# echoString output >> f1 Output to the f1 file[root@jiangwang home]# echo "I am f1 file" >>f1 [root@jiangwang home]# cat f1 I am f1 file [root@jiangwang home]# cat f2 I am f1 file [root@jiangwang home]# cat f3 I am f1 file [root@jiangwang home]# rm -f f1 [root@jiangwang home]# cat f2 I am f1 file [root@jiangwang home]# cat f3 cat: f3: No such file or directory [root@jiangwang home]#Copy the code
If the original f1 file is deleted, the hard link F2 is not affected, but the symbolic link F1 file is invalid.
Based on this you can do some relevant tests, you can get all the following conclusions:
- If symbolic connection F3 is deleted, f1 and F2 will not be affected.
- If hard connection F2 is deleted, f1 and F3 are not affected.
- Delete the original file F1, hard connection F2 has no impact, resulting in symbol connection F3 failure;
- Delete the original file f1 at the same time, hard link f2, the entire file will be deleted.