introduce
Linux is an open source operating system widely used in servers and personal computers. This document will introduce some commonly used Linux commands to help users better understand and use the Linux system.
Table of contents
- File and directory operations
- View and edit file content
- Process management
- System Management
- network management
File and directory operations
ls
Command format:ls [选项] [文件或目录]
Function: Display directory content or file information.
Options:
-l
: Display file details in long format.-a
: Show all files including hidden files.-h
: Display the file size in human-readable form.-R
: Display directory and subdirectory contents recursively.
Example:
$ ls -l
$ ls -a
$ ls -lh
$ ls -R
cd
Command format:cd [目录]
Function: Switch the current working directory.
Example:
$ cd /path/to/directory
$ cd ..
$ cd ~
pwd
Command format:pwd
Function: Display the absolute path of the current working directory.
Example:
$ pwd
mkdir
Command format:mkdir [选项] 目录
Function: Create directory.
Options:
-p
: Create directories recursively. If the upper-level directory does not exist, it will also be created.
Example:
$ mkdir mydir
$ mkdir -p /path/to/directory
cp
Command format:cp [选项] 源文件 目标文件
Function: Copy files or directories.
Options:
-r
: Copies a directory and its contents recursively.
Example:
$ cp file1.txt file2.txt
$ cp -r dir1 dir2
mv
Command format:mv [选项] 源文件 目标文件
Function: Move files or directories, or rename files or directories.
Options:
-i
: If the target file already exists, prompt the user whether to overwrite it.
Example:
$ mv file1.txt newdir/file1.txt
$ mv file1.txt file2.txt
rm
Command format:rm [选项] 文件或目录
Function: Delete files or directories.
Options:
-r
:Recursively delete a directory and its contents.-f
: Forced deletion without prompting the user for confirmation.
Example:
$ rm file1.txt
$ rm -r dir1
$ rm -rf dir1
find
Command format:find [路径] [选项]
Function: Search for files under the specified path.
Options:
-name
: Search by file name match.-type
: Search by file type match
View and edit file content
cat
Command format:cat [选项] 文件
Function: Display file content.
Options:
-n
: Display line number.
Example:
$ cat file.txt
$ cat -n file.txt
less
Command format:less [选项] 文件
Function: Display file contents in pages.
Options:
-N
: Display line number.空格键
: Page down.b
: Page up./关键词
: Search for keywords in files.
Example:
$ less file.txt
$ less -N file.txt
head
Command format:head [选项] 文件
Function: Display the content at the beginning of the file.
Options:
-n
: Specify the number of display lines, the default is 10 lines.
Example:
$ head file.txt
$ head -n 5 file.txt
tail
Command format:tail [选项] 文件
Function: Display the content at the end of the file.
Options:
-n
: Specify the number of display lines, the default is 10 lines.-f
: Track file content changes in real time.
Example:
$ tail file.txt
$ tail -n 5 file.txt
$ tail -f file.txt
grip
Command format:grep [选项] 模式 文件
Function: Search for the specified pattern in the file.
Options:
-i
: Ignore case.-v
: Reverse match, display unmatched lines.
Example:
$ grep "pattern" file.txt
$ grep -i "pattern" file.txt
$ grep -v "pattern" file.txt
we
Command format:vi 文件
Function: Use Vi editor to open files.
Vi is one of the most commonly used text editors in Linux, with powerful editing and search capabilities.
Example:
$ vi file.txt
In the Vi editor you can use the following commands:
i
: Enter insert mode and you can edit the file.Esc
: Exit insert mode.:w
:save document.:q
: Exit Vi editor.:wq
: Save the file and exit the Vi editor.
Process management
ps
Command format:ps [选项]
Function: Display currently running processes.
Options:
-f
: Show detailed information.-aux
: Show all processes.
Example:
$ ps
$ ps -f
$ ps -aux
top
Command format:top
Function: Display system process status in real time.
The Top command sorts processes by indicators such as CPU usage and memory usage, and updates them dynamically.
Example:
$ top
In the Top interface, you can use the following commands:
q
: Exit Top.
kill
Command format:kill [选项] 进程ID
Function: Terminate the specified process.
Options:
-9
: Forcefully terminate the process.
Example:
<br/>
```powershell
$ kill 1234
$ kill -9 5678
pstree
Command format:pstree [选项]
Function: Display the relationship between processes in a tree structure.
Options:
-p
: Display process ID.
Example:
$ pstree
$ pstree -p
System Management
uname
Command format:uname [选项]
Function: Display system information.
Options:
-a
: Display all information.
Example:
$ uname
$ uname -a
df
Command format:df [选项] [文件或目录]
Function: Display disk space usage.
Options:
-h
: Display the file size in human-readable form.-i
: Display inode information.
Example:
$ df
$ df -h
$ df -i
of
Command format:du [选项] [文件或目录]
Function: Display the disk usage of a file or directory.
Options:
-h
: Display the file size in human-readable form.-s
: Show only the total size.
Example:
$ du
$ du -h
$ du -s
free
Command format:free [选项]
Function: Display system memory usage.
Options:
-h
: Display the file size in human-readable form.
Example:
$ free
$ free -h
ifconfig
Command format:ifconfig [网络接口] [选项]
Function: Display or configure network interface information.
Options:
-a
: Display all network interfaces.网络接口
:Specify the network interface to be displayed or configured.
Example:
$ ifconfig
$ ifconfig eth0
ping
Command format:ping [选项] 主机名或IP地址
Function: Test the connectivity with the specified host.
Options:
-c
: Send the specified number of ICMP requests.-i
: Specify the time interval for ICMP requests.
Example:
$ ping google.com
$ ping -c 5 google.com
$ ping -i 2 google.com
network management
wget
Command format:wget [选项] URL
Function: Download files from the specified URL.
Options:
-O
: Specify the saving path and file name of the downloaded file.
Example:
$ wget http://example.com/file.txt
$ wget -O /path/to/save/file.txt http://example.com/file.txt
curl
Command format:curl [选项] URL
Function: Data transfer via URL.
Options:
-o
: Specify the saving path and file name of the downloaded file.
Example:
$ curl http://example.com/file.txt
$ curl -o /path/to/save/file.txt http://example.com/file.txt
ssh
Command format:ssh [选项] 用户名@主机名或IP地址
Function: Log in to the remote host through SSH protocol.
Options:
-
-p
: Specify the port number of the SSH server.Example:
$ ssh username@hostname $ ssh -p 2222 username@hostname
scp
Command format:
scp [选项] 源文件 目标文件
Function: Copy files between local host and remote host through SSH protocol.
Options:
-P
: Specify the port number of the SSH server.
Example:
$ scp file.txt username@hostname:/path/to/destination/ $ scp -P 2222 file.txt username@hostname:/path/to/destination/
ifup
Command format:
ifup 网络接口
Function: Start the specified network interface.
Example:
$ ifup eth0
ifdown
Command format:
ifdown 网络接口
Function: Close the specified network interface.
Example:
$ ifdown eth0
iptables
Command format:
iptables [选项] 规则
Function: Configure Linux firewall rules.
Options:
-A
: Add rules.-D
: Delete rule.-L
: List rules.
Example:
$ iptables -A INPUT -p tcp --dport 80 -j ACCEPT $ iptables -D INPUT -p tcp --dport 80 -j ACCEPT $ iptables -L
The above are some commonly used Linux commands, covering file and directory operations, file content viewing and editing, process management, system management and network management. By mastering these commands, you can better manage and operate your Linux system. Hope this document is helpful to you!