Linux does have a lot of commands, but those familiar with Linux are never bothered by too many commands. Because we just need to master the common commands, we can completely control Linux.

Next, let’s take a look at some common Linux commands.

1. File and directory operations

1. The ls command

The ls command can not only view the files contained in Linux folders, but also view file permissions (including directories, folders, file permissions) to view directory information, and so on.

The command format

Ls [option][directory name]

Commonly used parameters

  • -l: lists long data strings, including file attributes and permission data
  • -a: Lists all files, including hidden files. (often used)
  • -d: lists only the directory itself, not the file data of the directory
  • -h: lists the file capacity in a readable format (such as GB or kB)
  • -r: lists the contents of subdirectories (recursively), equal to all files in the subdirectory will be displayed

Using the instance

1. List the details of all files and directories in the home directory.

ls -a -l /home
ls -al /home
Copy the code

2. List details about all file directories in the current directory that start with “D”.

ls -l d*
Copy the code

2. The CD command

The most basic command statements, other command statements to operate, are based on using the CD command. Use to switch the current directory to dirName.

The command format

CD [Directory name]

Operating case

1. Access the system root directory from the current directory.

cd /
Copy the code

2. Go to the home/Code directory.

cd /home/Code
Copy the code

3. The PWD command

View the full path to current Working Directory.

Command format:

PWD [options]

Common parameters:

  • -p: displays the actual physical path instead of the link path
  • -l: Displays the connection path when the directory is a connection path

Operating case

1. The current path is displayed.

pwd
Copy the code

4. The mkdir command

To create a directory with the specified name, the user who creates the directory must have write permission in the current directory, and the specified directory name cannot be an existing directory in the current directory.

The command format

Mkdir [options] directory

Commonly used parameters

  • -m, –mode= mode, set permission < mode > (like chmod), not RWXRWXRWX minus umask
  • -p, –parents can be a path name. If some directories in a path do not exist, the system automatically creates the directories that do not exist. That is, multiple directories can be created at a time.
  • -v, –verbose Displays information each time a new directory is created
  • –help displays this help information and exits
  • –version Displays the version information and exits

Using the instance

1. Create an empty directory.

mkdir test
Copy the code

2. Create multiple directories recursively.

mkdir test/test1
Copy the code

3. Create a directory with permission 777.

mkdir -m 777 test2
Copy the code

4. Information is displayed when a directory is created.

mkdir -v test4
Copy the code

5. The rm command

Delete one or more files or directories in a directory. If the -r option is not used, rm will not delete the directory. If you use RM to delete a file, you can usually still restore the file.

The command format

Rm [options] file

Commonly used parameters

  • -f, –force Ignores non-existent files and never displays a prompt.
  • -i, –interactive Deletes the interface interactively
  • -r, -r, — instructing RM to recursively delete all directories and subdirectories listed in the argument.
  • -v, –verbose Displays detailed steps
  • –help displays this help information and exits
  • –version Displays the version information and exits

Using the instance

1. Delete the test. TXT file. The system prompts you whether to delete the file.

rm test.txt
Copy the code

2. Delete test. TXT forcibly.

rm -f test.txt
Copy the code

3. Delete the test subdirectory and all files in the subdirectory.

rm -r test
Copy the code

6. Rmdir commands

This command deletes one or more subdirectory entries from a directory. When you delete a directory, you must have the write permission on the parent directory.

The command format

Rmdir [options] directory

Commonly used parameters

  • P Recursively delete directory dirname. When the subdirectory is deleted and its parent directory is empty, it is also deleted. If the entire path is deleted or some of the paths remain for some reason, the system displays the corresponding information on standard output.

  • -v, –verbose Displays the command execution process

Using the instance

1. Delete the empty directory test1. The non-empty directory cannot be deleted.

rmdir test1
Copy the code

2. If the subdirectory is deleted and it becomes an empty directory, delete the subdirectory as well

rmdir -p test2 Only test2 is in the test directory
Copy the code

7. The mv command

It can be used to move a file or rename a file. When the second argument type is a file, the mv command completes the file renaming. If the second parameter is an existing directory name, multiple source files or directories can be specified. The mv command moves the specified source files to the destination directory.

The command format

Mv [Option] Source file or directory Destination file or directory

Commonly used parameters

  • -b: Back up files before overwriting them
  • -f: force Indicates the force. If the target file already exists, the system overwrites the file directly
  • -i: If the destination file already exists, the system asks whether to overwrite it
  • -u: Update the source file if the source file already exists.
  • T: – –target-directory= directory move all SOURCE arguments into directory –target-directory= directory move all SOURCE arguments into directory — Target-directory = directory move all SOURCE arguments into directory

Using the instance

1. Rename test1. TXT to test2.txt.

mv test1.txt test2.txt
Copy the code

2. Move the test1. TXT file to the test2 directory

mv test1.txt test2
Copy the code

TXT, test2. TXT, and test3. TXT to the test3 directory.

mv test1.txt test2.txt test3.txt test3
Copy the code

8. The cp command

Copies a source file to a destination file, or copies multiple source files to a destination directory.

The command format

