A detailed list of commonly used Linux commands

1. View directories and files: ls

The ls command is used to display directory listings or file information. Commonly used options are as follows:

  • -l displays detailed information including file type, permissions, owner, size, etc.
  • -a displays all files, including hidden files and current directory (.) and parent directories (…)
  • -h Display file size in human-readable format
  • -t Sort by modification time
  • -r Sort in reverse order
  • -R recursively displays all files and subdirectories in the directory
  • -d only displays the directory itself, not the files in the directory
    For example, use the ls -l command to display detailed information about all files in the current directory, use the ls -a command All files in the current directory can be displayed, including hidden files and the current directory (.) and upper-level directories (…).

2. Switch directory: cd

  • cd /home to enter the ‘/home’ directory
  • cd … returns to the previous directory
  • cd …/… returns to the directory two levels above

3. Display the current directory: pwd

The pwd command is used to display the path of the current working directory. In a Linux system, each user has a current working directory, which is the user's default directory when executing commands. Use the pwd command to view the current directory path, which is convenient for users to operate and manage files.

4. Create an empty file: touch

The touch command in linux is used to create a file or update the timestamp of a file. The syntax format is as follows:

touch [选项] 文件名

Common options:

  • -a Update file access time
  • -c do not create the file if it does not exist
  • -m updates the modification time of the file
  • -d specifies the time in the format [[CC]YY]MMDDhhmm[.ss]
    For example:
touch test.txt

This command will create a file named test.txt and update its timestamp if the file already exists.

5. Create directory: mkdir

mkdir command is used to create new directories in Linux.
Usage example:

  • Create a directory named "newfolder": mkdir newfolder
  • Create a multi-level directory: mkdir -p dir1/dir2/dir3
  • Create the directory and grant permissions: mkdir -m 755 newfolder
    Parameter description:
  • -p: Create directories recursively. If the parent directory does not exist, it will also be created.
  • -m: Set directory permissions.

6. View the file content: cat

catcommand is one of the commonly used text file editing commands in Linux systems. Its common uses are as follows:

  1. View the content of the file: Use the cat command in the terminal, followed by the file name to view the content of the file, such as cat file.txt.
  2. Merge files: Use the cat command to merge multiple files into one file. The command format is cat file1.txt file2.txt > file3.txt.
  3. Create a new file: Use the cat command in the terminal, followed by the file name. If the file does not exist, a new file will be created, and you can enter text in the file.
  4. Append text to the file: Use the cat command to append text to the file. The command format is cat >> file1.txt.
    In general, the cat command is very powerful and frequently used.

7. View the file content in pages: more

The more command is a tool used to view the contents of files in Linux systems. It can view files by pages. During the viewing process, you can use the space bar to turn pages or the b key to turn pages backward. You can use / and ? to find text.
Common syntax: more filename
For example: more test.txt
Common options:

  • -d: Display prompt information when turning pages
  • -f: Interpret newlines as spaces
  • -l: Make the more command think the file is paged
  • +n: Display the file starting from line n
    For detailed usage, please refer to the man manual in the Linux system.

8. View the content at the end of the file: tail

The tail command is a command used in Linux systems to view the content at the end of a file. The syntax format is: tail [option] [file name]
The -n option can specify the number of lines to display. For example: tail -n 10 file.txt means to display the last 10 lines of the file.txt file.
The -f option can display the end of the file in real time until the user manually stops it. For example: tail -f file.txt.
By default, the tail command displays the last 10 lines of the file.
Example:
tail -n 5 /var/log/syslog #View the last 5 lines of /var/log/syslog
tail -f /var/log/syslog #View the /var/log/syslog file in real time

9. Copy: cp

The cp command in Linux is used to copy files or folders. The syntax format is as follows:

cp [选项] 源文件目录 目标文件目录

Common options include:

  • -r: Recursive copy, if the source file is a folder, all files and subfolders in it will be copied
  • -p: Preserve the attributes of the source file (permissions, time, etc.)
  • -f: Force overwriting of target file
  • -u: Copy only when the source file is newer than the target file
    For example:
  • Copy the file file.txt to the directory /home/user/Documents with the command cp file.txt /home/user/Documents.
  • Recursively copy the folder /home/user/Pictures to the directory /mnt/backup while retaining the attributes of the source file. The command is cp -rp /home/user/Pictures /mnt/backup.

