今日学习目标
✅ 掌握文件和目录的增删改查
✅ 理解绝对路径和相对路径
✅ 学会使用通配符批量操作文件
第一部分:基础命令实战 打开你的CentOS虚拟机,登录后开始今天的练习:
1 2 cd ~/linux-learning/day2
1. pwd - 查看当前位置
2. ls - 列出文件和目录 1 2 3 4 5 6 7 8 9 10 11 ls ls -l ls -a ls -lh ls -lt ls -lS ls -l /etc | head -5 ls -la ~
3. cd - 切换目录 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 cd /etc cd ../../ cd ~ cd - cd .. cd ../.. cd /var/log pwd ls cd /tmp cd -
4. 创建文件和目录 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 mkdir project mkdir -p project/src/css mkdir project{1..5} touch readme.txt touch file{1..10}.txt mkdir -p mywebsite/{html,css,js,images,backup}cd mywebsitetouch html/index.htmltouch css/style.csstouch js/main.jstree .
5. cp - 复制文件 1 2 3 4 5 6 7 8 9 10 cp file1.txt file2.txt cp file1.txt backup/ cp -r project/ backup/ cp -p file1.txt file3.txt cp -a project/ project_backup/ cp -r mywebsite mywebsite_backup_$(date +%Y%m%d)ls -l
6. mv - 移动或重命名 1 2 3 4 5 6 7 8 9 10 11 12 mv oldname.txt newname.txt mv project/ project_old mv file1.txt backup/ mv *.txt backup/ mkdir documents images scriptsmv *.txt documents/ mv *.jpg *.png images/ 2>/dev/null
7. rm - 删除(小心使用!) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 rm file1.txt rm -f file2.txt rm -i file3.txt rm -r project/ rm -rf temp/ mkdir test_deletecd test_deletetouch a.txt b.txt c.txtrm -i *.txt cd ..rmdir test_delete
第二部分:文件查看命令实战 1 2 3 4 5 6 7 8 9 10 11 cd ~/linux-learning/day2cat > sample.txt << 'EOF' Linux is awesome I love learning Linux This is line 3 Linux commands are powerful Learning every day Line 6 is here The last line EOF
1. cat - 查看文件内容 1 2 3 cat sample.txt cat -n sample.txt cat /etc/passwd
2. less/more - 分页查看 1 2 3 4 5 6 7 8 less /var/log/messages more /etc/services
3. head/tail - 查看头尾 1 2 3 4 5 6 7 8 9 10 11 head sample.txt head -3 sample.txt tail sample.txt tail -5 sample.txt tail -f /var/log/messages tail -f /var/log/messages
4. grep - 文本搜索(超级重要!) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 grep "Linux" sample.txt grep -i "linux" sample.txt grep -n "line" sample.txt grep -v "Linux" sample.txt grep -c "Linux" sample.txt grep "^L" sample.txt grep "e$" sample.txt grep "L.*g" sample.txt grep "root" /etc/passwd grep -r "ERROR" /var/log/ ps aux | grep ssh
第三部分:实战项目 - 日志分析小工具 现在我们来做一个综合练习,创建一个日志分析脚本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 cd ~/linux-learning/day2cat > log_analyzer.sh << 'EOF' LOG_FILE="/var/log/messages" REPORT_DIR="$HOME /linux-learning/day2/reports" mkdir -p $REPORT_DIR REPORT_FILE="$REPORT_DIR /report_$(date +%Y%m%d_%H%M%S) .txt" echo "=========================================" > $REPORT_FILE echo " 系统日志分析报告 - $(date) " >> $REPORT_FILE echo "=========================================" >> $REPORT_FILE echo "" >> $REPORT_FILE echo "1. 日志文件信息:" >> $REPORT_FILE ls -lh $LOG_FILE >> $REPORT_FILE echo "" >> $REPORT_FILE ERROR_COUNT=$(grep -i "error" $LOG_FILE | wc -l) echo "2. 错误统计:" >> $REPORT_FILE echo " 错误总数: $ERROR_COUNT " >> $REPORT_FILE echo "" >> $REPORT_FILE echo "3. 最近的5条错误:" >> $REPORT_FILE grep -i "error" $LOG_FILE | tail -5 >> $REPORT_FILE echo "" >> $REPORT_FILE echo "4. 各服务日志量:" >> $REPORT_FILE echo " SSH: $(grep -c "sshd" $LOG_FILE) " >> $REPORT_FILE echo " Kernel: $(grep -c "kernel" $LOG_FILE) " >> $REPORT_FILE echo " Systemd: $(grep -c "systemd" $LOG_FILE) " >> $REPORT_FILE echo "" >> $REPORT_FILE echo "5. 最近24小时日志分布:" >> $REPORT_FILE for hour in {00..23}; do COUNT=$(grep " $(date +%b %d) $hour :" $LOG_FILE | wc -l) echo " $hour :00 - $hour :59 : $COUNT 条日志" >> $REPORT_FILE done echo "" >> $REPORT_FILE echo "报告生成时间: $(date) " >> $REPORT_FILE echo "=========================================" >> $REPORT_FILE echo "报告已生成: $REPORT_FILE " echo "报告摘要:" echo "- 错误总数: $ERROR_COUNT " echo "- 最近5条错误已记录" echo "- 按小时统计完成" if [ $ERROR_COUNT -gt 100 ]; then echo "⚠️ 警告:系统错误过多,建议检查!" fi EOF chmod +x log_analyzer.sh./log_analyzer.sh ls -l reports/cat reports/report_*.txt | head -20
第四部分:今日挑战任务 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 cd ~/linux-learning/day2mkdir challengecd challengemkdir -p challenge/{docs,data/{2024,2025},backup}touch challenge/docs/{report1.txt,report2.txt,notes.txt}cp challenge/docs/*.txt challenge/backup/mv challenge/data/2024 challenge/data/2023rmdir challenge/backup 2>/dev/null || echo "backup不为空" find /etc -name "*.conf" 2>/dev/null | wc -l find ~ -type f -mtime -7 2>/dev/null find / -type f -size +10M 2>/dev/null | head -10
第五部分:今日总结和作业 完成今天的学习后,你应该掌握了:
✅ 文件导航命令(pwd, cd, ls)
✅ 文件操作命令(touch, mkdir, cp, mv, rm)
✅ 文件查看命令(cat, less, head, tail)
✅ 文本搜索命令(grep)
✅ 能够创建简单的分析脚本
今日作业:
1 2 3 4 5 6 7 8 9 10 11 12 grep -i "warning" /var/log/messages | tail -10 ls /etc/*.conf ls /etc/rc*.d ls /var/log/*.log
常见问题解决
问题
解决方法
命令没找到
使用which 命令名查看是否安装
权限不足
使用sudo或以root执行
文件不存在
检查路径是否正确
删除错了
无法恢复! 所以删除前一定要确认
今日名言 :”在Linux中,一切都是文件” - 理解这句话,你就理解了Linux的一半。