Cp [option] Source file directory or cp [option] -t Source file of the directory

Commonly used parameters

  • -t –target-directory Specifies the target directory
  • -i — Interactive ask before overwriting (invalidate previous -n option)
  • -n –no-clobber do not overwrite existing files (invalidate the previous -i option)
  • -f –force Forcibly copies files or directories, regardless of whether the destination file or directory already exists
  • -u –update After this parameter is used, files are copied only when the modification time of the source file is updated compared with that of the destination file or when the destination file does not exist

Using the instance

1. Copy the test1. TXT file to the test1 directory

cp test1.txt test1 If the file exists, it will prompt you whether to overwrite it. If there is no direct replication
Copy the code
  1. Copy the entire directory test1 to test2
cp -a test1 test2
Copy the code

9. Touch command

Touch command parameters can change the date and time of a document or directory, including access time and change time.

The command format

Touch [options] file

Commonly used parameters

  • -A or –time=atime or –time=access or –time=use changes only the access time
  • -c or –no-create Creates no document
  • -d Uses the specified date and time instead of the current time
  • -f This parameter is ignored and only resolves compatibility problems with the BSD touch command
  • -m or –time=mtime or –time=modify Only changes the change time
  • -r Sets the date and time of the specified document or directory to the same as that of the reference document or directory. -t Uses the specified date and time instead of the current time

Using the instance

1. Create the test. TXT file that does not exist

touch test.txt
Copy the code

2. The practice for updating test. TXT is the same as that for test1.txt

touch -r test.txt test1.txt
Copy the code

10. The cat command

Used to display the contents of a file, or to concatenate several files, or to read and display the contents from standard input, often in conjunction with the redirection symbol.

The command format

Cat [options] [file]

Commonly used parameters

  • -a, –show-all is equivalent to -vet
  • -b, –number-nonblank Specifies the number of the non-blank output line
  • -e is the same thing as -ve
  • -e, –show-ends displays $at the end of each line
  • -n, –number Specifies the number of all output lines, starting from 1
  • -s, –squeeze-blank if there are more than two consecutive blank lines, it is replaced with one blank line
  • -t is equivalent to vT
  • -t, –show-tabs displays the TAB character as ^I
  • -u (ignored)
  • -v, –show-nonprinting uses ^ and M- references, except for LFD and TAB

Using the instance

1. Add line numbers to the contents of the test.log file and enter the test1.log file.

cat -n test.log  test1.log
Copy the code
  1. Display the contents of the test.log file in reverse order.
tac  test.log
Copy the code

11. The nl command

Output file content automatically add line number! The default result is a little different from cat-n. Nl can do a lot more with line numbers, including the number of digits and whether to auto-complete zeros and so on.

The command format

Nl [options] [file]

Commonly used parameters

  • -b: Specifies the line number in two ways:

  • -b a: lists the line numbers regardless of whether the line is empty (similar to cat-n).

  • -b t: If there are empty lines, do not list the line number on the empty line (default)

  • -n: Lists the line numbers in the following three ways:

  • -n ln: The row number is displayed on the left of the screen

  • -n rn: The row number is displayed on the right of its column and 0 is not added

  • -n rz: The row number is displayed on the right of its column and 0 is added

  • -w: indicates the number of digits occupied by the row number column

Using the instance

  1. List the contents of test.log in nl.
nl test.log
Copy the code
  1. List the contents of test.log in nl, with empty lines numbered.
nl -b a test.log
Copy the code

12. More orders

The function of the more command is the same as that of the cat command. The difference is that the more command can view the content of a file by page and directly skip to a line.

The command format

more [-dlfpcsu ] [-num ] [+/ pattern] [+ linenum] [file … ]

Commonly used parameters

  • +n displays from row n
  • -n Sets the screen size to N lines
  • +/ PATTERN searches for the pattern before each file display, and then displays the pattern starting after the first two lines of the string
  • -c Clear the screen from the top and display it
  • -d Press space to continue, ‘q’ to quit (Press the space key to continue, and Press the Q key to exit) is displayed to disable the ring tone function
  • -l Ignores the Ctrl+ L (page feed) character
  • -p repages a file by clearing the window rather than scrolling, similar to the -c option
  • -s Displays consecutive blank lines as one line
  • -u Deletes the underlined line in the file

Operating instructions

  • Enter: n rows down, need to be defined. The default is 1 line
  • Ctrl+F: Scroll down one screen
  • Space bar: Scroll down one screen
  • Ctrl+B: Return to previous screen
  • = : Displays the line number of the current line
  • : f: output file name and line number of the current line
  • V: Invoke the vi editor
  • ! Command: Calls the Shell and executes the command
  • Q: Exit more

Using the instance

1. Display the contents starting from line 3 in the test.log file.

more +3 test.log
Copy the code

2. Search for the first day3 string in the test.log file and display the output from the first two lines.

more +/day3 test.log
Copy the code
  1. Sets the number of lines displayed on each screen
more -5 test.log
Copy the code

13. Use less

Less is similar to more, but you can browse files at will. More can only move forward, not backward, and less does not load the entire file before viewing it.

The command format