10. Cut or rename: mv

The mv command in Linux is used to move or rename files and directories. The syntax is: mv [options] source file directory target file directory.
Common options:
-i: Ask the user before overwriting the target file.
-f: Force overwriting of the target file.
-u: Overwrite the target file only if the target file does not exist or the source file is newer than the target file.
Example:
-Move file file1 to directory dir1: mv file1 dir1
-Rename directory dir1 to dir2 :mv dir1 dir2
- Rename file file1 to file2: mv file1 file2

11. Delete: rm

The rm command is a command used to delete files or directories in Linux.
Usage: rm [option] file or directory
Common options:
-i: ask for confirmation before deletion
-r: Recursively delete directories
-f: Forced deletion without asking for confirmation
For example:

  • rm file.txt: delete file file.txt
  • rm -r dir : delete directory dir and all its contents
  • rm -i file.txt: Will ask for confirmation before deleting file file.txt
    Note: The rm command will not move files or directories to the trash or recycle bin, and cannot be deleted after deletion. recover. Please use with caution.

12. Search for files: find

findcommand is a very commonly used command in Linux systems and is used to find files and directories. Its basic syntax is as follows:

find [路径] [表达式]

Among them, the path specifies the starting location of the file or directory to be searched, and the expression is used to specify the search conditions.
Here are some common uses:

  1. Find a file with a specified name:
find /path/to/search -name "filename"
  1. Find files of a specified type:
find /path/to/search -type f
  1. Find files of a specified size:
find /path/to/search -size +10M
  1. Find files modified within a specified time range:
find /path/to/search -mtime -7
  1. Find files belonging to a specified owner or group:
find /path/to/search -user username
find /path/to/search -group groupname
  1. Find files containing specified text:
find /path/to/search -type f -exec grep "text" {} \;

The above are only some common uses of the find command. For more uses, you can use the man find command to view the help document or find information on the Internet.

13. Display or configure network devices: ifconfig

The ifconfig command is used in Linux to configure and display network interface information.
Usage: ifconfig [network interface] [options]
Common options:

  • up: Enable the specified network interface.
  • down: Disable the specified network interface.
  • inet: Display IPv4 address.
  • inet6: Display IPv6 address.
  • -a: Display information about all network interfaces, including disabled interfaces.
  • -s: Display summary information of the network interface in a concise format.
    Example:
  • Display all network interface information: ifconfig -a
  • Enable network interface eth0: ifconfig eth0 up
  • Disable network interface eth0: ifconfig eth0 down
  • Display the IPv4 address of network interface eth0: ifconfig eth0 inet
  • Display summary information for network interface eth0: ifconfig eth0 -s

14. Display network related information: netstat

The netstat command is used in Linux to display information about network connections, routing tables, and network interfaces.
Usage: netstat [options]
Common options:

  • -a: Display all active connections and listening ports.
  • -t: Show only TCP connections.
  • -u: Show only UDP connections.
  • -n: Display IP address and port number in numerical form.
  • -p: Display process information associated with the connection.
  • -r: Display routing table information.
    Example:
  • Show all active connections and listening ports: netstat -a
  • Show all TCP connections: netstat -t
  • Show all UDP connections: netstat -u
  • Display all routing table information: netstat -r
  • Display process information associated with the connection: netstat -p
    You can view the complete help document of netstat through the man netstat command for more detailed information Instructions for use and options explained.

15. Display process status: ps

The ps command is a command used to view process information in Linux systems. Commonly used parameters include:

  • -ef: Display all process information
  • -aux: Display process information for all users
  • -u [username]: Display process information of the specified user
  • -p [PID]: Display information about the specified process
    For example:
$ ps -ef

Details of all processes will be listed.

$ ps -u root

Process information of all root users will be listed.

$ ps -p 1234

The process information with pid 1234 will be listed.

16. Check the directory usage: du

