Technology Sharing

【Linux】01. Common Linux commands

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

1. ls command

grammar:ls [options] [directory name or file name]
Function: For a directory, this command lists all subdirectories and files under the directory. For a file, it lists the file name and other information
Common options

-a: List all files in the current directory, including hidden files
-l : List detailed information of the file
-d: Display directories as files instead of files under them
-R: recursively list files in all subdirectories

Example
insert image description here

summary
In Linux, files starting with . are called hidden files. In every file, there are . and . . These two files are special files. . is the current directory, and . . is the parent directory.
ls file*, which means to list the files containing file in their names, as shown in the following figure:
insert image description here

2. pwd command

grammar:pwd
Function: Print the current directory
Example
insert image description here
summary
/ is a path separator in Linux. We can locate the location of the file through the path.

3. cd command

grammar:cd directory name
Function: Jump to the specified directory
Example
insert image description here

4. tree directive

grammar:tree directory name
Function: Print the specified directory in tree form
Installation Instructions: yum install -y tree
Example
insert image description here
summary
We can see that the path is composed of multiple nodes. Any parent node can have multiple child nodes, and any child node has only one parent node, so we can say that the path is unique.

5. touch command

grammar:touch [file name]
Function: Create a new file that does not exist
Example
insert image description here

6. mkdir Command

grammar:mkdir [options] [directory name]
Function: Create a new directory under the current directory
Common options

-p: recursively create a series of directories, that is, you can create multiple directories at a time

Example
insert image description here

7.rmdir command

grammar:rmdir [options] [directory name]
Function: Delete empty directories
Common options

-p: When a subdirectory is deleted and the parent directory becomes an empty directory, the parent directory is deleted as well

Example
insert image description here

8. rm Command

grammar:rm [options] [filename or directory name]
Function: Delete files or directories. The rm command can delete files or directories at the same time.
Options

-f: Delete the file even if it is read-only (i.e. write-protected).
-i : Ask for confirmation before deleting each
-r: Delete the directory and all files under it

Example
insert image description here

9. man command

grammar:man [options] command
Function: Check the online manual for the command
Common options

Manual 1 for general instructions
Manual 2 to check system calls
Manual No. 3 to check the library functions of C language

Example
insert image description here

10. cat and tac commands

grammar:cat (tac) [options] [filename]
Function:cat (tac) View the contents of the target file in order (reverse order)
Common options

-b: Number non-empty output lines
-n: Number all output lines
-s: Do not output multiple blank lines

Example
insert image description here
summary

  1. In Linux, everything is a file (including keyboard and display). Input from the keyboard and output from the screen can be understood as reading data from the keyboard file and viewing data from the display file.
  2. echo can write data to the screen
    insert image description here
  3. The > symbol is called output redirection. If a file is connected to it, the data can be output to the target file. If there is data in the file, the data will be cleared. Therefore, the > file can create a new file.
  4. The >> symbol is called append redirection. If there is data in the file, it will continue to be written.
  5. The < symbol is called input redirection. If you follow it with a file, all the data in the file will be output to the screen.
    insert image description here

11. nano Command

grammar:nano file name
Function: Write to the target file
Installation Instructions:yun install -y nano
Example
insert image description here

12. cp command

grammar:cp [options] source file or directory target file or directory
Function: Copy a file or directory, and create the target file or directory if it does not exist
illustrate: 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 be displayed.
Common options

-f: Force copying of files or directories, regardless of whether the destination file or directory already exists
-i : Ask the user before overwriting files
-r: Recursive processing, processing the files and subdirectories under the specified directory together

Example
insert image description here

13. mv Command

grammar: mv [options] source file or directory target file or directory
Function: 1. Move files and directories 2. Rename

Common options

-f : If the target file already exists, overwrite it without asking
-i: If the target file already exists, it will ask whether to overwrite it

Example
insert image description here

14. more command

grammar:more [options] [filename]
Function: Similar to cat, you can view long files, but you can only scroll down, not up.
Options

-n: Number the output lines
q: quit

Example
insert image description here

15. less command

grammar: less [options] filename
Function: less is similar to more, but with less you can browse the file at will, while more can only move forward but not backward, and less will not load the entire file before viewing.
Options

-N: Display line numbers for each line
-i : Ignore case when searching
n : Repeat the previous search
N: Repeat the previous search in reverse direction
q : quit

16. head directive

grammar: head [options] [filename]
Function: head is used to display the beginning of a file to standard output. The default head command prints the first 10 lines of its corresponding file.
Options

-n: Display the first n lines of data

Example
insert image description here

17. tail command