Less [Parameter] File

Commonly used parameters

  • -b < buffer size > Sets the size of the buffer
  • -e Automatically leaves when the file is displayed
  • -f forces the opening of special files, such as peripheral codes, directories, and binaries
  • -g indicates only the last keyword searched
  • -i Ignores the case of the search
  • -m Displays the percentage of more commands
  • -n Displays the line number of each line
  • -o < filename > saves the output of less in the specified file
  • -q Does not use the warning tone
  • -s Displays a consecutive empty row
  • -s If the line is too long, the exceeding part will be discarded
  • -x < digit > Displays the TAB key as a specified digit space

Operation command

  • / String: Searches down for string
  • ? String: The ability to search up for string
  • N: Repeat the previous search (and/or? The relevant)
  • N: Repeat the previous search in reverse (and/or? The relevant)
  • B Turn back a page
  • D Turn back half a page
  • H The help page is displayed
  • Q Exits the less command
  • U Scroll forward half a page
  • Y scroll forward one row
  • The space bar scrolls a line
  • Enter to scroll a page
  • [pageDown] : Scroll down a page
  • [PageUp] : Scroll up a page

Using the instance

1. View the test.log file.

less test.log
Copy the code

14. The head of command

Head is used to display the beginning of the file to standard output. The default head command prints the first 10 lines of the corresponding file.

The command format

Head [parameter] [file]

Commonly used parameters

  • -q Hides the file name
  • -v Displays the file name
  • -c< bytes > Displays the number of bytes
  • -n< number of rows > Number of rows to be displayed

Using the instance

1. Display the first five lines of the test.log file

head -n 5 test.log
Copy the code

2. Display the first 20 bytes of the test.log file

head -c 20 test.log
Copy the code

15. The tail command

Displays the content at the end of a specified file. If no file is specified, the file is processed as input information. View log files frequently.

The command format

Tail [required parameters] [Selection parameters] [file]

Commonly used parameters

  • -f Indicates the reading cycle
  • -q Indicates that processing information is not displayed
  • -v Displays detailed processing information
  • -c< Number > Number of bytes to display
  • -n< number of rows > Number of rows to be displayed
  • –pid= Pid is used with -f to terminate the process after the process ID,PID, dies.
  • -q, –quiet, –silent never prints the header of the given filename
  • -s, –sleep-interval= s This parameter is used with -f to sleep for seconds at each repeated interval

Using the instance

1. Display the last five lines of the test.log file.

tail -n 5 test.log
Copy the code

2. View the file content

tail -f test.log
Copy the code

Two, file search

16. Which command

The which directive searches for the location of a system command in the PATH specified by the PATH variable and returns the first search result.

The command format

Which executable file name

Commonly used parameters

  • -n Specifies the length of the file name. The specified length must be longer than or equal to the longest file name in all files
  • The -p parameter is the same as the -n parameter, but contains the file path
  • -w Specifies the width of the output field
  • -v Displays the version information

Using the instance

1. Search for the file and display the command path.

which pwd
Copy the code
  1. Use which to find which
which which
Copy the code

17. Whereis command

The whereis command locates executables, source code files, and help files in the file system.

The command format

Whereis [-bmsu] [BMS directory name -f] File name

Commonly used parameters

  • -b Locates the executable file
  • -m Locate help files
  • -s Locates the source code file
  • -u Searches for all files except executable files, source files, and help files in the default path
  • -b Specifies the path to search for executable files
  • -m Specifies the path to search for help files
  • -s Specifies the path to search for source code files

Using the instance

1. Find all files related to SVN files.

whereis svn
Copy the code

2. Search only binary files.

whereis -b svn
Copy the code

18. The locate command

You can quickly search for specific files in the file system.

The command format

Locate [select parameter] [style]

Commonly used parameters

  • -e will be excluded from the search.
  • Minus 1 if it’s 1. Start safe mode. In safe mode, users do not see files that are not viewable by permissions. This starts to slow down, because Locate must go to the actual file system to get the permissions for the file.
  • -f excludes specific file systems, such as files from the PROC file system that we have not decided to place in a database.
  • -q Indicates the quiet mode. No error message is displayed.
  • -n Displays a maximum of N outputs.
  • -r uses the normal expression for the search condition.
  • -o Specifies the name of the data store.
  • -d Specifies the database path

Using the instance

1. Find all files related to PWD.

locate pwd
Copy the code
  1. Search the etc directory for all files starting with m.
locate /etc/m
Copy the code

19. The find command

It traverses down the file hierarchy, matches qualified files, and performs corresponding operations.

The command format

Find [option] [search path] [expression]

Commonly used parameters

  • The -print find command outputs the matched files to standard output
  • -exec find executes the given parameter on the matched files
  • Shell command
  • -name Searches for files by name
  • -type Searches for files of a certain type

Using the instance

1. Print the file directory list of the current directory.

find . -print
Copy the code

2. Print all file names in the current directory that do not end in. TXT.

find . ! -name "*.txt"
Copy the code

3. Print all the PHP files with permission 777 in the current directory.

find . -type f -name "*.php" -perm 777
Copy the code

