Linux Common Instructions (Part 2)

Table of contents

One: Linux basic instructions

view the online manual

text view related

time related

find related

Packaging and Compression Related

Check the Linux version and system

Other commands and hotkeys

Two: Redirect

input redirection

output redirection

Three: pipeline


One: Linux basic instructions

view the online manual

Linux commands have many parameters, we can't remember all of them, we can get help by checking the online manual.

man command

Syntax: man option command

Common options:

  • -k search manpage by keyword
  • -a will display all the chapters, such as man printf, it will start searching from the first chapter by default, stop when you know it, use the a option, when you press q to exit, it will continue to search until all chapters are searched

8 chapters of the manual:

  1. User commands: Contains commands and tools that most users can call directly
  2. System calls: Contains details of various system calls provided by the Linux kernel
  3. Library functions: Contains detailed information on various standard C library functions.
  4. Devices and Special Files: Contains detailed information on device drivers and special files.
  5. File Formats: Contains detailed information on various file formats and configuration files.
  6. Games: Contains some basic game programs.
  7. Other Miscellaneous: Contains various other miscellaneous information, such as conventions, norms, and various agreements.
  8. System Management Commands: Covers various system management commands, such as file and directory management, system monitoring, backup and restore, network management, disk management, and log management.

Some shortcut keys:

  • q quit
  • Enter Scroll down by line
  • Space page down
  • b turn up a page
  • /string lookup a string in a man page
  • num is only found in a few chapters


text view related

cat command (view the content of the target file)

Syntax: cat option file

Common options:

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

tac command (basically the same as cat, but printed from bottom to top)


more command (only one screen will be printed, not all will be printed, press Enter to scroll down)

Syntax: more option file

Common options (press q to exit):

  • -n number all lines of output

less command (print a screen, you can turn pages up and down)

Syntax: less option file

Common options:

  • -i ignore case when searching
  • -N display the line number of each line

hot key:

  • /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 (print the first 10 lines of the file by default)

Syntax: head options file

Options: -n print the first n lines

tail file (consistent with the head option, the last 10 lines of the file are printed by default)



time related

date command (display system time in default format)

date Display time in the specified format: date +%Y:%m:%d

The user can set the format to be displayed. The format is set as a plus sign followed by several tags. The commonly used tags are listed as follows:

  • %H : hours
  • %M : minutes
  • %S : seconds
  • %X : Equivalent to %H:%M:%S
  • %d : day
  • %m : month
  • %Y : full year
  • %F : Equivalent to %Y-%m-%d 

Timestamp:  A timestamp is a number or string that records the date and time an event occurred. It usually represents the number of seconds or milliseconds that have passed since a certain point in time. Timestamps are used extensively in computer systems, for example in logging, file creation and modification times, network communications, and more. Timestamps can be used to track the order of events, calculate time intervals, do sorting, etc.
The Linux timestamp is the number of seconds since January 1, 1970, not taking leap seconds into account.

Time -> Timestamp: date +%s

Timestamp -> time: date -d@timestamp



find related

find command (find files, possibly access disk)

Syntax: find specified directory option file

Options: -name search by filename


which command (see the path where other command programs are located)

 alias command (alias to command)

Syntax: alias new name = 'original command'


whereis command (find files with keywords in the system directory)

Syntax: whereis keyword


grep command (searches for a string in a file and prints the found lines) 

Syntax: grep option 'search string' file

Common options:

  • -i : Ignore the difference in case, so the case is treated as the same
  • -n : By the way, output the line number
  • -v: Reverse selection, that is, display the line without the content of the 'search string'


Packaging and Compression Related

zip command (packing and compression)

Syntax: zip option zip package name file/directory to be packaged

Option: -r recursive processing, all files and subdirectories in the specified directory will be processed together (if not added, only an empty directory will be compressed)

unzip command (unzip)

Syntax: unzip file name option path to be decompressed

Option: -d decompress to the specified path 


tar command (package compression and decompression can be achieved)

options:

  • -c : Create a parameter command for a compressed file (create means)
  •  -x : Unzip the parameter command of a compressed file
  • -t : view the files in the compressed package
  • -z : Does it also have the gzip attribute? That is, does it need to be compressed with gzip?
  • -j: Does it also have the attributes of bzip2? That is, does it need to be compressed with bzip2?
  • -v : Show files during compression! This is commonly used, but not recommended for background execution
  • -f : indicates the name of the file to be decompressed
  • -C (uppercase): Unzip to the specified directory

pack and compress

decompress



Check the Linux version and system

uname command

options:

  • -a view all info, kernel and architecture
  • -r only check the kernel version



Other commands and hotkeys

other:

  • history command (view historical command)
  • bc command (calculator, enter quit to exit)
  • cal command (display calendar)
  • echo command (followed by a string, the string is output in the output terminal by default)

hotkey:

  • Tab-command completion
  • CTRL+C - terminate the program in progress
  • CTRL+D - terminate login
  • CTRL+R - find historical commands
  • Up and down keys - look through the last few commands

Two: Redirect

input redirection

The default output objects of many output commands in Linux (such as echo, cat, etc.) are the standard output terminal (display interface), so can the content be output to the specified file?

">" can redirect the output of text output commands (such as echo, cat, etc.) in the form of text overwriting ( overwriting the original content) . The original echo command will output specific text content to the standard output terminal by default. ">" redirection can overwrite the text content to the specified file

 ">>" can redirect the output of the text output command in the form of text appending (the original content will not be overwritten) . For example, the echo command will output specific text content to the standard output terminal by default. ">>" redirection can Append specific text content to a file


output redirection

"<<" is the input redirection symbol for Here Document (input within the document). It allows you to specify multiple lines of input on the command line and pass it to the command as standard input. Using this notation, multiple lines of text can be specified directly on the command line without storing them in a file.

example:

In the above example, "EOF" after "<<" indicates the end tag of the input, and the following line is the content of the input. The output of the above command will display the three lines entered. 

"<" is the symbol to redirect the contents of the file as input. It passes the contents of the file to the command as standard input .


Three: pipeline

In Linux, a pipe is a special symbol used to pass the output of one command as input to another command . By using pipes, you can chain multiple commands together so that they cooperate and work together to accomplish complex tasks without creating temporary files.

The pipe symbol is the vertical bar "|" , which can pass the standard output of one command to the standard input of another command. For example, you can use pipes to pass the results of one command to another for processing without saving the results in a file .

For example:

 In this example, first take out the first 12 lines in test.txt, then save these 12 lines into the pipeline, and then print the last 2 lines in the pipeline.

Guess you like

Origin blog.csdn.net/2301_76269963/article/details/131597281