[First introduction to Linux]: Common commands (1)

Friends, guys, we meet again. In this issue, I will explain to you the basic knowledge points about Linux. If it has inspired you after reading it, please leave your comments and I wish you all the best. become!

C language column: C language: from entry to proficiency

Data Structure Column: Data Structure

Personal homepage: stackY、

C++ Column: C++

Linux Column: Linux

 

Table of contents

Basic instructions under Linux:

1. ls command

2. pwd command

3. cd command

4. touch command

5. mkdir command

5.1 tree command 

6. rmdir command and rm command

7. man command

8. cp command

9. mv command

10. Input and output redirection

10.1 Output redirection

10.2.cat command

10.3 Input redirection 


Basic instructions under Linux:

The essence of tools and instructions is similar to executable programs.

1. ls command

Syntax : ls [options][directory or file]
Function : For a directory, this command lists all subdirectories and files in the directory. For files, the file name is listed along with other information.
Common options:
  • -a Lists all files in the directory, including implicit files starting with .
  • -d displays the directory as a file, rather than displaying the files below it. For example: ls –d specifies the directory
  • -i Outputs the index information of the i node of the file. For example, ls –ai specifies the file
  • -k indicates the size of the file in k bytes. ls –alk specifies the file
  • -l List file details.
  • -n Use numeric UID, GID instead of name. (Introducing UID, GID)
  • -F Attach a character after each file name to indicate the type of the file. "*" indicates an executable ordinary file; "/" indicates a directory; "@" indicates
  • represents a symbolic link; "|" represents FIFOs; "=" represents sockets. (Directory type identification)
  • -r sorts the directory in reverse order.
  • -t Sort by time.
  • -s outputs the size of the file after the l file name. (Size sorting, how to find the largest file in a directory)
  • -R List files in all subdirectories. (recursion)
  • -1 outputs only one file per line.

Common options: ls -l and ls -l -a

Then ls -l can be abbreviated as ll, so when we use ls -l -a, we can directly write: ll -a

. represents the current directory

..indicates the upper-level directory

2. pwd command

Syntax : pwd
Function : Display the directory where the user is currently located

3. cd command

In the Linux system, the files and directories on the disk are organized into a directory tree, and each node is a directory or file.
Everything in Linux is a file.
File = content + attributes (the essence of content and attributes are data)
Syntax : cd directory name
Function : Change the working directory. Change the current working directory to the specified directory.
Example :
  • cd .. : Return to the upper directory
  • cd /home/litao/linux/ : absolute path
  • cd ../day02/ : relative path
  • cd ~ : Enter the user's home directory
  • cd - : Return to the recently visited directory

Just follow the two directories first created on my machine to demonstrate:

 

4. touch command

Syntax : touch [options]... file...
Function : The touch command parameters can change the date and time of a document or directory, including access time and change time, or create a new file that does not exist.
Common options :
  • -a or --time=atime or --time=access or --time=use only changes the access time.
  • -c or --no-create Do not create any documentation.
  • -d Use the specified date and time instead of the current time.
  • -f This parameter will be ignored and will not be processed. It is only responsible for solving the compatibility problem of the BSD version of the touch command.
  • -m or --time=mtime or --time=modify only change the change time.
  • -r sets the date and time of the specified document or directory to be the same as the date and time of the reference document or directory.
  • -t Use the specified date and time instead of the current time.

In this section we only understand the function of touch to create ordinary files:

Create a normal file in the current directory (the file does not exist)

The time the file was changed (the file exists)

5. mkdir command

Syntax : mkdir [options] dirname...
Function : Create a directory named "dirname" in 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 yet exist, that is, multiple directories can be created at one time.

 mkdir directory name: Create a new directory under the current directory

mkdir -p directory1/directory2/...: Create directories recursively

5.1 tree command 

tree .: The current directory is displayed in a tree-like structure

How to install the tree command?

yum install -y tree

After installation, enter the tree command to display the files in the directory in a tree structure.

6. rmdir command and rm command

rmdir is a command corresponding to mkdir. mkdir creates a directory, and rmdir deletes a directory.
Syntax : rmdir [-p][dirName]
Applicable objects : All users with permission to operate the current directory
Function : Delete empty directories
Common options :
        -p When the subdirectory is deleted, if the parent directory also becomes an empty directory, the parent directory will be deleted together.