4. Find all PHP files in the current directory and display their details.

find . -name "*.php" -exec ls -l {} \;
Copy the code

5. Search for all C code files in the current directory and count the total number of lines.

find . -type f -name "*.c" | xargs wc -l
Copy the code

The xargs command takes input from standard input and converts it to a specific parameter list. Command format: command | xargs [option] [command] xargs command should follow after the pipeline operator, because it to standard input as the main source of data flow. Commonly used parameters

  • -n Specifies the maximum number of parameters in each line
  • -d Specifies the delimiter

File packaging upload and download

20. The tar command

Used to compress and decompress files. Tar itself does not have compression. It is implemented by calling the compression function.

The command format

Tar [required parameters] [Select parameters] [file]

Commonly used parameters

Required parameters:

  • -a Adds compressed files to existing compressed files
  • -b Sets the block size
  • -c Creates a new compressed file
  • -d Records file differences
  • -r Adds a file to a compressed file
  • -u adds changes and existing files to existing compressed files
  • -x Extracts files from compressed files
  • -t Displays the contents of compressed files
  • -z Supports gzip decompression
  • -j Supports bzip2 decompression
  • -z Supports the compress decompression files
  • -v Displays the operation process
  • -l Sets the file system boundary
  • -k Keeps the original file
  • -m Indicates that the file is not overwritten
  • -w Verifies the correctness of the compressed file

Optional parameters:

  • -b Sets the number of blocks
  • -c To switch to the specified directory
  • -f Specifies the compressed file
  • –help Displays help information
  • –version Displays version information

Using the instance

1. Pack all files into a tar package.

tar -cvf test.tar test.log    # Pack only, not compress!

tar -zcvf test.tar.gz test.log  After packaging, compress with gzip

tar -zcvf test.tar.bz2 test.log Once packed, compress with bzip2
Copy the code

2. Decompress the tar package

tar -zxvf test.tar.gz
Copy the code

21. The gzip command

Use a wide range of compressors to compress files and add the extension “.gz” to their names.

The command format

Gzip [parameters] [file or directory]

Commonly used parameters

  • -A or — ASCII Uses the ASCII text mode.
  • -c or –stdout or –to-stdout prints the compressed file to the standard output device without altering the original file.
  • -d or –decompress or —-uncompress Uncompresses compressed files.
  • -for –force Forcibly compresses files. Regardless of whether a file name or hardlink exists and whether the file is a symbolic link.
  • -h or –help Online help.

Using the instance

1. Compress each file in the test1 directory into a. Gz file.

test6 $ gzip *
Copy the code

4. File permission setting

22. The chmod command

Change the access permission of a file or directory in the Linux system.

The command format

chmod [-cfvR] [–help] [–version] mode file

Commonly used parameters

Required parameters:

  • -c Reports processing information when changes occur
  • -f No error information is displayed
  • -r Processes all files in the specified directory and its subdirectories
  • -v Displays detailed processing information when running
  • Select parameters:
  • –reference=< directory or file > set to have the same permissions as the specified directory or file
  • –version Displays version information
  • < Permission Scope >+< Permission Settings > Grants specified permissions to directories or files within the permission scope
  • < Permission Scope >-< Permission Settings > Deletes specified permissions for directories or files in the permission scope
  • < Permission Scope >=< Permission Settings > Sets the permission of directories or files within the permission scope to the specified value

Scope of Authority:

  • U: indicates the current user of a directory or file
  • G: current group of directories or files
  • O: a user or group other than the current user or group of a directory or file
  • A: All users and groups

Permission Code:

  • R: indicates the read permission. The value is 4
  • W: indicates the write permission. The value is 2
  • X: execution permission, represented by the number 1
  • – : indicates the deletion permission. The value is 0

Using the instance

1. Add the execution permission to all user groups

chmod a+x test.log
Copy the code
  1. Example Delete the executable permission of all users
chmod a-x test.log
Copy the code

23. The CHGRP command

You can change the owning group of a file or directory by using the group name or group id.

The command format

CHGRP [option] [group] [file]

Commonly used parameters

Required parameters:

  • -c Displays debugging information when changes occur
  • -f Displays no error information
  • -r Processes all files in the specified directory and its subdirectories
  • -v Displays detailed processing information when running
  • Dereference applies to the pointing of a symbolic link, not to the symbolic link itself
  • –no-dereference works on the symbolic link itself

Select parameters:

  • — Reference =< file or directory >
  • –help Displays help information
  • –version Displays version information

Using the instance

1. Change the group properties of the file

chgrp -v bin test.log
Copy the code

2. Change the group properties of the test1.log file so that the group properties of the test1.log file are the same as those of the reference file test.log

chgrp --reference=test.log test1.log
Copy the code

24. The chown command

Chown changes the owner and group of a file.

The command format

Chown [option] [owner] [:[group]] file

Commonly used parameters

Required parameters:

  • -c Displays information about the changed part
  • -f Ignores error information
  • -h Restores symbolic links
  • -r Processes all files in the specified directory and its subdirectories
  • -v Displays detailed processing information
  • – Deference works on the pointing of symbolic links, not the link file itself

