运维小白学习记——02 Linux文件管理基础

📅 2026/7/19 7:40:28
运维小白学习记——02 Linux文件管理基础
一、命令的格式和辅助工具在Linux系统中每一条命令都遵循这个格式命令 选项 参数。我们以ls -la /var这个命令为例ls为命令-la为选项/var为参数。初学者要注意Linux系统命令几乎都是小写的命令和选项之间必须有空格运维的核心能力不是记住所有命令而是快速找到需要的命令。1.1 --help 快速查看命令帮助# ls 的命令帮助查看ls可使用的所有选项这里只截取了部分代码 waitwait:~$ ls --help Usage: ls [OPTION]... [FILE]... List information about the FILEs (the current directory by default). Sort entries alphabetically if none of -cftuvSUX nor --sort is specified. Mandatory arguments to long options are mandatory for short options too. -a, --all do not ignore entries starting with . -d, --directory list directories themselves, not their contents -h, --human-readable with -l and -s, print sizes like 1K 234M 2G etc. --si likewise, but use powers of 1000 not 1024 -i, --inode print the index number of each file -l use a long listing format -s, --size print the allocated size of each file, in blocks -t sort by time, newest first; see --time1.2 man 查看帮助手册man 手册分为 9 个章节不同章节存放不同类型的文档。当你执行man 命令时系统默认从第 1 章开始查找。如果想指定查看某个章节的内容可以用man章节号名称。章节名称内容说明1用户命令普通用户可以在shell中执行的命令日常用得最多2系统调用Linux内核提供的底层函数接口一般是C语言编程用的3库函数C标准库中的函数程序员开发时使用4特殊文件设备文件和/proc、/sys下的虚拟文件5文件格式与配置配置文件的格式说明6游戏Linux自带的游戏说明基本用不上7杂项协议、标准、宏等杂项说明8系统管理命令仅root用户使用的系统管理和维护命令9内核例程内核开发者使用的内部接口文档其中运维常用的3个章节是1、5、8。# 用 man -f 查看所有章节 waitwait:~$ man -f passwd passwd (1) - change user password passwd (1ossl) - OpenSSL application commands passwd (5) - password file二、文件目录与操作常见的9个目录目录用途/etc系统配置文件/var日志、缓存/tmp临时文件/home普通用户的家目录/rootroot 的家目录/bin基础命令/sbin系统管理命令/usr用户程序/proc进程信息2.1 pwd - 查看当前路径waitwait:~$ pwd /home/wait2.2 cd - 切换目录# 切换到绝对路径 waitwait:~$ cd /etc/ssh/ waitwait:/etc/ssh$ pwd /etc/ssh # 切换到用户家目录cd ~ 或直接 cd waitwait:/etc/ssh$ cd waitwait:~$ pwd /home/wait # 切换到根目录 waitwait:~$ cd / waitwait:/$ # 相对路径切换到 etc当前在/目录 waitwait:/$ cd etc/ waitwait:/etc$ # 切换到上级目录 waitwait:/etc$ cd .. waitwait:/$ # 切换到上一个所在的目录这个很实用的 waitwait:/etc/ssh$ cd waitwait:~$ cd - /etc/ssh waitwait:/etc/ssh$2.3 ls - 列出目录内容# 最常用组合详细全部 waitwait:~$ ls -la # 输出 #drwx------. 2 wait wait 83 Jul 14 11:40 . #drwxr-xr-x. 3 root root 18 Jul 14 11:32 .. #-rw-------. 1 wait wait 57 Jul 14 11:40 .bash_history #-rw-r--r--. 1 wait wait 18 Oct 29 2024 .bash_logout #-rw-r--r--. 1 wait wait 144 Oct 29 2024 .bash_profile #-rw-r--r--. 1 wait wait 522 Oct 29 2024 .bashrc2.4 mkdir - 创建目录# 创建目录 mkdir dir1 #一次创建多个目录 mkdir dir2 dir3 dir4 backup # -p 递归创建多级目录 mkdir -p dir1/docs/images2.5 touch - 创建文件# 创建空文件 touch file1.txt file2.txt # 批量创建10个文件file1 ~ file10 touch dir3/file{1..10} # 批量创建2个文件wait1、wait10 touch dir3/wait{1,10}2.6 cp - 复制文件或目录# 复制文件 cp file1.txt file1_backup.txt # 复制到/tmp cp file1.txt /tmp/ # 复制到当前目录.代表当前目录 cp /etc/passwd ./ # 复制目录必须加 -r cp -r dir1 dir1_backup cp -r dir1 /tmp/2.7 mv - 移动或重命名文件# 重命名 mv file1.txt data.txt # 移动到其他目录 mv data.txt dir1/docs/ # 移动所有txt文件 mv *.txt /var/tmp/2.8 rm - 删除文件或目录# 删除文件 rm /tmp/file1.txt # 交互式删除删除前确认更安全 rm -i /tmp/*.txt # 删除目录必须加 -r rm -r dir1_backup -f 强制删除 rm -rf dir2 dir3 backup #2.9 cat - 查看文件内容#查看文件内容 cat /etc/hosts #查看系统版本 cat /etc/os-release # -n 显示行号 cat -n /etc/hosts2.10 head - 显示文件开头部分 / tail - 显示文件末尾部分# head默认显示前10行 head /etc/passwd #显示前5行内容 head -n 5 /etc/passwd # tail默认显示后10行 tail /etc/passwd #显示最后20行 tail -n 20 /var/log/messages # tail -f实时追踪监控日志的神器 Rocky: tail -f /var/log/secure # 安全相关日志按 CtrlC 退出 Ubuntu: tail -f /var/log/auth.log # 安全相关日志以上就是今天的全部学习内容请大家耐心跟练上一篇https://blog.csdn.net/weixin_65758790/article/details/162880827?spm1001.2014.3001.5501