The du command is a commonly used command in Linux, used to view the disk usage of files or directories. Its basic usage is:
du [option] [file or directory]
Among them, the option can be:
-h : Display the file size in human-readable form, such as 1K, 2M, etc.
-s: Only displays the total size, not the size of each file or directory.
-c: Display both the total size and the size of each file or directory.
-a: Display the size of all files and directories, including hidden files and directories.
-b: Display the file size in bytes.
For example, to view the size of all files and directories in the current directory, you can use the command:
du -h .
where "." indicates the current directory. If you want to see the size of a specific file or directory, you can pass it as a parameter to the du command, for example:
du -h /home/user/Documents
This will display the size of all files and subdirectories under the "/home/user/Documents" directory.

17. Check disk space usage: df

The df command is used to display the disk space usage of the file system. It lists information such as the file system's name, total capacity, used capacity, free capacity, usage, and mount points. You can use the df command to check the disk space usage so that you can clean or expand the disk in time.
The df -h command is used to display the disk space usage of a file system in a human-readable manner. It will display the capacity in units such as K, M or G, making it easier to understand.
The specific usage is as follows:

df -h [选项] [文件系统]

Common options:

  • -h: Displays disk space usage in human-readable form.
  • -t <文件系统类型>: Only display file systems of the specified type.
  • -x <文件系统类型>: Exclude file systems of the specified type.
  • -a: Display all file systems, including special file systems (such as proc, sysfs, etc.).
  • -l: Only local file systems are displayed, network file systems (NFS, etc.) are not included.
  • -i: Display inode usage instead of disk space usage.
    Example:
df -h
df -h -t ext4
df -h -x tmpfs
df -h -l

The above is the basic usage of the df -h command. You can use different options to display or exclude the disk space usage of specific types of file systems as needed.

18. Display the current process information of the system: top

The top command is a command used in Linux systems to view the resource usage of each process in the system in real time. To use it, enter "top" in the terminal.
After using the top command, all running processes in the current system will be displayed and sorted according to indicators such as CPU usage and memory usage. Display content and process sorting can be changed by pressing keys.

19. Kill the process: kill

kill command is used to terminate the process. Enter "kill [process number]" in the Linux terminal to terminate the corresponding process. For example: "kill 1234" will terminate the process with process number 1234.
If you need to forcefully terminate the process, you can add the -9 parameter before the command, for example: "kill -9 1234".
In addition, you can also use the killall command to terminate all processes with the specified name. For example: "killall firefox" will terminate all processes named firefox.

  • kill -s 9 27810: Kill the process with process number 27810, force termination, and system resources cannot be recycled

20. Compression and decompression: tar

The tar command is a commonly used file packaging and compression tool in Linux systems. It can pack multiple files into a single file, and can also decompress a packed file into multiple files.
The common command format is as follows:

  • Packaging: tar -cvf [packaging file name.tar] [file/directory to be packed]
  • Unzip: tar -xvf [package file name.tar]
  • View the contents of the package file: tar -tvf [package file name.tar]
    Among them, -c means to create the package file, -x means to decompress, and -v means to display the detailed process. -f means use file.
    In addition, there are some commonly used parameters:
  • -z compress/decompress .gz files
  • -j compress/decompress .bz2 files
  • -J compress/decompress .xz files
  • –exclude=PATTERN excludes packaging/decompression of specified files/directories
  • –wildcards wildcard matching files/directories
    For example:
  • tar -zcvf test.tar.gz testdir packs and compresses the testdir directory into a test.tar.gz file
  • tar -xzvf test.tar.gz Decompress the test.tar.gz file.

21. Change the owner and group of a file or directory: chown

The chown command is used to change the owner and/or group of a file or directory. Its syntax is:
chown [option] [owner][:group] file/directory
where the option can be -R (recursive change), -v (display detailed information about changes), -c (display only changed information), -f (ignore error information), etc. Owner and group can be username or user ID, group name or group ID. If only the owner is specified, the group does not change. If only the group is specified, the owner will not change. If both owner and group are specified, separate them with a colon.
For example, to change the owner of the file file.txt to user1 and the group to group1, you can use the following command:
chown user1:group1 file.txt
To change the owner of directory dir1 and all its subdirectories and files to user2, you can use the following command:
chown -R user2 dir1

22. Change the access permissions of a file or directory: chmod