Select parameters:

  • –reference=< directory or file > Set the specified directory/file as the reference file/directory of the operation to the same owner and group of the reference file/directory
  • –from=< Current user: Current group > Changes only when the current user and group are the same as the specified user and group
  • –help Displays help information
  • –version Displays version information

Using the instance

1. Change the owner and group

chown mail:mail test.log
Copy the code

Five, disk storage

25. The df command

Displays the available space for files on the specified disk.

The command format

Df [options] [file]

Commonly used parameters

Required parameters:

  • -a List of all file systems
  • -h Facilitates reading
  • -h equals “-h”, but 1K=1000 instead of 1K=1024
  • -i Displays inode information
  • The -k block is 1024 bytes
  • -l Displays only local file systems
  • The -m block is 1048576 bytes
  • –no-sync Ignores the sync command
  • -p The output format is POSIX
  • –sync Before obtaining disk information, run the sync command
  • -t Indicates the type of the file system

Select parameters:

  • –block-size=< block size > Specifies the block size
  • -t< File system type > Displays only information about disks of the selected file system
  • -x< File system type > Does not display the disk information of the selected file system
  • –help Displays help information
  • –version Displays version information

Using the instance

1. Display the usage of the specified disk

df -t ext3
Copy the code

26. The du command

Displays the disk usage for each file and directory.

The command format

Du [options] [file]

Commonly used parameters

  • -a or -all Displays the size of individual files in a directory.
  • -b or -bytes Indicates the size of a directory or file, in bytes. — -c or –total Displays the total of all directories or files in addition to the size of individual directories or files.
  • -k or –kilobytes are output in KB(1024bytes).
  • -m or –megabytes are output in MB.
  • -s or — Summarize displays only the sum and lists only the last sum.
  • -h or –human-readable will improve the readability of the information in units of K, M, or G.
  • -x or –one-file-xystem is based on the file system at the beginning of processing. If a different file system directory is encountered, it is skipped.
  • -l < symbolic link > or –dereference< symbolic link > displays the source file size of the symbolic link specified in the option.
  • -s or –separate-dirs Displays the size of individual directories, excluding the size of their subdirectories.
  • -x < file > or –exclude-from=< file > Specifies a directory or file in < file >.
  • –exclude=< directory or file > Ignores the specified directory or file.
  • -d or –dereference-args displays the size of the source file for the specified symbolic link.
  • -h or –si has the same parameters as -h, but K, M, and G are converted to 1000 units.
  • -l or –count-links Recomputes the hardware link file.

Using the instance

1. The space occupied by the specified directory or file is displayed

du test # directory
du test.log # file
Copy the code

Performance monitoring and optimization commands

27. The top command

This section describes how to view information about processes that are running, including process IDS, memory usage, and CPU usage.

The command format

Top [parameter]

Common parameters

  • – b batch
  • -c Displays complete governance commands
  • -i Ignores the failure process
  • -s Indicates the confidential mode
  • -s Indicates the accumulation mode
  • -i< time > Sets the interval
  • -u< User name > Specifies the user name
  • -p< process number > Specifies the process
  • -n< times > Indicates the number of times that the loop is displayed

Using the instance

  1. Process information is displayed.
top
Copy the code

28. The free command

Displays the used and free memory of the system, including physical memory, interactive area memory (SWAP), and kernel buffer memory.

The command format

Free [parameter]

Common parameters

  • -b Displays the memory usage in bytes
  • -k Displays the memory usage in KB
  • -m Displays the memory usage in MB
  • -g Displays the memory usage in GB
  • -o Does not display the buffer adjustment column
  • -s< interval seconds > Continuously observe memory usage
  • -t Displays the total memory column.
  • -v Displays the version information.

Using the instance

1. The memory information is displayed.

free
free -g # In GB
free -m # in MB
Copy the code

29. vmstat

Used to display virtual memory information.

The command format

  • vmstat [-a] [-n] [-S unit] [delay [ count]]
  • vmstat [-s] [-n] [-S unit]
  • vmstat [-m] [-n] [delay [ count]]
  • vmstat [-d] [-n] [delay [ count]]
  • vmstat [-p disk partition] [-n] [delay [ count]]
  • vmstat [-f]
  • vmstat [-V]

Common parameters

  • -a: Displays active and inactive memory
  • -f: Displays the number of forks since system startup
  • -m: Displays slabinfo
  • -n: Displays the field name only once at the beginning
  • -s: displays memory-related statistics and the number of system activities
  • Delay: refresh interval. If not specified, only one result is displayed
  • Count: refresh times. If no refresh count is specified, but the refresh interval is specified, the refresh count is infinite
  • -d: displays disk statistics
  • -p: displays statistics about a specified disk partition
  • -s: displays information in a specified unit. The parameter k, k, m, and m represent 1000, 1024, 1000000, and 1048576 bytes respectively. Default unit: K (1024 bytes)

Using the instance

1. Display active and inactive memory.

vmstat -a 5 5 Sample 5 times in 5 seconds
Copy the code

30. Lostat command

Iostat allows you to view the activity and load information of cpus, network adapters, TTY devices, disks, and CD-RoMs.

