This article mainly introduces some common file management commands in Linux, as follows:
PWD, ls, CD, cp, mv, rm, ln, mkdir, cat, more, less, head, tail
The PWD command is used to display the path of the current working directory
> pwd
# /home/user
Copy the code
The ls command, or list, is used to list the contents of the current working directory. The parameters are as follows:
-a
: Displays all files, including hidden files and directories-l
: Displays detailed information. If this parameter is not specified, only the name is displayed-t
: Displays contents sorted by modification time-s
: Displays content sorted by file size
> ls -l
# drwxrwxr-x 2 user user 4096 Sep 12 16:05 folder
#-rw-rw-r-- 1 user user 12 Sep 12 16:00 file
Copy the code
After the -l parameter is added, seven fields are displayed in each file or directory. The meanings of each field are as follows:
- File description: This will be introduced in detail in the next article, interested friends may wish to have a sneak peek
- Number of children: 1 if files; In the case of a directory, the total number of subdirectories and files contained in the directory
- File owner
- File Group
- File size, expressed in bytes. Most directories are expressed in 4096
- Change the time in the format of
Month Day Hour:Minute
- The file name
The CD command, full name of which is change directory, is used to switch working directories. The commands for switching common directories are as follows:
cd ~
: Switch to the home directory. This command can be abbreviated ascd
cd /
: Switch to the root directorycd -
: Switches to the previous directorycd ..
: Switches to the upper-level directory
> cd
Copy the code
The cp command, short for copy, is used to copy files and directories. The common parameters are as follows:
-r
: Is used to copy subdirectories and files together-p
: In addition to copying the content, the change time is copied along with the access permission-d
: Preserve links while copying-f
: If the destination contains a file with the same name, the file is overwritten-i
: If the destination contains a file with the same name, the user is required to confirm whether the file is overwritten-a
: is used to copy directories-dpr
The combination of
> cp [option] source0 [,source1 [,source2 [,...]]] destination
Copy the code
The mv command, short for move, is used to move files and directories. The parameters are as follows:
-f
: If the destination contains a file with the same name, the file is overwritten-i
: If the destination contains a file with the same name, the user is required to confirm whether the file is overwritten
> mv [option] source0 [,source1 [,source2 [,...]]] destination
Copy the code
Tip: This command can be used to rename A file, for example, to rename A to B as mv A B
The rm command, full name of which is remove, is used to delete files and directories. The common parameters are as follows:
-r
: Deletes subdirectories and files together. This parameter is used when deleting directories-f
: Forcible deletion, regardless of the current state of a file or directory-i
: Ask whether you want to delete it one by one
> rm [option] source0 [,source1 [,source2 [,...]]]
Copy the code
Note: Use this command with extreme care, because in Linux deleting is a permanent removal that can never be retrieved
The ln command, which stands for link, is used to create a link file
> The new file and the original file point to the same data (actually the same I-node).
> ln source destination
> # create a soft link, where the new file will only store the location of the original file, without saving any actual file content
> ln -s source destination
Copy the code
Here’s a direct example:
> Create a file called fileA
> touch fileA
> Create fileA hard link fileB
> ln fileA fileB
> Create fileA soft link fileC
> ln -s fileA fileC
#At this point, the relationship between the three files can be roughly represented as the following figure
#
# i-node (pointing to the disk block where the data is actually stored)
#/ \
# / \
# fileA fileB
# /
# /
#FileC (saves the location of fileA)
> # write fileA
> echo "Hello World" > fileA
> # check the contents of fileB and find the same contents as fileA
> # Since fileB and fileA point to the same I-node, they read the same data
> cat fileB
> FileC = fileA
> FileC first finds fileA based on the saved location information, and then reads the contents of the disk based on the i-node that fileA points to
> cat fileC
> # remove fileA
> rm fileA
#At this point, the relationship between the three files can be roughly represented as the following figure
#
# i-node (pointing to the disk block where the data is actually stored)
# \
# \
# fileB
#
#
#FileC (saves the location of fileA)
> # Check the contents of fileB and find the same as the original
> # Removing fileA does not affect the fileB pointing to the original I-Node
> cat fileB
> # check the contents of fileC and find the corresponding file
> # delete fileA, fileC could not find fileA based on saved location information
> cat fileC
Copy the code
The mkdir command, short for make directory, is used to create a directory
> mkdir folder
Copy the code
By the way, creating a file can be done with the touch command
> touch file
Copy the code
The cat command, short for concatenate, is used to read and print content. It is often used in conjunction with the redirection character
If no file is specified, the content is read from the keyboard. If there is a specified file, the content is read from the file
> Read from the keyboard and write to the file via redirection
> cat > file
> Read from the keyboard and append to the file via redirection
> cat >> file
> # Read the content by the file, and print on the screen, can be used to view the file
> cat file
> The contents are read from a file and appended to another file by redirection, which can be used to concatenate files
> cat file >> file2
Copy the code
When the keyboard reads the content, press Enter to confirm the input, and press Ctrl+C to finish the input
The more command is used to display file contents in pages. It is suitable for reading long files
> more file
Copy the code
In this reading mode, press Enter to scroll to the next line, press Space to jump to the next page, and press Q to exit
The less command, which displays the contents of a file interactively, is suitable for reading long files
> less file
Copy the code
In this reading mode, you can use the arrow keys to control the screen scroll and press Q to exit
The head command and tail command are used to display the first and last contents of a file respectively. The common parameters are as follows:
-c Number
: Specifies the first/last Number bytes to display-n Number
: Specifies the first/last Number line to display
> Display the first 5 bytes of the file
> head -c 5 file
> Display the last 5 lines of the file
> tail -n 5 file
Copy the code