Appearance
基础命令
文件与目录操作
pwd - 显示当前目录
bash
pwd # 显示当前目录
pwd -P # 显示物理路径(解析符号链接)cd - 切换目录
bash
cd /Users/username # 切换到指定目录
cd ~ # 切换到用户主目录
cd # 切换到用户主目录
cd .. # 切换到上级目录
cd - # 切换到上一个目录
cd ../.. # 切换到上两级目录ls - 列出目录内容
bash
ls # 列出当前目录
ls /Users # 列出指定目录
ls -l # 长格式显示
ls -a # 显示隐藏文件
ls -h # 人类可读大小
ls -R # 递归显示
ls -t # 按修改时间排序
ls -S # 按文件大小排序
ls -G # 彩色输出
ls -la # 组合使用
ls -la@ # 显示扩展属性mkdir - 创建目录
bash
mkdir dir1 # 创建单个目录
mkdir dir1 dir2 # 创建多个目录
mkdir -p a/b/c # 创建嵌套目录
mkdir -m 755 mydir # 指定权限rmdir - 删除空目录
bash
rmdir dir1 # 删除空目录
rmdir -p a/b/c # 删除嵌套空目录rm - 删除文件或目录
bash
rm file1 # 删除文件
rm -r dir1 # 删除目录
rm -rf dir1 # 强制删除目录
rm -i file1 # 交互式删除
rm -f file1 # 强制删除
rm *.log # 删除所有 .log 文件cp - 复制文件或目录
bash
cp file1 file2 # 复制文件
cp file1 dir1/ # 复制到目录
cp -r dir1 dir2 # 复制目录
cp -p file1 file2 # 保留属性
cp -i file1 file2 # 覆盖前确认
cp -a dir1 dir2 # 归档复制
cp -R dir1 dir2 # 复制目录(macOS 推荐)
cp file1 file2 dir1/ # 复制多个文件到目录mv - 移动或重命名
bash
mv file1 file2 # 重命名
mv file1 dir1/ # 移动文件
mv dir1 dir2 # 移动目录
mv -i file1 file2 # 覆盖前确认
mv -f file1 file2 # 强制覆盖
mv *.txt dir1/ # 移动多个文件touch - 创建文件或更新时间戳
bash
touch file1 # 创建空文件
touch file1 file2 # 创建多个文件
touch -c file1 # 不创建新文件
touch -t 202312251200 file1 # 设置指定时间文件查看
cat - 查看文件内容
bash
cat file1 # 显示文件内容
cat -n file1 # 显示行号
cat -b file1 # 非空行显示行号
cat -s file1 # 合并空行
cat file1 file2 # 显示多个文件
cat file1 file2 > file3 # 合并文件more - 分页查看
bash
more file1 # 分页查看
more +10 file1 # 从第10行开始
more +/pattern file1 # 从匹配处开始常用操作:
- 空格:下一页
- Enter:下一行
- q:退出
- /pattern:搜索
less - 增强分页查看
bash
less file1 # 分页查看
less +F file1 # 实时跟踪(类似 tail -f)
less -N file1 # 显示行号常用操作:
- 空格/PageDown:下一页
- b/PageUp:上一页
- /pattern:向下搜索
- ?pattern:向上搜索
- n:下一个匹配
- N:上一个匹配
- q:退出
head - 查看文件开头
bash
head file1 # 显示前10行
head -n 20 file1 # 显示前20行
head -c 100 file1 # 显示前100字节
head -n -5 file1 # 不显示最后5行tail - 查看文件结尾
bash
tail file1 # 显示后10行
tail -n 20 file1 # 显示后20行
tail -f file1 # 实时跟踪
tail -F file1 # 跟踪(文件重建时继续)
tail -f -n 50 file1 # 从后50行开始跟踪
tail -f file1 | grep error # 过滤跟踪文件查找
find - 查找文件
bash
find ~ -name "*.txt" # 按名称查找
find ~ -iname "*.txt" # 忽略大小写
find ~ -type f # 查找文件
find ~ -type d # 查找目录
find ~ -size +100M # 大于100M
find ~ -size -1M # 小于1M
find ~ -mtime -7 # 7天内修改
find ~ -mtime +30 # 30天前修改
find ~ -user root # 按所有者查找
find ~ -perm 755 # 按权限查找
find ~ -empty # 查找空文件/目录组合条件:
bash
find ~ -name "*.txt" -type f
find ~ -name "*.txt" -o -name "*.log"
find ~ -name "*.txt" -exec rm {} \;
find ~ -name "*.txt" -exec cp {} /backup \;
find ~ -name "*.txt" | xargs rmmdfind - Spotlight 搜索
bash
mdfind "filename" # 搜索文件名
mdfind -name "filename" # 只搜索文件名
mdfind "kind:pdf" # 搜索 PDF 文件
mdfind "kind:image" # 搜索图片
mdfind "kind:document" # 搜索文档
mdfind "content:keyword" # 搜索内容
mdfind -onlyin ~/Documents "keyword" # 限定目录
mdfind 'kMDItemFSContentChangeDate >= $time.today' # 今天修改的文件locate - 快速查找
bash
locate file1 # 快速查找
locate -i file1 # 忽略大小写
locate -n 10 file1 # 限制结果数量
locate -r "\.txt$" # 使用正则表达式
sudo /usr/libexec/locate.updatedb # 更新数据库which - 查找命令位置
bash
which ls
which python
which -a python # 显示所有匹配whereis - 查找程序文件
bash
whereis ls
whereis -b ls # 只查找二进制
whereis -m ls # 只查找手册文件权限
chmod - 修改权限
数字方式:
bash
chmod 755 file1 # rwxr-xr-x
chmod 644 file1 # rw-r--r--
chmod 700 file1 # rwx------
chmod -R 755 dir1 # 递归修改符号方式:
bash
chmod u+x file1 # 所有者添加执行权限
chmod g-w file1 # 组移除写权限
chmod o=r file1 # 其他人只读
chmod a+x file1 # 所有人添加执行权限
chmod u=rwx,g=rx,o=r file1权限对照表:
| 数字 | 权限 | 说明 |
|---|---|---|
| 7 | rwx | 读+写+执行 |
| 6 | rw- | 读+写 |
| 5 | r-x | 读+执行 |
| 4 | r-- | 只读 |
| 3 | -wx | 写+执行 |
| 2 | -w- | 只写 |
| 1 | --x | 只执行 |
| 0 | --- | 无权限 |
chown - 修改所有者
bash
chown user1 file1 # 修改所有者
chown user1:group1 file1 # 修改所有者和组
chown :group1 file1 # 只修改组
chown -R user1 dir1 # 递归修改chgrp - 修改所属组
bash
chgrp group1 file1
chgrp -R group1 dir1xattr - 扩展属性
bash
xattr file1 # 列出属性
xattr -l file1 # 显示属性值
xattr -w attr_name value file1 # 设置属性
xattr -d attr_name file1 # 删除属性
xattr -c file1 # 删除所有属性
xattr -cr dir1 # 递归删除所有属性其他常用命令
echo - 输出文本
bash
echo "Hello World"
echo -n "No newline" # 不换行
echo -e "Line1\nLine2" # 解释转义字符
echo $PATH # 输出变量
echo $? # 输出上一命令退出状态date - 日期时间
bash
date # 显示当前时间
date "+%Y-%m-%d" # 格式化输出
date "+%Y-%m-%d %H:%M:%S"
date -v+1d # 明天
date -v+7d # 7天后
date -v-1d # 昨天
date -j -f "%Y-%m-%d" "2023-12-25" "+%A" # 解析日期history - 命令历史
bash
history # 显示历史命令
history 20 # 显示最近20条
!100 # 执行第100条命令
!! # 执行上一条命令
!ls # 执行最近的 ls 命令
!$ # 上一条命令的最后一个参数
history -c # 清空历史alias - 命令别名
bash
alias # 显示所有别名
alias ll='ls -la' # 设置别名
alias rm='rm -i'
unalias ll # 删除别名clear - 清屏
bash
clear # 清屏
Ctrl + L # 快捷键清屏open - 打开文件或应用
bash
open . # 在 Finder 中打开当前目录
open ~/Downloads # 打开下载目录
open -a "Visual Studio Code" . # 用 VS Code 打开
open -a "Sublime Text" file.txt
open -a Safari https://apple.com
open file.txt # 用默认程序打开
open -e file.txt # 用 TextEdit 打开
open -t file.txt # 用默认文本编辑器打开