Linux common instructions (summary)

foreword

This blog will summarize the basic operating instructions of Linux based on the existing knowledge. Because my understanding of Linux is still at a relatively shallow level, this blog is only a summary of the most basic instructions of Linux. The following blogs are only for personal use. A summary of the learning process, it would be a great honor to be of help to all bloggers.

Basic instructions for directory or file operations

ls command

Function: For a directory, the subdirectories and files under the directory will be listed; for files, the file name and other related information will be listed
Syntax: ls [option] [directory or file]

Common options:

  • ls -a : list all files in the directory
  • ls -d : display the directory as a file
  • ls -k : Indicates the file size in k bytes
  • ls -l : list detailed information about files
  • ls -r : Reverse the directory display
  • ls -t : display in chronological order
  • ls -R: List all files in subdirectories

example:
insert image description here

pwd command

Function: Display the current directory of the user
Syntax: pwd
insert image description here

cd command

Function: Switch the working directory, switch the current working directory to the specified directory
Syntax: cd directory name

Common options:

  • cd . . : Return to the previous directory
  • cd ~ : return to the user directory
  • cd - : Return to the last visited directory

In the Linux system, the directory root files on the disk are formed into a directory tree, and each node is a directory or file
insert image description here

touch command

Syntax: touch [option] File
function: The touch command parameter can change the date and time of the document or directory, or create a new file
example
insert image description here

mkdir command

Syntax: mkdir[options] dirname(directory name)
Function: Create a directory named "dirname" under the current directory
Example
insert image description here

rm command

Function: Delete file or directory
Syntax: rm [option] [directory name/file name]

Common options:

  • rm -f : force deletion, even if the file is read-only, it will be deleted directly
  • rm -i : Ask for confirmation one by one before deleting
  • rm -r : delete the directory and all files under it

Example:
insert image description here
ps: When performing a delete operation, try not to use the forced delete command, and carefully confirm the file name before proceeding

cp command

Function: copy file or directory
Syntax : cp[option] source file or directory name target file or directory name
Explanation: cp command is used to copy files or directories, if more than two files or directories are specified in one command at the same time, it will be Different results are produced depending on whether the last destination is an existing directory. If it is an existing directory, all previous directories and files will be copied to this directory. If not, an error message will be reported.

Common options:

  • cp -f or -force: Forcibly copy files or directories, regardless of whether the destination directory or file already exists
  • cp -i or -interactive: ask user before overwriting files
  • cp -r : Recursive processing, processing 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

Example:
insert image description here

mv command

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

Common options:

  • mv -f : Mandatory overwriting, if the target file already exists, directly overwrite without asking
  • mv -i : If the target file already exists, ask if you want to overwrite it

Example:
insert image description here

man

Function: View the documentation of a command
Syntax: man [option] command

Common options:

  • man -k : search the online help by keyword
  • man num can only be found on page num
  • man man can view several chapters and meanings in the man manual

Basic commands for file operations

vi command

Function: Default text editor on Linux
Syntax: vi filename

Operation instructions inside the text editor:
In view mode:
i: Enter edit mode (start editing from the cursor position)
o: Enter edit mode (start editing from a new line
j/k: The function is the same as the up and down arrows, switch lines
/search Content: Search after pressing Enter, press n after searching to switch to the next matching string, shift+n to return to the previous
gg: switch to the beginning of the entire document
shift+g: switch to the end of the entire document
dd: delete The line where the cursor is
located After completing all operations, enter:
w: write (save the modified document)
q: exit
!: force
wq: exit after writing
! q: force exit without writing,
and finally press enter to execute
in edit mode:
esc: switch back In view mode
, except the esc key, other keys will be edited as text content in edit mode

cat command

Function: Display file content (print output)
Syntax: cat [options] filename

Common options:
cat -n: number all lines of output

Example:
insert image description here

less instruction

Function: View the content of the file, even if all the content of the file will not be loaded into the memory immediately, you can also search
Syntax: less [parameter] file name

Commonly used options:
j/k/arrow keys: scroll up and down the screen
less -N: display the number of each line
n: repeat the previous search
q: exit

head command

**Function:** Used to display the beginning of the document to the standard output, the default head command prints the first 10 lines of the corresponding file
Syntax: head [option] file name

Common options:

  • head -n+lines: how many lines before the custom display

Example:
insert image description here

tail command

Function: Used to display the content at the end of the specified file
Syntax: tail [options] [parameters] filename

Common options:

  • -f read in a loop
  • How many lines after -n+line number display

Example:
insert image description here

pipeline

Pipes are an old way of "interprocess communication". You can use | as a pipe symbol in Linux commands.

Syntax: Command 1 | Command 2
Function: Use the output of the previous command as the input of the next command

Example:
output line 15 in the test.txt file
insert image description here

redirect

In Linux, the startup of each process will be accompanied by the opening of these three IOs by default, that is, standard output, standard input, and standard error, which will correspond to the three devices of the monitor, keyboard, and monitor respectively. In Linux, many commands will
be The default output is to the standard output, that is, it is displayed on the monitor. For example: cat test.txt
We can use the > symbol to write the content that should be output to the standard output to the specified file. This operation is called redirection

Redirection can be broken down into three types

1. Standard input redirection: <
insert image description here
2. Standard output redirection >
insert image description here
3. Standard error redirection: 2 >
insert image description here

Therefore, in the previous example in the introduction pipeline, it is also possible to output the 15th line of the test.txt file. In the above,
insert image description here
first write the output of the first 15 lines of test.txt to a temporary file temp (if the current directory cannot find The file will automatically create one), and then output the last line of tmep

Process, network, system statistics related instructions

View process pid

Find by port number:

netstat -anp | grep port number

Example:
insert image description here
Find by process name:

ps -ef | grep process name
ps aux | grep process name

Example:
insert image description here
insert image description here

Linux users and permissions

Linux users

There are two types of users under Linux: super user (root) and ordinary user

  • Super User: Can perform anything under Linux system
  • Ordinary users: limited things to do under the Linux system

Related commands:
useradd username——create a new user
password password—set password
su -username——switch to a user, and switch system environment variables will also switch to the specified user

User permissions to access files

Basic authority
i read (r/4): Read has the authority to read the content of the file for the file; for the directory, it has the authority to browse the directory information write
(w/2): Write has the authority for the file The authority to modify the content of the file; for the directory, it has the authority to delete the files in the mobile directory
Execute (x/1): execute For the file, it has the authority to execute the file; for the directory, it has the authority to enter the directory
"-" Indicates that it does not have this permission

chmod: command

Function: Set the access permission of the file
Syntax: chmod [option] permission file name

Common options:

  • R -> : recursively modify the permissions of directory files

The relevant format for permissions:

  • +: increase permissions
  • -: Cancel permission
  • =: grant permission

User symbol:

  • u: owner
  • g: same group as the owner
  • o: other users
  • a: all users

ll: View the permissions and accessible users of files in the current directory
insert image description here

Guess you like

Origin blog.csdn.net/m0_46233999/article/details/119533653