The chmod command is used to change the access permissions of files or directories. The syntax format is: chmod [options] permissions file/directory.
Common options:

  • -R: Recursively change the permissions of all files and subdirectories in the directory.
  • -v: Display the name of the changed file.
    Permissions can be represented by numbers or letters:
  • Numerical representation: rwx corresponds to 7, 6, 5, 4, 3, 2, 1 and 0 respectively. For example, 755 means that the file owner has read, write, and execute permissions, and other users only have read and execute permissions.
  • letter representation: u represents the file owner, g represents the group where the file is located, o represents other users, and a represents all users. + means increasing permissions, - means reducing permissions, = means setting permissions. For example, u+x means adding execute permissions to the file owner.
    For example:
  • chmod 755 test.txt: Set read, write, and execute permissions (file owner), read, and execute permissions (other users) for the test.txt file.
  • chmod -R +x test_dir: Recursively add execution permissions to all files and subdirectories in the test_dir directory.
    Note: There are three types of file permissions in Linux, read, write, and execute.

23. Text editing: vim

vim is a commonly used text editor, often used to edit text files in Linux systems. You can use the vim command to open a file and edit it. The specific usage is as follows:

  1. Open a file: Enter the vim command in the terminal, followed by the file name, to open the file. For example: vim test.txt
  2. Insert mode: After opening the file, press the i key to enter insert mode, where you can edit text.
  3. Save the file: After editing is completed, press the Esc key to exit the insert mode, and then enter the :wq command to save and exit the file.
  4. Abandon modifications: If you do not want to save the modifications, you can press the Esc key to exit the insert mode, and then enter the :q! command to abandon the modifications and exit the file.
  5. Search text: In command mode, enter/add the text you want to find to find the text in the file.
  6. Replace text: In command mode, enter: %s/text to be replaced/replaced text/g to replace the text in the file.
  7. Copy and paste: In command mode, press the v key to enter visual mode, then select the text to be copied, then press the y key to copy, and finally press the p key to paste.
    The above are the basic usage of vim commands, and there are many advanced usages that require further learning and mastering.

24. Shut down or restart: shutdown

  • shutdown -h now: Shut down immediately
  • shutdown -r -t 60: Restart after 60 seconds
  • shutdown -r now:restart(1)
  • reboot: restart(2)

25. Help command: man

The "man" command is used to view the help documentation of instructions in the Linux system. To use it, enter "man" followed by the command name in the terminal, such as "man ls" to view the help documentation for the ls command. While viewing, you can use the space bar to turn pages and the "q" key to exit.

26. View IO reading and writing: iotop

The iotop command is used to monitor disk IO in Linux systems and can display disk IO usage and IO statistics of related processes in real time. The following is the usage of iotop command:

  1. Run directly: Enter the iotop command in the terminal to run iotop directly and display disk IO information in real time.
  2. View specific devices: Use the "-d" parameter to specify the device to be monitored. For example, iotop -d /dev/sda indicates that only the IO status of the /dev/sda device is monitored.
  3. Display cumulative values: Use the "-a" parameter to display cumulative IO statistics. For example, iotop -a means to display accumulated IO statistics instead of real-time data.
  4. Display thread level: Use the "-P" parameter to display thread-level IO statistics for each process. For example, iotop -P indicates to display thread-level IO statistics for each process.
  5. Display bandwidth: Use the "-b" parameter to display the IO bandwidth in KB/s. For example, iotop -b displays the IO bandwidth in KB/s.
  6. Display sectors: Use the "-s" parameter to display IO information in units of sectors. For example, iotop -s indicates to display IO information in sectors.
  7. Sort display: Use the "-o" parameter to specify which field to sort and display. For example, iotop -o %cpu indicates sorting and displaying by CPU usage.
    The above are some common uses of the iotop command. There are other parameters and options that can be further explored and used.
    iotop -o: Directly view relatively advanced disk reading and writing programs
    (yum install iotop installation)

27. Check the port number occupancy: lsof -i