The command format

Iostat [parameter] [time] [number of times]

Common parameters

  • -c Displays the CPU usage
  • -d Displays the disk usage
  • -k Displays the value in KB
  • -m Displays information in the unit of M
  • -n Displays disk array (LVM) information
  • -n Displays NFS usage
  • -p[disk] Displays information about disks and partitions
  • -t Displays information about the terminal and CPU
  • -x Displays detailed information

Using the instance

1. Display all information periodically.

iostat 2 3 # Refresh the display every 2 seconds and display 3 times
Copy the code

31. The lsof command

View the file your process opened, the process that opened the file, and the port (TCP, UDP) the process opened.

The command format

Lsof [parameter] [file]

Common parameters

  • -a Lists the processes in which open files exist
  • -c< process name > lists the files opened by the specified process
  • -g Lists details about processes with GID numbers
  • -d< file number > Lists the processes that occupy the file number
  • +d< directory > lists open files in the directory
  • +D< directory > recursively lists open files in the directory
  • -n< directory > Lists the files that use NFS
  • -i< Condition > Lists the processes that meet the conditions. (4, 6, protocol: port, @IP)
  • -p< process number > lists the files opened by the specified process number
  • -u Lists details about processes with the UID number

Using the instance

1. See who is using the bash file, that is, find the process associated with a file.

lsof /bin/bash
Copy the code

Network commands

32. The ipconfig command

Using the ifconfig command, you can view and configure network devices.

The command format

Ifconfig [network device] [Parameters]

Common parameters

  • Up Enables the specified network device or nic
  • Down Disables the specified network device or nic.
  • Arp Indicates whether a network adapter supports ARP
  • -promisc Indicates whether the interface supports the promiscuous mode. If this parameter is selected, the interface receives all packets sent to it from the network
  • – allMulti Indicates whether the multicast mode is supported. If this parameter is selected, the network adapter receives all multicast packets on the network
  • -a Displays information about all interfaces
  • -s Displays summary information (similar to netstat -i)
  • Add Configures an IPv6 address for the specified nic
  • Del Deletes the IPv6 address of the specified nic

Using the instance

1. Enable or disable the specified nic

ifconfig eth0 up
ifconfig eth0 down
Copy the code

2. Run ifconfig to change the MAC address

ifconfig eth0 hw ether 00:AA:BB:CC:DD:EE
Copy the code

33. The route command

The Route command is used to manipulate the kernel BASED IP routing table. Its main purpose is to create a static Route to specify a host or network through a network interface, such as eth0.

The command format

route [-f] [-p] [Command [Destination] [mask Netmask] [Gateway] [metric Metric]] [if Interface]]

Common parameters

  • -c Displays more information
  • -n Does not parse the name
  • -v Displays detailed processing information
  • -f Displays sending information
  • -c Displays the route cache
  • -f Clears the routing table of all gateway entries.
  • -p makes the route permanent when used together with the add command.
  • Add: Adds a new route.
  • Del: Deletes a route.
  • Net: indicates that the destination address is a network.
  • -host: indicates that the destination ADDRESS is a host.
  • Netmask: When adding a network route, you need to use a network mask.
  • Gw: routing data packets through the gateway. Note that the gateway you specify must be accessible.
  • Metric: Sets the hop count of the route.
  • Command specifies the Command you want to run (Add/Change/Delete/Print).
  • Destination Specifies the network Destination of the route.

Using the instance

1. Display the current route

route 
route -n
Copy the code

2. Add a gateway or set a gateway

Route add-net 224.0.0.0 netmask 240.0.0.0 dev eth0Copy the code

34. The ping command

Determine the status of the network and each external host; Tracking and isolating hardware and software problems; Test, evaluate, and manage the network.

The command format

Ping [parameter] [Host name or IP address]

Common parameters

  • -d Uses the SO_DEBUG function of the Socket
  • -f Limit detection. Send network packets to a machine in large numbers and quickly to see how it responds
  • -n Outputs only numeric values
  • -q displays no information about the sent packet, only the final result
  • -r Directly sends the packets to the remote host, ignoring the common Routing Table. It is usually to check whether there is a problem with the local network interface
  • -r Records the routing process
  • -v Displays the detailed command execution process
  • -c Number: stops after sending a specified number of packets
  • -i seconds: set a few seconds to send packets to a network between a machine, the default value is sent a second time -i network interface: send packets using the specified network interface – l front loading: set before send out the required information, leading a sample packet – p style: set the fill the template style – s bytes of data packets: Specifies the number of bytes of data to be sent. The default value is 56, plus an 8-byte ICMP header, making a total of 64ICMP data bytes. -t TTL value: sets the TTL size

Using the instance

  1. Ping gateway
Ping - 192.168.120.1 bCopy the code

35. The traceroute command

Allows you to track the routing of network packets. The default packet size is 40Bytes, which can be set by the user.

The command format

Traceroute [parameters] [host]