The rm command can delete files or directories at the same time
Syntax : rm [-firv][dirName/dir]
Applicable to : All users
Function : Delete files or directories
Common options :
  • -f Even if the file attribute is read-only (i.e. write-protected), delete it directly
  • -i Ask for confirmation one by one before deleting
  • -r deletes the directory and all files under it

rmdir directory: only empty directories can be deleted

Since our test directory is not empty, it prompts that the directory is not empty and cannot be deleted.

rm file: delete empty files

rm -f file: Forcefully delete the file (without prompting)

rm -r directory: delete all branch nodes containing this directory in this directory (recursive deletion)

rm -r -f directory: forcefully delete all branch nodes containing this directory (recursive deletion)

rm -rf ./*: means to delete all files and directories matching the current directory

7. man command

Linux commands have many parameters, and it is impossible for us to remember them all. We can get help by checking the online manual. The command to access the Linux man page is man
Syntax : man [options] command
Common options
  • -k Search online help num based on keywords only in chapter num
  • -a displays all chapters, such as man printf. By default, it starts searching from the first chapter and stops when it is known. Use the a option. When pressing q to exit, it will continue to search until all chapters are searched. .
Explain, the manual is divided into 8 chapters
  • 1 is a normal command
  • 2 is a system call, such as open, write, etc. (Through this, you can at least easily find out what headers need to be added to call this function.
  • pieces)
  • 3 is a library function, such as printf, fread4 is a special file, that is, various device files under /dev
  • 5 refers to the format of the file, such as passwd, which will explain the meaning of each field in the file.
  • 6 is reserved for games and is defined by each game.
  • 7 is the attachment and there are some variables. For example, global variables such as environ are explained here.
  • 8 is a command used for system management. These commands can only be used by root, such as ifconfig

 man + (manual) + command

8. cp command

Syntax : cp [ options ] source file or directory target file or directory
Function : Copy files or directories
Description : 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 forcefully copies a file or directory, regardless of whether the destination file or directory already exists.
  • -i or --interactive ask the user before overwriting the file
  • -r recursively processes files and subdirectories in the specified directory together. If the form of the source file or directory does not belong to the directory or symbolic chain
  • If connected, it will be treated as an ordinary file.
  • -R or --recursive recursive processing, processing files and subdirectories in the specified directory together

cp src dst: copy directly

cp -rf src dst: recursive forced copy

9. mv command

The mv command is the abbreviation of move . It can be used to move files or rename files ( move (rename) files ). It is a commonly used command in Linux systems and is often used to back up files or directories.
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 will rename the file or move it to a new one.
  • in the 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).
  • Renames the given source file or directory to the given destination file name.
  • 3. When the second parameter is the name of an existing directory, there can be multiple source files or directory parameters. The mv command will move all the source files specified by each parameter to
  • in the target directory.
Common options :
  • -f: force means force. If the target file already exists, it will be overwritten directly without asking.
  • -i: If the destination file (destination) already exists, you will be asked whether to overwrite it!

mv src dst (does not exist): rename

mv src dst(exists): cut

10. Input and output redirection

echo string: you can print the string directly (displayed on the monitor)

10.1 Output redirection

echo string > file: Output the string originally output on the display file (standard output) to the specified file.

Principle: 1. Create a file 2. Write a string to a file

> File (does not exist): Create an empty file

***Note : Every time output redirection is written to a file, the original content in the file needs to be cleared.

> File (exists): Clear the file

>> file (exists): append redirect

10.2.cat command

Syntax : cat [ options ] [ file ]
Function : View the contents of the target file
Common options :
  • -b output line numbers for non-empty lines
  • -n numbers all lines of output
  • -s does not output multiple blank lines

cat file-n: View the contents of the file on the monitor

10.3 Input redirection 

Cat defaults to reading from the keyboard (standard input)

cat < file: originally reads data from the keyboard, but is redirected to read data from the specified file

Friends and guys, good times are always short. Our sharing in this issue ends here. If you want to know what happens next, please listen to the next episode~. Don’t forget to leave you precious after reading it. Thank you for your support! 

Guess you like

Origin blog.csdn.net/Yikefore/article/details/133465996