Summary of commonly used commands in Linux systems

hint

  • [ ] : Indicates that the fields in [ ] are optional parameters
  • [options] : Optional parameters, representing command options. Options usually consist of a dash (-) followed by a letter or a dash (–) followed by a word. These options allow different formatting and filtering of the command's output.
  • [directory] : Optional parameter, representing the path of the directory.

1. File and directory operations

  • ls: List directory contents.
ls [options] [directory]
# 列出当前目录的内容(通常是用户的主目录)
ls

# 列出指定目录的内容
ls /path/directory

# 列出当前目录的内容,包括隐藏文件和目录的内容
ls -a

# 以长格式列出当前目录的内容(包括权限、所有者、文件大小等信息)
ls -l
  • pwd: Displays the current working directory.
pwd
  • cd: Change directory.
cd [directory]
# 进入指定目录
cd /path/to/directory

# 进入用户的主目录(通常是 home 目录)
cd ~

# 进入上级目录
cd ..

# 进入根目录
cd /

# 返回上一次所在的目录(使用 - 作为参数)
cd -
  • touch: creates an empty file.
touch filename
  • mkdir: Create directory.
mkdir directory
  • rm: Delete a file or directory.
rm [options] file/directory
# -r 或 -R:递归删除目录及其内容。
# 使用这个选项时,要特别小心,因为它将删除目录及其所有子目录和文件。
rm -r directory/

# -f:强制删除,不会提示用户确认。
# 这个选项通常与 -r 一起使用,以删除目录及其内容而无需确认。
rm -rf directory/

# -i:交互式删除,会在删除前提示用户确认。
rm -i file
  • cp: Copy a file or directory.
cp [options] source destination

options : can be a series of options used to control replication behavior.
source : The path to the source file or directory to be copied.
destination : The path of the target file or directory, that is, the location after copying.

# 复制文件到另一个位置
cp file1.txt /path/to/destination/

# 复制多个文件到目标目录
cp file1.txt file2.txt /path/to/destination/

# 复制目录及其内容到目标目录,使用 -r 选项进行递归复制
cp -r directory/ /path/to/destination/

# 保留原文件的权限和属性,使用 -p 选项
cp -p file1.txt /path/to/destination/

# 强制覆盖目标文件,如果目标文件已存在,使用 -f 选项
cp -f file1.txt /path/to/destination/

# 显示复制进度,使用 -v 选项(verbose)
cp -v file1.txt /path/to/destination/
  • mv: Move or rename a file or directory.
mv [options] source destination

options : can be a series of options used to control the behavior of moving or renaming.
source : The path to the source file or source directory to be moved or renamed.
destination : The path of the target file or directory, that is, the location after movement or the name after renaming.

# 移动文件到另一个位置
mv file1.txt /path/to/destination/

# 重命名文件
mv oldname.txt newname.txt

# 移动目录到另一个位置,使用 -i 选项进行交互式确认
mv -i directory/ /path/to/destination/

# 强制覆盖目标文件,如果目标文件已存在,使用 -f 选项
mv -f file1.txt /path/to/destination/

# 移动目录并保留原文件的权限和属性,使用 -p 选项
mv -p directory/ /path/to/destination/

# 移动多个文件到目标目录
mv file1.txt file2.txt /path/to/destination/

# 显示移动过程,使用 -v 选项(verbose)
mv -v file1.txt /path/to/destination/

2. File viewing and editing

  • cat: View file contents.
cat filename
  • more / less: View file contents in split screen.
# more 命令以一页一页地显示文件内容,每次按下空格键会显示下一页,按下 q 键退出。
# more 不支持回滚操作,只能向前查看。
more filename

# less 支持向前和向后滚动查看文件。
# 你可以使用空格键向下滚动一页,使用 b 键向上滚动一页,按下 q 键退出。
less filename
  • head / tail: View the head or tail of the file.
head [options] filename
tail [options] filename
# 查看文件的前 20 行
head -n 20 filename

# 查看文件的最后 20 行
tail -n 20 filename 

# 实时监视文件的末尾内容 
tail -f filename    
  • nano / vi / vim: text editor.
  • nano is a relatively simple text editor suitable for beginners. It provides basic text editing functions, including opening, editing, saving and exiting files. nano provides some common shortcut key operations, such as Ctrl + O to save files, Ctrl + X to exit the editor, etc.
  • vi is a powerful but slightly complex text editor that usually takes some time to become familiar with. It is divided into multiple modes, including normal mode, insert mode and command mode. By default, vi is in normal mode after opening a file, and you need to press the i key to enter insert mode to edit text. After editing, press Esc to return to normal mode, then use :w to save the file, :q to exit the editor.
  • vim is an enhanced version of vi with more features and customization options. vim also has multiple modes. Use the i key to enter insert mode, press Esc to return to normal mode, then use :w to save the file, :q to exit the editor, :wq to save and exit the editor, :q! to abandon all changes and force quit editing. device.