Common parameters

  • -d Uses the socket-level debugging function
  • -f Sets the TTL of the first detection packet
  • -f Set not to break
  • -g Sets a maximum of eight source route gateways
  • -i Sends packets using the specified network interface
  • -i Uses ICMP response instead of UDP data information
  • -m Sets the TTL of the maximum TTL
  • -n Indicates the IP address instead of the host name
  • -p Sets the UDP communication port
  • -r Directly sends the packets to the remote host, ignoring the common Routing Table
  • -s Sets the IP address for sending data packets from the local host
  • -t Sets the TOS value for detecting packets
  • -v Displays the detailed command execution process
  • -w Sets the waiting time for the return from the remote host
  • -x Enables or disables the correctness check of packets

Using the instance

1. Traceroute is simple and most commonly used

traceroute www.baidu.com
Copy the code
  1. Hop count is set
traceroute -m 10 www.baidu.com
Copy the code

36. net stat command

The display displays statistics related to IP, TCP, UDP, and ICMP protocols, and is generally used to check the network connection of each port on the local host.

The command format

Netstat [-accefghilMnNoprSTUVVwx] [-a < Network type >] [– IP]

Common parameters

  • -a or -all Displays all sockets
  • -A< Network type > or – < Network type > lists the related addresses in the connection of the network type
  • -c or -continuous Lists the network status continuously
  • -c or – cache Displays the cache information configured by the router
  • -e or -extend Displays other network information
  • -f or -fib Displays the FIB
  • -g or -groups Displays the member list of multiple broadcast groups
  • -h or -help Online help
  • -i or -interfaces Displays the network interface information form
  • -l or – listening Displays the Socket of the monitored server
  • -m or -masquerade displays disguised network connections
  • -n or -numeric uses the IP address directly, not through the DNS
  • -n or -netlink or -symbolic Indicates the symbolic connection name of the network hardware peripheral device
  • -o or – timers Displays timers
  • -p or -programs displays the identifiers and names of the programs using the Socket
  • -r or -route Displays the Routing Table
  • -s or -statistice Displays network work information statistics
  • -t or -tcp Displays the connection status of TCP
  • -u or – udp Displays the connection status of udp transport protocol
  • -v or – verbose Displays the command execution process
  • -v or -version Displays the version information
  • -w or – raw Displays the connection status of the RAW transmission protocol
  • -x or -unix This parameter has the same effect as the specified -a Unix parameter
  • – IP or – inet This parameter has the same effect as the -a inet parameter

Using the instance

  1. List all ports
netstat -a
Copy the code

37. The Telnet command

Run the Telnet command to start the terminal phase and log in to the remote host.

The command format

Telnet [parameters] [host]

Common parameters

  • -8 Allows the use of 8-bit character data, including input and output
  • -a Attempts to automatically log in to the remote system
  • -b< Host alias > Specifies the name of the remote host using the alias
  • -c does not read. Telnetrc files in the user’s exclusive directory
  • -d Enables the debugging mode
  • -e< Detach character > Sets detach character
  • -e Filters out detached characters
  • -f This parameter has the same effect as the “-f” parameter

Using the instance

1. The remote server cannot be accessed

Telnet 192.168.120.206Copy the code

8. Other commands

38. The ln command

Create a synchronous link for a file in another location. When we need in different directories, use the same file, we don’t need to put in every need directory a file must be the same, we just in a fixed directory, put the file, and then in the other directory with the ln command links (link) it can, need not repeat take up disk space.

The command format

Ln [parameter] [source file or directory] [destination file or directory]

Commonly used parameters

Required parameters:

  • -b Deletes and overwrites the previously established link
  • -d Allows super users to create hard links for directories
  • -f This parameter is mandatory
  • -i Indicates the interactive mode. If the file exists, the user is prompted whether to overwrite it
  • -n Treats symbolic links as general directories
  • -S soft link (symbolic link)
  • -v Displays detailed processing procedures

Select parameters:

  • -s “-s < suffix backup string >” or “–suffix=< suffix backup string >”
  • -v -v < Backup mode > or –version-control=< Backup mode >

Using the instance

1. Create a soft link linktest for the test.log file.

ln -s test.log linktest
Copy the code

2. Create a hard link lntest for test.log.

ln test.log lntest
Copy the code

39. The diff command

Compare the contents of individual files or directories.

The command format

Diff [parameter] [file 1 or directory 1] [file 2 or directory 2]

Commonly used parameters

  • -c In context mode, all text is displayed and differences are marked
  • -u Displays file contents in the unified mode
  • -A will only compare text files line by line
  • -n During directory comparison, if file A exists Only in A certain directory, Only in directory is displayed by default. With the -n argument, diff compares file A to A blank file
  • -r Compares files in directories recursively

Using the instance

TXT and test2. TXT are displayed.

diff test1.txt test2.txt
Copy the code

40. The grep command

A powerful text search tool that uses regular expressions to search text and print matching lines.

The command format

grep [option] pattern file

Commonly used parameters

  • -c Counts the number of times the ‘search string’ (that is, pattern) is found
  • -i ignores case differences, so case are considered the same
  • -n Displays the line number
  • -v Displays the lines that do not match
  • -r recursive search
  • –color=auto Color the keyword part found

Using the instance

