Article directory
1. File system commands
ls - list directory contents
ls [选项] [文件或目录]
- Function: Display the file and directory list in the specified directory.
- Common selection:
-a
: Show all files, including hidden files.-l
: Display detailed information in long format.-h
: When used with-l
, displays the file size in human-readable format (e.g. KB, MB).
Example:
# 列出当前目录所有文件
ls -a
# 显示当前目录详细信息
ls -lh
cp - copy a file or directory
cp [选项] 源文件 目标文件
- Function: Copy files or directories to another location.
- Common selection:
-r
: Recursively copies a directory and its contents.-i
: Prompt before overwriting a file.-v
: Display detailed copy information.
Example:
# 将文件复制到另一个目录
cp source.txt /path/to/destination/
# 递归复制整个目录
cp -r source_directory /path/to/destination/
mv - move a file or directory
mv [选项] 源文件 目标文件
- Function: Move files or directories to a new location, or rename files or directories.
- Common selection:
-i
: Prompt before overwriting a file.-v
: Display detailed movement information.
Example:
# 将文件移动到新位置
mv source.txt /path/to/destination/
# 重命名文件
mv oldname.txt newname.txt
rm - delete a file or directory
rm [选项] 文件或目录
- Function: Delete files or directories.
- Common selection:
-r
: Recursively delete a directory and its contents.-f
: Forced deletion, ignoring non-existent files, without prompting.-i
: Prompt before deletion.
Example:
# 删除单个文件
rm file.txt
# 递归删除目录
rm -r directory_name
mkdir - create a new directory
mkdir [选项] 目录名
- Function: Create a new empty directory.
- Common selection:
-p
: When creating a directory, create a parent directory if necessary.-v
: Display information when creating a directory.
Example:
# 创建单个目录
mkdir new_directory
# 创建目录及其父目录
mkdir -p path/to/new_directory
rmdir - remove empty directories
rmdir [选项] 目录
- Function: Delete empty directories.
- Common selection:
-p
: When a subdirectory is deleted, try to delete the parent directory.
Example:
# 删除空目录
rmdir empty_directory
touch - Create or update a file
touch [选项] 文件名
- Function: If the file does not exist, create an empty file; if the file exists, update the access and modification time of the file.
Example:
# 创建一个新文件(存在则更新现有文件的时间)
touch newfile.txt
2. System management commands
top - displays system process information
top [选项]
- Function: Display real-time system processes and resource usage.
- Common selection:
-o
: Sort by specified column (such as cpu, mem).-u
: Only show processes for a specific user.-s
: Set refresh interval.
Example:
# 显示所有进程,按 CPU 使用率排序
top -o cpu
# 每 5 秒刷新一次
top -s 5
ps - displays current process status
ps [选项]
- Function: Display process information in the current system.
- Common selection:
-e
: Show all processes.-f
: Show full format.-u 用户名
: Display the processes of a specific user.
Example:
# 显示所有进程
ps -e
# 显示特定用户的进程
ps -u username
kill - terminate a process
kill [选项] 进程ID
- Function: Send a signal to a specific process, usually used to terminate the process.
- Common selection:
-9
: Forcefully terminate the process.-15
: Terminate the process gracefully (default).
Example:
# 优雅地终止进程
kill 1234
# 强制终止进程
kill -9 1234
system_profiler - Get system information
system_profiler [选项]
- Function: Display detailed information about the Mac system hardware and software configuration.
- Common selection:
-detailLevel [级别]
: Set the information detail level (mini, basic, full).
Example:
# 显示所有硬件和软件信息
system_profiler
# 显示基本信息
system_profiler -detailLevel basic
diskutil - Disk management tool
diskutil [命令] [选项]
- Function: Manage disks and storage devices.
- Common command:
list
: Show all disks.info 磁盘
: Get disk information.eraseDisk 格式 名称 磁盘
: Format the disk.
Example:
# 显示所有磁盘
diskutil list
# 格式化磁盘
diskutil eraseDisk JHFS+ NewDisk disk2
networksetup - Network configuration tool
networksetup [选项] [参数]
- Function: Configure network settings.
- Common command:
-listallnetworkservices
: List all network services.-setairportpower 服务 状态
: Turn wireless network on or off.
Example:
# 列出所有网络服务
networksetup -listallnetworkservices
# 关闭 Wi-Fi
networksetup -setairportpower Wi-Fi off
sudo - Execute commands with superuser privileges
sudo [命令]
- Function: Execute commands with the permissions of the root user.
Example:
# 以 root 权限运行命令
sudo command_name
3.Network commands
ping - test network connection
ping [选项] 主机名或IP地址
- Function: Send an ICMP ECHO_REQUEST packet to test the network connection with the remote host.
- Common selection:
-c 数量
: Send a specific number of ping requests.-t 存活时间
: Set the lifetime of IP packets.
Example:
# ping一个地址,发送 5 个请求
ping -c 5 www.example.com
# 设置生存时间为 10
ping -t 10 www.example.com
ifconfig - Configure or display network interface parameters
ifconfig [接口]
- Function: Used to configure or display the parameters of the system network interface.
- Common selection:
接口名 up/down
: Enable/disable the specified interface.接口名 inet 地址
: Set the IPv4 address of the specified interface.
Example:
# 显示所有接口的信息
ifconfig -a
# 启用一个接口
sudo ifconfig en0 up
netstat - displays network status
netstat [选项]
- Function: Display network interface, routing table, network connection and other information.
- Common selection:
-r
: Display routing table.-s
: Display network statistics.
Example:
# 显示路由表
netstat -r
# 显示所有端口的统计信息
netstat -s
ssh - Securely log in remotely to another computer
ssh [选项] 用户名@远程主机
- Use: Securely log in to another computer remotely over the network.
- Common selection:
-p 端口
: Specify the port number of the remote host.-i 密钥文件
: Specify the private key file used for authentication.
Example:
# 使用特定端口登录
ssh -p 2222 [email protected]
# 使用私钥文件登录
ssh -i /path/to/private_key [email protected]
scp - Securely copy files to a remote host
scp [选项] 本地文件 用户名@远程主机:远程路径
- Function: Securely copy files to a remote host via SSH.
- Common selection:
-P 端口
: Specify the port number of the remote host.-r
: Recursively copy the entire directory.
Example:
# 将本地文件复制到远程服务器
scp /path/to/local/file [email protected]:/path/to/remote/
# 递归复制本地目录
scp -r /path/to/local/dir [email protected]:/path/to/remote/
nslookup - Query DNS information
nslookup [域名]
- Function: Query the DNS information of the domain name.
- Common selection:
-type=类型
: Specify the DNS record type to be queried, such as A, MX, TXT, etc.
Example:
# 查询域名的默认 DNS 信息
nslookup example.com
# 查询域名的 MX 记录
nslookup -type=MX example.com
4. Text processing commands
cat - Concatenate and display file contents
cat [选项] [文件...]
- Function: Display the content of a file, or connect the contents of multiple files for display.
- Common selection:
-n
: Display line number.-b
: Output line numbers only for non-empty lines.
Example:
# 显示文件内容
cat file.txt
# 显示多个文件内容
cat file1.txt file2.txt
# 显示内容并编号所有行
cat -n file.txt
grep - search for text
grep [选项] 模式 [文件...]
- Function: Search the file for lines matching the specified pattern.
- Common selection:
-i
: Ignore case.-v
: Inverts the match, showing unmatched lines.-r
: Search directories recursively.
Example:
# 在文件中搜索词汇
grep 'word' filename
# 递归搜索包含词汇的文件
grep -r 'word' /path/to/dir/
# 显示不包含指定词汇的行
grep -v 'word' filename
sed - stream editor
sed [选项] '命令' [文件...]
- Role: Perform basic text conversion on text streams (files or input).
- Common selection:
-e
: Allows multiple commands.-i
: Edit files directly (non-standard sed usage, use with caution).
Example:
# 替换文本
sed 's/old/new/g' file.txt
# 删除行
sed '3d' file.txt
awk - Pattern scanning and processing language
awk [选项] '程序' [文件...]
- Role: Powerful text processing language, suitable for complex text analysis and reporting.
- Common selection:
-F
: Specifies the field separator.
Example:
# 打印文件的第一列
awk '{print $1}' file.txt
# 按逗号分隔并打印第二列
awk -F, '{print $2}' file.csv
nano - Command line text editor
nano [文件]
- Function: A simple and easy-to-use text editor.
Example:
# 编辑文件
nano file.txt
vi/vim - Visual text editor
vi [选项] [文件...]
- Function: A powerful text editor that supports multiple editing modes and extensions.
Example:
# 打开或创建文件进行编辑
vi file.txt
sort - Sort lines of text
sort [选项] [文件...]
- Function: Sort the lines of the text file.
- Common selection:
-n
: Sort by numeric value.-r
: Sort in reverse order.
Example:
# 对文件内容排序
sort file.txt
# 数字排序
sort -n file.txt
5. Permissions and user management commands
chmod - change file permissions
chmod [选项] 权限 文件或目录
- Function: Change the access permissions of files or directories.
- Common selection:
-R
: Recursively change permissions on a directory and its contents.
- Permissions: Permissions can be numbers (like 755) or symbols (like u+r).
Example:
# 给文件设置读写执行权限
chmod 755 file.txt
# 递归地给目录设置权限
chmod -R 755 directory/
chown - change file owner
chown [选项] 用户[:组] 文件或目录
- Function: Change the owner and group of a file or directory.
- Common selection:
-R
: Recursively change the owner of a directory and its contents.
Example:
# 更改文件的所有者
chown username file.txt
# 递归地更改目录所有者
chown -R username directory/
sudo - execute commands as superuser
sudo [命令]
- Function: Allow ordinary users to execute commands as superuser (administrator).
Example:
# 以超级用户身份运行命令
sudo command_name
su - switch users
su [选项] [用户名]
- Function: Switch the current user identity to another user.
Example:
# 切换到 root 用户
su root
# 切换到特定用户
su username
passwd - change user password
passwd [选项] [用户]
- Function: Change the user's password.
Example:
# 更改当前用户的密码
passwd
# 更改指定用户的密码(需要管理员权限)
sudo passwd username
dscl - Directory Services Command Line Tool
dscl [选项] [命令]
- Role: Used to manage user accounts and groups.
- Common command:
. -list /Users
: List all users.. -create /Users/用户名
: Create a new user.
Example:
# 列出所有用户
dscl . -list /Users
# 创建新用户
sudo dscl . -create /Users/newuser
groups - displays the groups the user belongs to
groups [用户名]
- Function: Display all groups to which the user belongs.
Example:
# 显示当前用户的组
groups
# 显示指定用户的组
groups username
6. Disk management commands
diskutil - Disk management tool
diskutil [命令] [选项]
- Function: Disk Utility command line interface, used to manage disks and storage devices.
- Common command:
list
: List all disks and partitions.info 磁盘
: Displays disk or partition details.eraseDisk 格式 名称 磁盘
: Format the entire disk.eraseVolume 格式 名称 分区
: Format a specific partition.mountDisk 磁盘
: Mount all partitions on the disk.unmountDisk 磁盘
: Unmount all partitions on the disk.
Example:
# 列出所有磁盘:
diskutil list
# 格式化磁盘:
diskutil eraseDisk JHFS+ NewDisk disk2
# 卸载磁盘:
diskutil unmountDisk /dev/disk2
df - displays disk space usage
df [选项] [文件...]
- Function: Display the free space and used space of the disk partition.
- Common selection:
-h
: Display in easy-to-read format (such as GB and MB).-i
: Show inode information instead of block usage.
Example:
# 显示所有磁盘使用情况:
df -h
# 显示指定文件所在磁盘的使用情况:
df -h /path/to/file
du - displays directory space usage
du [选项] [文件或目录...]
- Function: Display the disk space occupied by a directory or file.
- Common selection:
-h
: Display in an easy-to-read format.-s
: Show total only.
Example:
# 显示当前目录的磁盘使用情况:
du -h
# 显示指定目录的总磁盘使用量:
du -sh /path/to/directory
mount - Mount a file system
mount [选项] [设备] [目录]
- Function: Mount the file system to the specified mount point.
Example:
# 显示所有挂载的文件系统:
mount
# 挂载一个设备
mount /dev/disk2s1 /mnt/disk2
umount - Unmount a file system
umount [选项] [目录或设备]
- Function: Unmount the file system from its mount point.
Example:
# 卸载一个挂载点
umount /mnt/disk2
fsck - File system consistency check and repair
fsck [选项]
- Function: Check and repair the consistency of the file system.
- Common selection:
-y
: Automatically fix problems during inspection.-f
: Force checking even if the file system appears to be clean.
Example:
# 修复特定的文件系统
fsck /dev/disk2s1
hdiutil - Manipulate disk images
hdiutil [操作] [选项]
- Role: Used to create, check, mount and operate disk image files.
- Common operations:
create
: Create a new disk image.attach
: Mount the disk image.detach
: Unmount the disk image.
Example:
# 创建磁盘映像
hdiutil create -size 500m -volname "MyVolume" -fs "HFS+" myimage.dmg
# 挂载磁盘映像
hdiutil attach myimage.dmg
7. Search and find commands
find - Find files and directories
find [路径...] [表达式]
- Function: Search for files and directories in the directory tree, supporting multiple search conditions.
- common expression:
-name 模式
: Search by file name.-type 类型
: Search by type (f stands for ordinary file, d stands for directory).-exec 命令 {} \;
: Execute commands on matching files.
Example:
# 在当前目录及子目录中查找所有 .txt 文件
find . -name "*.txt"
# 查找并删除特定文件
find . -name "temp.txt" -exec rm {
} \;
grep - search for text in a file
grep [选项] 模式 [文件...]
- Function: Search the file for lines of text that match the specified pattern.
- Common selection:
-i
: Ignore case.-r
: Search directories recursively.-l
: List only filenames containing matching lines.
Example:
# 在文件中搜索特定文本
grep "search term" file.txt
# 递归地在目录中搜索文本
grep -r "search term" /path/to/dir
locate - Find files quickly
locate [选项] 模式
- Function: Use a pre-built database to quickly find the location of files.
- Common selection:
-i
: Ignore case differences.-n 限制
: Limit the number of results displayed.
Example:
# 查找名为 example.txt 的文件
locate example.txt
# 忽略大小写查找文件
locate -i Example.txt
mdfind - Search using Spotlight
mdfind [选项] 查询
- Use: Use Spotlight's index for file search.
Example:
# 搜索与查询匹配的文件
mdfind "search term"
# 查找特定种类的文件(例如 PDF)
mdfind "kMDItemContentType == 'com.adobe.pdf'"
which - locates the executable file
which [程序名]
- Function: Display the full path of the executable file to be executed.
Example:
# 查找特定程序的位置
which python
whereis - Find binaries, source files and man pages
whereis [程序名]
- Function: Quickly locate the location of the program's binary files, source files and man pages.
Example:
# 查找程序的相关文件
whereis python