nano filename
vi filename
vim filename

3. File permission management

  • chmod: Modify file permissions.
chmod [options] mode filename

options: Options for the chmod command, such as -R for recursively modifying permissions on a directory and its contents.
mode: Permission mode, which can be represented by numbers or symbols.
filename: The name of the file or directory whose permissions are to be modified.

Setting permissions using numbers
The chmod command can use numbers to represent permissions, where each permission has a numeric value:
4: Read permission (r)
2: Write permission (w)
1: Execute permission (x)

Using these numbers, you can set them Added together to create permission patterns.

# 将文件 filename 的权限设置为所有者可读、写和执行,而其他用户只能读和执行
chmod 755 filename

# 递归地设置目录 directory 及其所有内容的权限
chmod -R 755 directory

Setting Permissions Using Symbols
Permission patterns can also be represented more visually using symbols.
+: Add permissions.
-: Delete permissions.
=: Set permissions.

# 允许所有者添加执行权限
chmod u+x filename 

#删除群组和其他用户的读写权限
chmod go-rw filename
  • chown: Modify the file owner and group.
chown [newowner]:[newgroup] filename

newowner: The username or user ID to be assigned to the new owner of the file or directory.
newgroup: The group name or group ID of the new group to be assigned to the file or directory.
filename: The name of the file or directory whose owner and group it belongs to are to be modified.

# 将文件 file.txt 的所有者更改为用户 alice
chown alice file.txt 

# 将文件 file.txt 的所属组更改为组 users
chown :users file.txt 

# 将文件 file.txt 的所有者更改为用户 alice,并将所属组更改为组 users
chown alice:users file.txt 

4. File compression and decompression

  • tar: Packed (compressed) files.
tar -cvf archive.tar files/directories

-c: Create archive file.
-v: Display detailed information, usually used to view the list of files being packaged.
-f: Specifies the name of the archive file.

# 创建一个名为 myarchive.tar 的归档文件,包含 file1.txt、file2.txt 和 directory1/
tar -cvf myarchive.tar file1.txt file2.txt directory1/
  • tar: Unpack (decompress) a file.
tar -xvf archive.tar

-x: Unpack archive files.
-v: Display detailed information, usually used to view the list of files being unpacked.
-f: Specifies the name of the archive file to be unpacked.

# 解包名为 myarchive.tar 的归档文件
tar -xvf myarchive.tar
  • tar: View archive file contents.
tar -tvf archive.tar

-t: Display the contents list of the archive file.
-v: Display detailed information, usually used to view file lists.
-f: Specifies the name of the archive file to view.

# 显示 myarchive.tar 归档文件的内容列表
tar -tvf myarchive.tar
  • tar: Add files or directories to an archive.
tar -rvf archive.tar new_files/directories

-r: Add files or directories to the archive.
-v: Display detailed information, usually used to view the list of added files.
-f: Specifies the name of the archive file.

# 向名为 myarchive.tar 的归档文件中添加新文件和目录
tar -rvf myarchive.tar newfile.txt newdirectory/
  • gzip/gunzip: Compress and decompress files.
# 压缩
tar -czvf archive.tar.gz files/directories

# 解压缩
tar -xzvf archive.tar.gz

-z: indicates using gzip compression (or decompression)

5. Find files

  • find: Find files and directories in the file system.
find [directory] [condition] filename

directory : The starting directory path to search. If not specified, it defaults to the current directory.
-name : Define the matching conditions for search. Common ones are -name for matching by file name, and -type, -size, -mtime, etc. for matching based on type, size, modification date, etc.
filename : The name of the file or directory to be searched, wildcards can be used for fuzzy matching.

# 在当前目录及其子目录中搜索文件名为 "example.txt" 的文件
find . -name example.txt

# 在 /home/user/documents 目录中搜索文件名以 ".pdf" 结尾的文件
find /home/user/documents -name "*.pdf"

# 在整个文件系统中搜索目录名为 "backup" 的目录
find / -type d -name backup

# 搜索当前目录及其子目录中所有的空文件夹
find . -type d -empty