The lsof (list open files) command is used to list open files and files being used by processes in the Linux system. It can display file-related information, such as file descriptor, process ID, user, file type, etc. The following are common uses of the lsof command:

  1. List all open files: Enter the "lsof" command in the terminal to list all open files in the current system.

  2. List the files opened by the specified process: Use the "-p" parameter plus the process ID, such as "lsof -p 1234" to list the files opened by the process with process ID 1234.

  3. List the files opened by the specified user: Use the "-u" parameter plus the user name, such as "lsof -u username" to list the files opened by the process under the specified user name.

  4. List the files opened in the specified directory: Use the "+D" parameter plus the directory path, such as "lsof +D /path/to/directory" to list the files opened in the specified directory.

  5. List which processes are using the specified file: Use the "+f" parameter plus the file path, such as "lsof +f /path/to/file" to list the processes that are using the specified file.

  6. List network connections: Use the "-i" parameter, such as "lsof -i", to list information about the current network connections in the system.

  7. List listening ports: Use the "-i" parameter plus "LISTEN", such as "lsof -i TCP:LISTEN", to list the listening TCP ports in the system.

These are just some common uses of the lsof command, there are other parameters and options that can be further explored and used.

28. View the report system running time and average load: uptime

The "uptime" command is used to display the running time of the system and the average load of the system. The following is the usage of the "uptime" command:

  1. Direct operation: Enter the "uptime" command in the terminal to display the system's running time, current time, number of logged-in users, and average load of the system.

  2. Display readable format: Use the "-p" parameter to display the system's running time in a readable format. For example, "uptime -p" will display the running time in the format of "up 2 days, 3 hours, 30 minutes".

  3. Specify the refresh interval: Use the "-s" parameter to specify the refresh interval to update the display of average load. For example, "uptime -s" will display the system's uptime and load average at each refresh.

  4. Display short format: Use the "-s" parameter combined with the "-p" parameter to display the system's running time and average load in short format. For example, "uptime -s -p" will display the running time in the format of "up 2d 3h 30m".

These are just some common uses of the "uptime" command, there are other parameters and options that can be further explored and used.

29. Commonly used shortcut keys

  • Ctrl + a moves the cursor to the beginning
  • Ctrl + c interrupts the current program
  • Ctrl + d exit the current window or current user
  • Ctrl + e Cursor to the end
  • Ctrl + l clears the screen, which is equivalent to clear
  • Ctrl + u Cut and delete the content (before the cursor) - - Ctrl + k Cut and delete the content (after the cursor) - - Ctrl + r Search (recently used commands)
  • tab all paths and completion commands
  • Ctrl+shift+c command line copy content
  • Ctrl+shift+v command line paste content
  • Ctrl + q cancel screen lock
  • Ctrl + s perform screen lock

30. Important orders

  • top: View memory/display system current process information
  • df -h: Check disk storage status
  • iotop: View IO reading and writing (yum install iotop installation)
  • iotop -o: Directly view relatively advanced disk reading and writing programs
  • netstat -tunlp | grep port number: Check the port number occupancy (1)
  • lsof -i:port number: Check the port number occupancy (2)
  • uptime: View and report system running time and average load
  • ps aux: View process

31. fdisk command

The fdisk command is used to partition a disk and can create, delete, and adjust disk partitions. The following are common uses of the fdisk command:

  1. List the device partition table: Enter the "fdisk -l" command in the terminal to list all disk devices and their partition table information in the system.
  2. Partition the specified device: Use the "fdisk /dev/sdX" command, replacing "/dev/sdX" with the path of the device to be partitioned, such as "/dev/sda". Then follow the prompts to perform partitioning operations.
  3. Create a new partition: In the interactive interface of the fdisk command, use the "n" command to create a new partition. Select the primary partition or logical partition according to your needs, and specify the starting sector and size of the partition.
  4. Delete a partition: In the interactive interface of the fdisk command, use the "d" command to delete the specified partition. Select the partition serial number to be deleted according to the prompts.
  5. View created partitions: In the interactive interface of the fdisk command, use the "p" command to view the list of created partitions.
  6. Save the partition table: In the interactive interface of the fdisk command, use the "w" command to save changes to the partition table.
  7. Exit the fdisk interactive interface: In the interactive interface of the fdisk command, use the "q" command to exit the interface without saving changes.
    Please note that before partitioning the disk, be sure to back up important data and operate with caution to avoid data loss.

Guess you like

Origin blog.csdn.net/feritylamb/article/details/132300877