1. Remove the root lines from the /etc/passwd file and color the keywords.

grep "root" /etc/passwd --color=auto
cat /etc/passwd | grep "root" --color=auto
Copy the code

2. Remove the lines that do not contain root and nologin from the /etc/passwd file.

grep -v "root" /etc/passwd | grep -v "nologin"
Copy the code

41. The wc command

Displays the number of lines, words, and bytes contained in the file.

The command format

Wc [option] [file]

Commonly used parameters

  • -c Indicates the number of bytes
  • -l Indicates the number of rows
  • -m indicates the number of characters. This flag cannot be used together with the -c flag
  • -w Counts the number of words. A word is defined as a string separated by whitespace, tabs, or newline characters
  • -l Prints the length of the longest line

Using the instance

1. Count the number of bytes, lines, and characters in a file.

wc -c test.txt
wc -l test.txt
wc -m test.txt
Copy the code

2. Count the number of bytes, lines, and characters in a file. Print only numbers instead of file names.

cat test.txt | wc -c
cat test.txt | wc -l
cat test.txt | wc -m
Copy the code

42. The ps command

Displays the status of the current process.

The command format

Ps/parameters

Commonly used parameters

  • A Displays all processes
  • -a Displays all programs on the same terminal
  • -a Displays all processes
  • C Displays the process name
  • -n Indicates reverse selection
  • – e is equal to the “-a”
  • E Displays environment variables
  • F Displays relationships between programs
  • -h Displays the tree structure
  • R Displays the process of the current terminal
  • T Displays all programs on the current terminal
  • U Specifies all processes of the user
  • -au Displays detailed information
  • -aux displays all trips that include other users
  • -c < command > Lists the status of the specified command
  • –lines< lines > The number of lines displayed per page
  • –width< number of characters > Number of characters to display per page

Using the instance

1. Display information about all processes.

ps -A
Copy the code
  1. Displays information about a specified user.
ps -u root
Copy the code
  1. Displays all process information, along with the command line.
ps -ef
Copy the code

43. Watch command

You can output the command output to the standard output device, which is used to periodically execute commands or periodically execute commands.

The command format

Watch [parameter] [command]

Commonly used parameters

  • -n or –interval watch Runs the program every 2 seconds by default. You can use -n or -interval to specify the interval.
  • -d or –differences The -d or –differences option watch will highlight the changed areas. The -d=cumulative option will highlight any changes (whether or not the most recent changes were made).
  • -t or -no-title will turn off the watch command at the top interval, command, current time output.
  • -h, –help View the help document

Using the instance

1. Highlight changes in the number of links every second

watch -n 1 -d netstat -ant
Copy the code

2. Highlight changes in the number of HTTP links every second

watch -n 1 -d 'pstree|grep http'
Copy the code

44. The at command

A specified task can be executed only once at a specified time. (AtD process needs to be started)

The command format

At [parameter] [time]

Commonly used parameters

  • -m Sends an email to the user after the specified task is completed, even if there is no standard output
  • -i Indicates the alias of ATQ
  • -d Alias of atRM
  • -v Displays the time when the task will be executed
  • -c Prints the task content to standard output
  • -v Displays the version information
  • -q< queue > uses the specified queue
  • -f< file > reads the task from the specified file instead of from standard input
  • -t< time parameter > Submits the task to be run as a time parameter

Using the instance

1.3 Days later, the /bin/ls command is executed at 5:00 p.m

at 5pm+3 days 
at> /bin/ls
at> <EOT>
Copy the code

45. The crontab command

Execute specified system instructions or shell scripts at fixed intervals. The unit of time interval can be any combination of minutes, hours, days, months, weeks or more. (Crond service must be enabled)

The command format

Crontab [-u user] file or

crontab [-u user] [ -e | -l | -r ]

Commonly used parameters

  • -u user: indicates the crontab service of a user. For example, -u ixdba indicates the crontab service of user IXdba. This parameter is usually run by the root user.
  • File: File is the name of the command file, indicating that file is used as the crontab task list file and crontab is loaded. If this file is not specified on the command line, the crontab command will accept commands typed on standard input (keyboard) and load them into crontab.
  • -e: edits the crontab file of a user. If no user is specified, the crontab file of the current user is edited.
  • -l: displays the crontab file of a user. If no user is specified, the crontab file of the current user is displayed.
  • -r: deletes the crontab file of a user from the /var/spool/cron directory. If no user is specified, the crontab file of the current user is deleted by default.
  • -i: prompts confirmation when deleting the crontab file of the user.

Using the instance

1. List the crontab file.

crontab -l
Copy the code

2. Edit the crontab file.

crontab -e
Copy the code

Crontab task instance

1. Run the command every minute

* * * * * command
Copy the code

2. Each hour is executed at the third and 15th minutes

3,15 * * * * command
Copy the code

3. The third and 15th minutes from 8 a.m. to 11 a.m

3,15, 8-11 * * *command
Copy the code

Linux

  • A collection of 55 Linux tutorials and hands-on projects
  • Linux operations and DevOps
  • How to learn Linux command line, what experience ideas and methods?
  • Linux China open Source Community