A complete collection of commonly used commands in the Linux operating system: file operations

Linux operating system instructions

Previous: Linux Operating System Instructions (1) System Management

mkdir command

Syntax: mkdir [option] dirname...
Function: Create a directory named "dirname" under the current directory
Common options:

-p, --parents ** can be a path name. At this time, if some directories in the path do not exist yet, after adding this option, the system will automatically create those directories that do not exist, that is, multiple directories can be created at one time; ****

Example:

mkdir –p test/test1 : 递归建立多个目录  

rmdir command && rm command

rmdir is a command corresponding to mkdir. mkdir is to create a directory, and rmdir is a delete command.

  • grammar:
 rmdir [-p] [dirPath]
  • Applicable objects : all users with current directory operation authority

  • Function : delete empty directory

  • Common options:

    • -p When the subdirectory is deleted, if the parent directory also becomes an empty directory, delete the parent directory together.

The rm command can delete files or directories at the same time

  • grammar:

     rm [-f-i-r-v] [dirName/dir]
    

    Parameter information:

    • -f Even if the file attribute is read-only, delete it directly
    • -i Ask for confirmation one by one before deleting
    • -r delete the directory and all files under it
  • Applicable object : all users

  • Function : delete file or directory

cp command

This cp is not that cp! ! owo

Syntax: cp [options] source file or directory target file or directory

Function: copy files or directories

Explanation: The cp command is used to copy files or directories. If more than two files or directories are specified at the same time, and the final destination is an existing directory, it will copy all the previously specified files or directories to this directory. . If multiple files or directories are specified at the same time, and the final destination is not an existing directory, an error message will appear.

Common options:

  • -f or --force Forcibly copy files or directories, regardless of whether the destination file or directory already exists
  • -i or --interactive ask user before overwriting files
  • -r Recursive processing, processing the files and subdirectories under the specified directory together. If the form of the source file or directory does not belong to a directory or a symbolic link, it will be treated as an ordinary file
  • -R or --recursive recursive processing, the files and subdirectories in the specified directory will be processed together

Example:

# copy files

cp test.c new.c # Copy and generate a new.c file in the current folder

cp test.c … # Copy and generate a test.c file in the upper directory

cp test.c …/new.c # Copy and generate a new.c file in the upper directory

# copy folder

cp -r test new # Copy the test folder in the current directory to the new folder, if the file in the source folder does not belong to the user, it will prompt

cp -rf test new # Copy the test folder in the current directory to the new folder, and ignore the user's ownership

mv command

The mv command is an abbreviation for move, which can be used to move (cut) files or rename files.

Syntax: mv [options] source file or directory target file or directory

Function:

  1. Depending on the type of the second parameter in the mv command (whether it is a target file or a target directory), the mv command renames the file or moves it to a new directory.
  2. When the second parameter type is a file, the mv command completes the file renaming. At this time, there can only be one source file (it can also be the source directory name), and it will rename the given source file or directory to the given target filename.
  3. When the second parameter is an existing directory name, there can be more than one source file or directory parameter, and the mv command will move the source files specified by each parameter to the target directory.

Common options:

  • -f : force means that if the target file already exists, it will be overwritten without asking
  • -i : If the target file (destination) already exists, it will ask whether to overwrite

Example:

mv test.c newFolder/ # Move test.c file to newFolder

mv test newFolder # Move the test folder to the newFolder folder.

mv test.c newName.c # Rename the test.c file name to newName.c

cat command

Function: View the content of the target file in positive order, and the end of the output is the end of the text.

Syntax: cat [options] [file]

in:

  • -b number non-empty output lines
  • -n number all lines of output
  • -s Do not output multiple blank lines

Extension: view the content of the target file in reverse order

tac [options] [files] (names are reversed)

more command

Syntax: more [option] [file]
Function: more command, the function is similar to cat, but it starts to view from the beginning and scrolls down gradually , and supports viewing n lines at a time, but does not support scrolling up .
Common options:

  • -n number all lines of output
  • q exit more

Example:

more test.c # Read the test.c file from the beginning, and load the maximum number of lines that the screen can reach, press Enter to load 1 line at a time

more -5 test.c # Read the test.c file from the beginning, read 5 lines at a time, and press Enter to add 5 lines

#skip view

more -5 test.c # Specify the number of lines, otherwise the maximum line will be loaded

/100

# From the beginning, enter /100 during the viewing process to directly browse to the 100th line. If it exceeds the number of lines in the file, an error will be prompted.