grammar: tail [options] [filename]
Function: Used to display the content at the end of the specified file. If no file is specified, it is processed as input information. Commonly used to view log files.
Options

-n: Display the number of lines

Example
insert image description here
summary
So how do we intercept the data between rows 5000 and 5020?
We can intercept the first 5020 lines of data first, and then intercept the last 20 lines of data. Do we need to create a new file? We can use pipelines to cleverly solve this problem:
insert image description here

18. Time-related instructions

Date display
date specifies the format to display time: date +%Y:%m:%d
date Usage: date [OPTION]… [+FORMAT]

  1. In terms of display, users can set the format they want to display. The format is set as a plus sign followed by several tags. The commonly used tags are listed below.
    • %H : Hour (00…23)
    • %M : Minute (00…59)
    • %S : seconds (00…61)
    • %X : Equivalent to %H:%M:%S
    • %d : day (01…31)
    • %m : Month (01…12)
    • %Y : Full year (0000…9999)
    • %F : Equivalent to %Y-%m-%d
  2. In setting time
    • date -s //Set the current time. Only root users can set the time, others can only view it.
    • date -s 20080523 //Set to 20080523, which will set the specific time to 00:00:00
    • date -s 01:01:01 //Set the specific time, and do not change the date
    • date -s "01:01:01 2008-05-23" //This can set the entire time
    • date -s "01:01:01 20080523" //This can set the entire time
    • date -s "2008-05-23 01:01:01" //This can set the entire time
    • date -s "20080523 01:01:01" //This can set the entire time
  3. Timestamp
    Time->Timestamp: date +%s
    Timestamp->time: date -d@1508749502
    The Unix epoch (Unix time, POSIX time or Unix timestamp) is the number of seconds that have passed since January 1, 1970 (midnight UTC/GMT), excluding leap seconds.

19. cal command

grammar: cal [year][month][year]
Function: Used to view calendar and other time information. If there is only one parameter, it means the year (1-9999). If there are two parameters, it means the month and year.

-3: Display the system's calendar for the previous month, current month, and next month
-j: Display the day of the year (the year is calculated by day, starting from January 1st, and the default display is the number of days in the current month in the year)
-y: Display the calendar for the current year

Example
insert image description here

20. find command

grammar: find pathname -options filename
Function: Used to find files in the file tree and make corresponding processing (possibly accessing the disk)
Common options

-name Search for files by file name
-o or

Example
insert image description here

21. grep command

grammar: grep [common options] search string files
Function: Search for a string in a file and print out the found lines
Common options

-n: Output line number by the way
-i : Ignore uppercase and lowercase differences, so uppercase and lowercase are considered the same
-v: reverse selection, that is, display the line without the 'search string' content

Example
insert image description here

22. which command

grammar:which command
Function: Used to find the location of instructions
Example
insert image description here
summary
We found that these instructions are executable programs, also files, and they are stored in the directory /user/bin
Instructions, executable programs, and commands are the same thing and are all files.

23. alias directive

grammar:alias directive alias
Function: Used to give an alias to a command
Example
insert image description here

24. zip/unzip commands

grammar: zip [compressed file.zip] [directory or file]
Function: Compress a directory or file into zip format
Installation:yum install _y zip unzip
Common options

-r: Recursive processing, processing all files and subdirectories under the specified directory together
-d: extract to the specified directory

Example
insert image description here
summary
The role of packaging: reduce the amount of data transmitted over the network and facilitate network transmission

25. tar Command

grammar
Compression: tar -czf [filename.tgz] [file]
Decompression: tar -xzf [filename.tgz]
Function: Compress a directory or file into a .tgz file
Common options

-C: Unzip to the specified directory
-v: Visualize the suppression process
-c: Create a parameter command for a compressed file
-x: parameter command to decompress a compressed file
-z: Can it be compressed by gzip?
-f: followed by the file or directory name

Exampleinsert image description here

26. bc command

grammar:bc
Function:Calculate numbers and output the results
Example
insert image description here

27. uname command

grammar:uname [options]
Function: uname is used to obtain information about the computer and operating system.
Additional Notes: uname can display basic information such as the version of the operating system used by the Linux host and the name of the hardware.
Common options

-a: Output all information in detail, including kernel name, host name, kernel version number, kernel version, hardware name, processor type, hardware platform type, operating system name
-r: Output kernel version number

Example
insert image description here

28. shutdown command

grammar: shutdown command
Function: Shutdown
Common instructions

-h: Shut down the system immediately after stopping the system services.
-r: Restart the system after stopping the service
-t sec: -t is followed by the number of seconds, which means "shut down after a few seconds"

summary
The author uses a cloud server and does not shut down the computer, otherwise he would have to go to the website to open it.