shell基础知识和常用命令

📅 2026/7/2 12:56:46 👁️ 阅读次数 📝 编程学习
shell基础知识和常用命令

一、Shell 提示符

  1. 默认 shell 为 bash,默认bash shell提示符是$;图形界面需打开终端。

  2. man 命令查手册:man -k搜关键词,q退出;Tab 键自动补全。

二、浏览文件系统

  1. 单一根目录//分隔路径,无盘符;磁盘通过挂载点接入目录树。

  2. 关键目录:/home用户目录、/root管理员目录、/var日志、/tmp临时文件、/media/mnt外设挂载点。

  3. 路径:绝对路径(从 / 开始)、相对路径(.当前、..上级)。

三、文件和目录列表

  1. pwd查看当前目录;cd切换目录。

  2. ls列文件:-l看权限详情、-a显示隐藏文件、-R递归、-F区分目录 / 文件;支持* ? [] !通配符过滤。

四、文件 / 目录管理

  1. 创建:touch建空文件;mkdir -p创建多级目录。

  2. 复制cp -i、移动重命名mv -i-i覆盖前提醒。

  3. 删除:rm删文件,rmdir仅删空目录;rm -rf强制递归删目录(高危)。

  4. 链接:ln硬链接(同 inode);ln -s软链接(快捷方式)。

五、处理文件

  1. file判断文件类型。

  2. cat全量输出;more/less分页浏览(less 可上下翻)。

  3. head -n看前 N 行;tail -n看末尾 N 行,tail -f实时监控日志。

创建项目结构:

pwd
mkdir -p ~/LearningNotes/{lectures,references,backups}
cd ~/LearningNotes/lectures
pwd

创建和编辑笔记:

pwd
# 创建两个空文件
touch linux_basics.txt commands_cheatsheet.txt

# 写入linux_basics.txt内容
cat > linux_basics.txt << EOF
Linux Command Basics
cd: Change directory
ls: List files and directories
pwd: Print working directory
cat: Display entire file
head: Show first 10 lines
tail: Show last 10 lines
less: Interactive file viewer
EOF

# 写入commands_cheatsheet.txt内容
cat > commands_cheatsheet.txt << EOF
cat: Display entire file
head: Show first 10 lines
tail: Show last 10 lines
less: Interactive file viewer
EOF

文件操作:

pwd
# 复制并重命名
cp commands_cheatsheet.txt ../references/command_reference.txt
# 重命名文件
mv linux_basics.txt linux_fundamentals.txt
# 创建备份文件
cp linux_fundamentals.txt ../backups/
# 更新时间戳
touch commands_cheatsheet.txt

查看和分析文件内容:

pwd
# 查看完整内容
cat linux_fundamentals.txt
# 查看前两行
head -n 2 ../references/command_reference.txt
# 查看后三行
tail -n 3 commands_cheatsheet.txt
# 交互式浏览
less ../references/command_reference.txt

项目维护:

pwd
# 切换到项目根目录
cd ~/LearningNotes
pwd
# 列出完整目录结构
tree
# 删除文件
rm references/command_reference.txt
# 创建空文件
touch project_status.txt
# 更新所有文件访问时间
touch lectures/* references/* backups/* project_status.txt

最终项目结构:

LearningNotes/
├── backups/
│ └── linux_fundamentals.txt
├── lectures/
│ ├── commands_cheatsheet.txt
│ └── linux_fundamentals.txt
├── references/
└── project_status.txt