less command

Function: less is similar to more, but you can use less to browse files at will , while more can only move forward, but not backward, and less will not load the entire file before viewing .

Syntax: less [parameters] file

parameter:

  • -i ignore case when searching
  • -N display the line number of each line
  • /string: function to search down for "string"
  • ?string: function to search upwards for "string"
  • n: repeat previous search (related to / or ?)
  • N: Repeat previous search in reverse (relative to / or ?)
  • q:quit

head command

It is used to display a certain number of text blocks at the beginning or end, head is used to display the beginning of the file to standard output

Syntax: head [parameter]... [file]...
Function: head is used to display the beginning of the file to the standard output, and the default head command prints the first 10 lines of the corresponding file.
options:

-n<number of lines> number of lines to display

Example:

head -5 test.c #Output the first 5 lines in the test.c file (to the standard output, in human language: output to the screen)

tail command

head and tail are as easy to understand as their names, head is used to display the beginning of the file to the standard output, and tail is to see the end of the file. The tail command writes the file to standard output starting from the specified point (the default is the last 10 lines).

Syntax: tail[required parameters][option parameters][file]

Function: Used to display the content at the end of the specified file. If no file is specified, it will be processed as input information . Commonly used to view log files.

parameter:

  • f loop read
  • -n<number of lines> Display the number of lines.

Use the -f option of the tail command to easily check the log file being changed. tail -f filename will display the last content in filename on the screen, and not only refresh, so that you can see the latest file content.

Extension: How to range read?

We have learned 4 reading methods, now let's try how to read the data in the range.

Extended knowledge points:

In Linux, the echo command outputs content to standard output, for example:

echo "Hello World"		
# output:
[root@]# echo "Hello World"
Hello World

echo 	
# output:
[root@]# echo 
...(空行)

At this time, we input the string in double quotes to the standard output (screen) by default, but what if we need to output the content to a file? Very simple, this time we introduced "output redirection".

echo "Hello World" > log.txt
# 这句话的意思是,将输出的内容,输出重定向流入log.txt文件内,不再流入标准输出器内(此刻的流是在语言层面上帮助理解,但不能认为操作系统该操作为“流”!!)
# 此时,我们会把Hello World文字输出到当前目录内的log.txt文件内,如果有当前文件则直接覆写,如果没有则创建之。

# 如果我们不想覆写写入文档内,那么我们只需要多加一个尖括号
echo "Hello World" >> log.txt
# 此时内容不再是覆写,而是“追加重定向”在文档尾部

The cat we mentioned earlier can read information from the file to the standard output, so if we do not specify the file name, for example:

cat
# 此时,我们系统会从我们的标准输入内读取信息(说人话:键盘,例如C/C++里的scanf, cin)

Then we can output redirection just now, can we output redirection now? The answer is yes.

cat < test.c
# 此时,test.c的内容会直接流入cat内

Is there a way to temporarily store the content and flow into the next unit. Very simple, we can use "pipelines".

cat file.txt | head -10
# 中间的 | 就是管道的意思。
# 这里的意思是,cat读取file.txt内的内容,并将内容通过管道 | 放入head 里,最后head在将其进行 -10 操作
# 系统像流水线一样将内容逐个处理完流入到最后一个处理单元内。
# 小tips: 使用管道时,默认隐式发生重定向!

Now that the basics are completed, let's turn the compass forward, how to read the content of the interval. At this point we simulate reading 80-100 lines of content in a file;

  • Method 1: Redirect the output to a temporary file, and finally read the temporary file

    head -100 file.txt > temp.txt		# 从头开始读100行,并重定向到temp.txt内
    tail -20 temp.txt					# 读取temp.txt内后20行内容
    
  • Method 2: Use pipelines instead of temporary files.

    #方式一
    cat file.txt | head -100 | tail -20
    # 1. 先读取file.txt,流入管道
    # 2. head从管道内进行-100
    # 3. tail从管道内进行-20
    #方式二
    head -100 file.txt | tail -20
    

    Soul Painter:

    image-20220729020608459

epilogue

In the future, we will continue to update the notes related to the Linux operating system. If you like our articles, one-click triple link, and follow up the latest articles. Don’t forget to follow our official account "01 Programming House" to read first! Pay attention to the hut, learn programming without getting lost. Your support is the motivation for us to keep going!

Guess you like

Origin blog.csdn.net/weixin_43654363/article/details/126415918