# 查找 /var/log 目录下最近 7 天内修改过的 .log 文件
find /var/log -type f -name "*.log" -mtime -7
  • grep: Search for text in a file.
grep [options] pattern [file...]

pattern: The text pattern to search for, which can be a normal string or a regular expression.
file…: List of files to search. You can specify one or more files. If you do not specify a filename, grep will read data from standard input.

Common options
-i or --ignore-case: ignore case matching.
-r or --recursive: Search files in the directory recursively.
-w or --word-regexp: only match the whole word, not part of it.
-n or --line-number: Display the line number of the matching line.
-l or --files-with-matches: Only display file names containing matching text, without displaying specific matching lines.
-v or --invert-match: Display lines that do not match the pattern.
-c or --count: Only display the number of lines matching the pattern without displaying specific lines.
-e or --regexp: Allows multiple search patterns to be specified on the command line.

# 在文件中搜索特定字符串
grep "pattern" filename

# 使用正则表达式搜索
grep -E "regex_pattern" filename

# 递归搜索目录中的文件
grep -r "pattern" /path/to/search

# 显示匹配行的行号
grep -n "pattern" filename

# 只显示包含匹配文本的文件名
grep -l "pattern" /path/to/search/*

# 显示不匹配模式的行
grep -v "pattern" filename

# 指定多个搜索模式
grep -e "pattern1" -e "pattern2" filename
  • locate: Find files based on the database.
locate [options] pattern

pattern: The file name pattern to search for. Can be an ordinary string, not a regular expression.

Common options
-i or --ignore-case: ignore case matching.
-l or --limit: Limit the number of matching results displayed.
-c or --count: Only display the number of matching results, not the file name.
-r or --regexp: Allows regular expression patterns to be specified on the command line.

# 查找包含指定名称的文件
locate filename

# 查找包含指定名称的目录
locate directoryname

# 忽略大小写查找
locate -i filename

# 限制显示结果数量
locate -l 5 filename

# 显示匹配结果的数量而不显示文件名
locate -c filename

# 使用正则表达式搜索
locate -r "regex_pattern"

6. System information and status

  • uname: Display system information.
uname [options]

Common options:
-a or --all: display all information, equivalent to -s -n -r -v -m -p -i.
-s or --kernel-name: display the kernel name.
-n or --nodename: Display the network node host name.
-r or --kernel-release: Display the kernel version.
-v or --kernel-version: Display kernel release information.
-m or --machine: Display hardware architecture.
-p or --processor: Display processor type.
-i or --hardware-platform: display hardware platform

# 显示操作系统名称
uname

# 显示所有系统信息
uname -a

# 显示内核版本
uname -r

# 显示硬件架构
uname -m
  • top/htop: Display system processes and resource usage.
# 多数 Linux 发行版中都默认安装了 top
# top 会以实时更新的方式显示系统状态,按 q 键可以退出
top

# 需要单独安装 htop。
# 是 top 的增强版本,提供了更多的功能和可视化效果
htop
  • ps: Display the current user's processes.
ps [options]
# 显示当前用户的所有进程
ps

# 显示所有进程的详细信息(包括其他用户的进程)
ps aux

# 以树状结构显示进程(显示进程的父子关系)
ps -ejH

# 显示某个特定用户的进程(以用户名为条件过滤)
ps -u username

# 显示某个特定进程的信息(以进程ID为条件过滤)
ps -p process_id

# 显示完整的进程命令行(包括参数)
ps -f

# 按 CPU 使用率排序并显示前 N 个进程
ps aux --sort=-%cpu | head -n N

# 按内存使用排序并显示前 N 个进程
ps aux --sort=-%mem | head -n N
  • df: Check disk space.
df [options]
# 查看所有挂载点的磁盘空间信息
df

# 查看指定文件系统类型(如 ext4、NTFS)的磁盘空间信息
df -t ext4

# 以更易读的格式显示磁盘空间(以 GB、MB 等单位显示)
df -h

# 仅显示本地文件系统(不包括虚拟文件系统)的磁盘空间信息
df -x tmpfs -x devtmpfs

# 显示指定挂载点的磁盘空间信息(用于特定目录的磁盘空间查看)
df /path/to/directory

# 以 inode 数量的方式查看文件系统信息(显示可用 inode 数量等)
df -i
  • du: Check the directory space usage.
du [options] directory
# 查看指定目录的磁盘空间使用情况(默认单位为千字节)
du /path/to/directory

# 以人类可读的格式显示磁盘空间(以 GB、MB 等单位显示)
du -h /path/to/directory

# 查看目录的总磁盘使用情况,不显示子目录的详细信息
du -s /path/to/directory

# 仅显示指定深度内的目录磁盘使用情况(例如,只显示一级子目录的情况)
du --max-depth=1 /path/to/directory

# 以更多详细信息显示目录的磁盘使用情况,包括子目录
du -ah /path/to/directory

# 排除指定目录或文件的磁盘使用情况,以忽略特定内容
du --exclude=pattern /path/to/directory

7. User and permission management

  • who: View the list of users currently logged into the system.
who
  • whoami: Displays the username of the currently logged in user.
whoami
  • passwd: Change user password.
passwd [username]

If no username is provided, simply type passwd and press Enter and you will be prompted for the current user's password, followed by a new password and confirmation.
If a username is provided, you will be prompted for a new password and confirmation, and the user's password will be changed.

  • adduser / useradd: Add user.
useradd [options] username
# 创建用户并创建home目录
useradd -m username 
  • deluser / userdel: Delete a user.
userdel [options] username
  • sudo: Execute commands with superuser privileges.

sudo is a very important Linux command that allows ordinary users to execute privileged commands with the permissions of the super user (also called the root user). This is an important security measure to ensure that not every user is able to perform potentially dangerous operations.

sudo command

command is the command to be executed with superuser privileges

8. Network related operations

  • ping: Tests connectivity between hosts.
ping [options] hostname/IP

hostname/IP is the hostname or IP address of the target host you want to test.
Common options
-c count: Specify the number of times to send packets. For example, -c 4 means sending 4 packets.
-i interval: Specifies the time interval (in seconds) between sending packets. For example, -i 2 means sending a packet every 2 seconds.
-s packetsize: Specifies the packet size to send (in bytes). For example, -s 64 means sending 64-byte packets.
-t timeout: Set the timeout, which is the maximum time to wait for the target host to respond. If there is no response after this time, the connection is considered failed.
-q: Runs ping in quiet mode, displaying only a summary of the results rather than the details of each packet.
-v: Run ping in verbose mode, displaying detailed information for each packet.

# 向示例主机 example.com 发送4个数据包
ping -c 4 example.com

# 设置数据包大小为100字节,并每隔1秒发送一个
ping -s 100 -i 1 example.com
  • ifconfig/ip: Configure network interface information.

Both the ifconfig and ip commands are used to configure and manage network interface information, but they may differ in different Linux distributions. ifconfig has been deprecated in some Linux distributions. Therefore, in new Linux systems, it is recommended to use the ip command to configure and manage network interfaces.

# 显示当前系统上所有的网络接口(如以太网接口、无线接口等)的配置信息,包括IP地址、子网掩码、MAC地址等。
ifconfig

# 将以太网接口 eth0 的IP地址设置为 192.168.1.2,子网掩码为 255.255.255.0
ifconfig eth0 192.168.1.2 netmask 255.255.255.0

# 显示当前系统上所有网络接口的详细信息
ip addr show

# 将以太网接口 eth0 的IP地址设置为 192.168.1.2,子网掩码为 /24,表示 255.255.255.0
ip addr add 192.168.1.2/24 dev eth0
  • netstat/ss: View network status.
netstat [options]

ss [options]

netstat and ss are both commands used to view network status and network connection information, but they may differ in different Linux distributions. It should be noted that the ss command has replaced netstat in some new Linux systems because it is faster and supports more options. Therefore, if you use a newer Linux distribution, it is recommended to use the ss command to check the network status.


# 查看网络接口信息
netstat -i

# 查看路由表信息
netstat -r

# 查看所有网络套接字的详细信息
ss -tuln

# 查看监听的TCP套接字
ss -tln

# 查看UNIX套接字
ss -lx
  • ssh: Remote login to other hosts.
ssh [options] [user@]hostname

[user@]hostname: The username and hostname (or IP address) of the remote host.
Common options
-p port: Specify the remote port number of the connection (default is 22).
-i identity_file: Specifies the private key file used for authentication.
-L [bind_address:]port:host:hostport: Create a local port forwarding.
-R [bind_address:]port:host:hostport: Create a remote port forwarding.
-X: Enables X11 forwarding, allowing graphical applications to be run on the remote host.
-C: Enable compression to improve data transfer performance.
-A: Enable proxy jump, allowing connections to other hosts through remote hosts.

# 系统将提示你输入远程主机的用户密码以进行身份验证。一旦验证成功,你就可以在远程主机上执行命令。
ssh [email protected]

9. Package Management

  • apt/apt-get: Debian/Ubuntu 包电影。

apt and apt-get are package management tools for managing software packages on Debian and Debian-based Linux distributions such as Ubuntu.

# 更新软件包列表,这会从软件源更新软件包列表,以确保你能够获取到最新的软件包信息
apt update

# 安装指定名称的软件包
apt install package-name

# 升级系统中的所有软件包
apt upgrade

# 删除软件包,但保留配置文件
apt remove package-name

# 如果要删除软件包及其配置文件
apt purge package-name

# 搜索包含指定搜索词的软件包
apt search search-term

# 显示软件包信息
apt show package-name

# 查看已安装的软件包列表
apt list --installed

# 清理不再需要的依赖和缓存
apt autoremove
  • yum/dnf: Red Hat/CentOS package manager.

yum and dnf are package management tools used to manage software packages on Linux distributions such as Red Hat, CentOS, and Fedora.

# 更新软件包列表
sudo yum update
# 或者
sudo dnf update

# 安装软件包
sudo yum install package-name
# 或者
sudo dnf install package-name

# 升级系统中的所有软件包
sudo yum upgrade
# 或者
sudo dnf upgrade

# 删除软件包,但保留配置文件
sudo yum remove package-name
# 或者
sudo dnf remove package-name

# 删除已安装的软件包及其配置文件
sudo yum erase package-name
# 或者
sudo dnf erase package-name

# 搜索软件包
yum search search-term
# 或者
dnf search search-term

# 显示软件包信息
yum info package-name
# 或者
dnf info package-name

# 查看已安装的软件包列表
yum list installed
# 或者
dnf list installed

# 清理不再需要的依赖和缓存
sudo yum autoremove
# 或者
sudo dnf autoremove

10. Process Management

  • kill: Terminate the process with the specified process ID.
kill [options] process_id
  • ps: View process information.
ps [options]
  • top / htop: View system processes.
top

htop
  • killall: Terminate the process with the specified name.
killall [options] process_name

11. Time and date

  • date: Displays the system date and time.
date
  • cal: Display calendar.
cal

# 查看 2023 年 8 月的日历
cal 8 2023
  • timedatectl: Configure system time and time zone.
# 查看当前的时间和时区设置
timedatectl

# 启用自动时间同步(NTP)
sudo timedatectl set-ntp true

# 将时区设置为 "Asia/Shanghai"
timedatectl set-timezone Asia/Shanghai

# 将日期设置为 "2023-08-28",时间设置为 "14:30:00"
timedatectl set-time "2023-08-28 14:30:00"

12. System shutdown and restart

  • shutdown: Shut down the system.
# 立即关闭系统
sudo shutdown -h now

# 立即重启系统
sudo shutdown -r now

# 计划在一定时间后关闭系统
sudo shutdown -h +30

# 取消计划的关机
sudo shutdown -c
  • reboot: Restart the system.
reboot

13. File transfer

  • scp: Safely copy files.
scp [options] source destination

source is the path to the file or directory to be copied.
destination is the target location, which can be the path of a local or remote computer, in the format [user@]host:destination.

# 从本地复制到远程计算机
scp /path/to/local/file username@remote:/path/to/destination/

# 从远程计算机复制到本地
scp username@remote:/path/to/remote/file /path/to/local/destination/

# 从远程计算机复制到本地并保持原始文件名
scp username@remote:/path/to/remote/file /path/to/local/destination/

# 从本地复制到远程计算机并保持原始文件名
scp /path/to/local/file username@remote:/path/to/destination/
  • rsync: Synchronize files and directories.
rsync [options] source destination
# 从本地复制到远程计算机
rsync [options] /path/to/local/file username@remote:/path/to/destination/

# 从远程计算机复制到本地
rsync [options] username@remote:/path/to/remote/file /path/to/local/destination/

# 从远程计算机复制到本地并保持原始文件名
rsync [options] --relative username@remote:/path/to/remote/file /path/to/local/destination/

# 从本地复制到远程计算机并保持原始文件名
rsync [options] --relative /path/to/local/file username@remote:/path/to/destination/

14. Other commonly used commands

  • echo: print text.
echo "text"
  • history: View command history.
history [options]
  • wget/curl: Download files.
wget [options] URL
curl [options] URL

These commands are commonly used in Linux systems and are used to perform various system management and file operation tasks. Each command has its own options and parameters. You can view the help information by following the command with --help for more details.

Guess you like

Origin blog.csdn.net/weixin_53902288/article/details/132752115