Linux 常用命令.
文件操作命令.
1、文件处理命令.
ls [选项] [参数].
list
列出参数的属性信息
参数为路径名
常用选项
选项 | 说明 |
---|---|
- l | 显示目录下详细的文件信息,可简化为 ll |
- a | 显示目录下全部文件(含隐藏文件) |
- d | 查看目录属性 |
- t | 按创建时间顺序列出文件 |
- i | 输出文件inode编号 |
-R | 递归打印目录下所有文件信息 |
ll
查看到的字段信息依次是:
①文件类型和权限 ②文件硬连接 ③文件所有者 ④文件所有者所属组 ⑤文件占用空间 ⑥文件最近访问时间 ⑦文件名
cd 参数.
change directory
更改目录
参数为路径名
pwd.
print working directory
打印工作目录的绝对路径
touch 参数.
更新文件时间为当前时间,如果文件或目录不存在,就会自动创建一个
参数为文件或目录
mkdir [选项] 参数.
make directory
创建目录
参数为目录或路径名
选项
选项 | 说明 |
---|---|
- p | 若路径中的目录不存在,则先创建目录 |
- v | 查看文件创建过程 |
cp [选项] 源文件或目录 目的目录.
copy
复制,复制文件到指定目录,如果需要复制目录需要加 -R
选项
选项 | 说明 |
---|---|
- R | 递归复制 |
- p | 复制的同时不修改文件属性:所有者、所属组、权限、时间。。。 |
- f | 强行复制文件或目录,无论目的文件或目录是否已经存在 |
mv 源文件或目录 目的目录.
move
移动文件或目录,如果移动的文件在同一目录,但是不同名,相当于重命名
rm [选项] 文件或目录.
remove
移除文件或目录
选项
选项 | 说明 |
---|---|
- f | 强制删除目录或文件 |
- r | 递归删除 |
- i | 删除时,需要进行确认 |
rmdir [-p] 目录.
remove directory
删除目录,只能删除目录
-p
表示 会在删除指定目录后检查其上层,若该目录已变成空目录,则将其一并删除。
2、文件查看命令.
cat 文件名.
concatenate and display file
连接和显示文件,将文件中的内容打印到输出设备
more [文件名].
分页显示文件内容
快捷键
快捷键 | 说明 |
---|---|
f / Space | 显示下一页 |
Enter | 显示下一行 |
q / Q | 退出 |
head -n filename.
n 表示要查看的开头行数
tail -n filename.
n表示要查看的尾行数
3、权限管理命令.
使用权限管理命令前,需提升用户权限:sudo
chmod [a | u | g | o] {+ | - | =} rwx 文件或目录.
change the permissions mode of file
: 变更文件或目录的权限
a : 所用用户, u :当前用户, g :当前用户组, o :其他用户
+
添加权限 ,-
取消权限,=
设定权限
r : 读,w :写,x :执行
chmod a+rw xxx # 对文件xxx 所有用户都添加读写权限
可以使用chmod 440 xxx
代替上述
chown 用户 文件或目录.
change the owner of file
更改文件所有者
chgrp [组名] [文件或目录].
change file group
4、文件搜索命令.
which 命令.
查找命令所在的路径,能找到命令的别名记录
whereis 命令.
查找命令所在的路径,可以展示命令帮助文档的所在路径
find 搜索路径 [选项] 搜索关键字.
关键字可以是 文件名,文件大小,文件所有者。。
选项
选项 | 说明 |
---|---|
- name | 根据文件名查找 |
- size | 根据文件大小查找 |
- user | 根据文件所有者查找 |
locate [选项] 搜索关键字.
查找文件或目录
功能与 find -name相同,但更快(因为他遍历的是
/var/lib/locatedb
数据库,不是整个路径),可能出现文件不存在的情况,因此我们需要使用该命令之前,使用updatedb
命令手动更新数据库
grep 指定字符 源文件.
用于在文件中搜索与字符串匹配的行并输出
通常与 管道连用
ls | grep xxx # 查询当前路径中 文件名为xxx的文件
网络管理与通信命令.
ifconfig [参数].
interfaces config
配置和显示Linux内核中网络接口的参数
netstat [选项].
打印Linux系统中网络系统的状态信息
选项
选项 | 说明 |
---|---|
- a | 显示所有端口 |
- at | 显示tcp所有端口 |
- au | 显示udp所有端口 |
-r, --route display routing table
-I, --interfaces=<Iface> display interface table for <Iface>
-i, --interfaces display interface table
-g, --groups display multicast group memberships
-s, --statistics display networking statistics (like SNMP)
-M, --masquerade display masqueraded connections
-v, --verbose be verbose
-W, --wide don't truncate IP addresses
-n, --numeric don't resolve names
--numeric-hosts don't resolve host names
--numeric-ports don't resolve port names
--numeric-users don't resolve user names
-N, --symbolic resolve hardware names
-e, --extend display other/more information
-p, --programs display PID/Program name for sockets
-o, --timers display timers
-c, --continuous continuous listing
-l, --listening display listening server sockets
-a, --all display all sockets (default: connected)
-F, --fib display Forwarding Information Base (default)
-C, --cache display routing cache instead of FIB
-Z, --context display SELinux security context for sockets
ping [选项] [参数].
选项 | 说明 |
---|---|
- c | 设置回应次数 |
- s | 设置数据包大小 |
- v | 详细显示指令的执行过程 |