Linux work note

Executive Summary

核心观点(金字塔原理)

结论先行: Linux命令行是系统管理和日常运维的核心工具,掌握高频命令能显著提升工作效率。

支撑论点:

  1. 文件操作命令(ls、cp、mv、rm、find)是日常工作的基础
  2. 系统管理命令(top、df、du、ps、kill)是运维监控的关键
  3. 文本处理命令(grep、sed、awk)是数据分析的利器

SWOT 分析

维度 分析
S 优势 命令分类清晰,涵盖文件操作、系统管理、网络传输等核心场景,实用性强
W 劣势 仅列出命令用法,缺少实际案例场景和组合使用技巧
O 机会 适合作为Linux入门速查手册,可快速定位所需命令
T 威胁 命令参数众多易混淆,部分危险命令(如rm -rf)使用不当可能造成数据丢失

适用场景

  • Linux系统日常运维和管理工作
  • 开发人员在服务器上的文件操作和调试
  • 系统管理员进行性能监控和故障排查

Linux高频cmd
  • rz : 上传
  • sz : 下载
  • ls -lrt : 浏览当前目录所有文件,-lrt 按照时间正序排序
  • grep : 搜索命令
  • top : 查看服务器性能指标
  • cp sourcefile targetfile :复制
  • mv sourcefiile targetfile :剪切/重命名
  • rm -rf
  • uname -a :显示系统所有属性
  • arch : 显示系统硬件架构
  • last -5 : 查看成功登录用户信息,显示前5条
  • lastb : 查看用户不成功登录信息,参数与上面的一样
  • who : 查看已登录用户
  • df :查看磁盘使用情况
  • ln :创建文件链接
  • du :查看文件占用空间
  • find :查找文件
  • locate :定位文件位置
  • tar zcvf /tmp/test.tar.gz /home/temp
  • unzip file.zip -d /target/dir : 解压 zip 文件到指定目录
  • gzip file / gunzip file.gz : 压缩/解压 gzip 格式文件
  • head -n 5 file :显示file的前5行
  • tail -10 file :显示file的尾几行
  • wc file :统计文件的内容
  • sort :排序
  • history : 查看历史cmd
  • wc ­ print the number of newlines, words, and bytes in files
  • Linux 文件权限由10位字符表示:第1位为文件类型(d=目录, -=文件, l=链接),后9位分三组:所有者(owner)、所属组(group)、其他用户(other)
  • 每组三位分别代表读(r=4)、写(w=2)、执行(x=1)权限,- 表示无此权限
  • 例如 drwxrw-r--:目录,所有者可读写执行,组用户可读写不可执行,其他用户只读
  • 数字表示法:chmod 764 file 等价于 rwxrw-r--
  • chmod – change file access permissions
  • chown – change file owner and group
  • su –  change user ID or become super­user
  • passwd – update  a user’s authentication tokens(s) 
  • who – show who is logged on
  • ps – report a snapshot of the current processes
  • kill – to kill a process(using signal mechanism) 
  • ssh – SSH client (remote login program)  Usage: ssh [options] [user]@hostname 
  • scp – secure copy (remote file copy program) Usage: scp [options] [[user]@host1:file1] [[user]@host2:file2]
  • fdisk – partition manipulator   eg. sudo fdisk ­l
  • mount – mount a file system  Usage: mount ­t type device dir  
  • umount – unmount file systems  Usage: umount [OPTIONS] dir   device…      
  • du – estimate file space usage   Usage:  du [OPTION]… [FILE]… 
  • df – report filesystem disk space usage  Usage: df [OPTION]… [FILE]…  
  • quota – display disk usage and limits   Usage: quota [OPTION]  
  • reboot – reboot the system   Usage: reboot [OPTION]    
  • poweroff – power off the system       Usage: poweroff [OPTION]
  • bg – make a foreground process to run in background   Usage: type ’ctrl+z’  and then ’bg <job id>’  
  • fg – to make background process as foreground process       Usage: fg [jobid]
  • jobs – displays the names and ids of background jobs   Usage: jobs
  • sed ­  stream editor for filtering and transforming text Usage: sed [OPTION] [input­file]… eg. sed ’s/love/hate/g’ loveletter.txt     
  • awk ­ pattern scanning and processing language   
  • find ­ search for files in a directory hierarchy     Usage: find [OPTION] [path] [pattern]        
  • locate – find or locate a file  Usage: locate [OPTION]… FILE…    
[root@www ~]# grep [-acinv] [--color=auto] '搜寻字符串' filename
选项与参数
-a  binary 文件以 text 文件的方式搜寻数据
-c 计算找到 '搜寻字符串' 的次数
-i 忽略大小写的不同所以大小写视为相同
-n 顺便输出行号
-v 反向选择亦即显示出没有 '搜寻字符串' 内容的那一行
--color=auto 可以将找到的关键词部分加上颜色的显示喔

# grep energywise *           #在当前目录搜索带'energywise'行的文件
# grep -r energywise *        #在当前目录及其子目录下搜索'energywise'行的文件
# grep -l -r energywise *     #在当前目录及其子目录下搜索'energywise'行的文件但是不显示匹配的行只显示匹配的文件

- 显示1000行到3000行
cat filename| head -n 3000 | tail -n +1000
*注意两种方法的顺序
分解
   tail -n 1000显示最后1000行
   tail -n +1000从1000行开始显示显示1000行以后的
   head -n 1000显示前面1000行

- 用sed命令
sed -n '5,10p' filename 这样你就可以只查看文件的第5行到第10行