目录
一、认识shell
1、Linux Shell 解释器
- 是 Linux 系统中运行的一种特殊程序(软件)
- 位于用户和 Linux 内核之间充当命令解释器(翻译官)
- 用户登录 Linux 系统时,会被分配到一个 Shell 程序
- Bash 是 Linux 系统中默认使用的 Shell 程序(文件位于 /bin/bash)
- Shell 负责接收用户输入的操作命令、解释、提交给内核执行
- Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁
- Shell 既是一种命令语言,又是一种程序设计语言
- Shell 是指一种应用程序,这个应用程序提供了一个界面,用户通过这个界面访问操作系统内核的服务
- 用户登录Linux系统时,自动加载一个Shell程序
echo $SHELL # 查看用户的 shell 程序
ls -lh /bin/bash # 查看 bash 文件信息
cat /etc/shells # 查看操作系统支持的所有 shell 程序
命令示例:
echo $SHELL
ls -lh /bin/bash
cat /etc/shells
输出结果:
[root@MineGi ~]# echo $SHELL
/bin/bash
[root@MineGi ~]# ls -lh /bin/bash
-rwxr-xr-x. 1 root root 942K 4月 1 2020 /bin/bash
[root@MineGi ~]# cat /etc/shells
/bin/sh
/bin/bash
/usr/bin/sh
/usr/bin/bash
[root@MineGi ~]#
Linux 中的 shell 是系统的用户界面,它提供了用户与内核进行交互操作的一种接口。以下是关于 Linux 中 shell 的详细说明:
1. 定义与功能
- Shell是系统的用户界面,位于Linux内核与用户之间,充当了二者之间的解释器程序。
- 它接收用户输入的命令并把它们送入内核去执行。
- Shell有自己的编程语言,允许用户编写由shell命令组成的程序,这些程序具有与其他应用程序相同的效果。
- Shell支持多种编程结构,如循环结构和分支控制结构,可以用于实现复杂的任务自动化。
2. 存在形式
- 在Linux系统中,shell有两种主要的表现形式:一种是在无图形界面下的终端运行环境下的shell,另一种是桌面上运行的类似于Windows的MS-DOS运行窗口。
3. 主要功能
- 命令解释器:Shell能够解析用户输入的命令并执行相应的操作。
- 管道和重定向:Shell支持将一个命令的输出作为另一个命令的输入(管道),或者将命令的输入/输出重定向到文件中。
- 变量和环境变量:Shell允许用户定义和使用变量,以及设置和修改环境变量。
- 脚本和批处理:Shell支持编写脚本和批处理文件,用于自动化执行一系列命令或任务。
- 条件和循环:Shell支持条件语句和循环结构,可以根据条件执行不同的命令或重复执行一组命令。
- 命令行编辑和快捷键:Shell提供了丰富的命令行编辑功能和快捷键,如命令补全、历史命令查找和修改等。
- 调用外部程序:Shell可以调用并执行系统中的其他程序和脚本。
- 调试和错误处理:Shell提供了调试功能,可以输出错误信息和调试信息,帮助用户诊断和解决问题。
- 用户和权限管理:Shell可以用于管理用户和权限,包括用户的创建、删除、修改密码以及文件和目录的权限设置等。
4. 版本与类型
- Linux中的shell有多种不同的版本和类型,包括Bourne Shell、BASH(Bourne Again Shell)、Korn Shell、C Shell和Z Shell等。其中,BASH是GNU操作系统上默认的shell。
5. 使用方式
- 用户可以通过终端或图形界面中的shell窗口来输入和执行命令。
- 用户也可以编写shell脚本文件,并通过shell程序来执行这些脚本文件。
6. 基本语法
- Shell语法包括执行命令、变量赋值、输出变量、注释、重定向输入输出、管道、条件判断、循环、函数定义和调用等。
7. 应用示例
- Shell脚本可以用于各种自动化任务,如文件备份、系统监控、批量文件处理等。通过编写hell脚本,用户可以大大提高工作效率。
总之,Linux中的shell是用户与内核进行交互的重要工具,具有丰富的功能和灵活的使用方式。
2、命令的本质
其实就是一个程序,一串代码。而用一个程序得先安装这个程序 。如 ls 命令由代码构成,执行的任务是显示当前文件夹中的所有文件=程序
3、Linux 命令的分类
shell 分为内部命令和外部命令,一般先执行内部命令,hash 再执行外部命令
内部命令 | 外部命令 |
集成于Shell解释器程序内部的一些特殊指令,也称为内建(Built-in)指令 | Linux系统中能够完成特定功能的脚本文件或二进制程序 |
属于Shell的一部分 | 属于Shell解释器程序之外的命令 |
没有单独对应的系统文件 | 每个外部命令对应了系统中的一个文件 |
自动载入内存,可以直接使用 | 必须知道其对应的文件位置,由Shell加载后才能执行 |
- 内部命令
- 集成与 Shell 解释器程序内部的一些特殊指令,也称为内建(Built-in)指令
- 属于 Shell 内的一部分
- 没有单独对应的系统文件
- shell 运行时内部命令自动载入内存,可以之间使用
- 外部命令
- Linux 系统中能够完成特定功能的脚本文件或二进制程序文件
- 属于 Shell 解释器程序之外的程序文件
- 每个外部命令对应了系统中的一个程序文件和多个库文件
- 必须知道其对应的程序文件位置,由 Shell 加载后才能执行
Linux 命令可以按照多种方式进行分类,但以下是一些常见的分类方法:
1. 根据功能分类
- 文件和目录操作:如 ls, cd, pwd, cp, mv, rm, mkdir, rmdir 等。
- 文本处理:如 cat, tac, more, less, grep, sed, awk, sort, uniq, wc 等。
- 系统管理:如 top, htop, free, df, du, uptime, who, w, ps, kill, reboot, shutdown 等。
- 网络管理:如 ping, traceroute, ifconfig, ip, netstat, ss, telnet, nc (netcat) 等。
- 压缩和解压:如 gzip, gunzip, bzip2, tar, unzip 等。
- 用户管理:如 useradd, userdel, passwd, su, sudo 等。
- 包管理(仅在某些Linux发行版中,如Debian、Ubuntu和RPM系的):如 apt, apt-get, apt-cache, dpkg, yum, dnf 等。
- 进程间通信:如 ipcs, ipcrm, msgget, msgsnd, msgrcv 等(这些命令在普通用户中不常用)。
- 硬件管理:如 lspci, lsusb, hdparm 等。
- 安全:如 sshd, sshpass, ssh-keygen, chattr, lsattr 等。
- 其他:如 date, cal, man, info, help 等。
2. 根据来源分类
- Shell 内置命令:这些是shell(如bash, zsh, dash等)自带的命令,如 cd, echo, eval, exec, exit, read 等。
- 外部命令:这些是作为单独的程序文件存在的命令,如 ls, cp, mv 等。
3. 根据权限要求分类
- 需要特殊权限的命令:如 reboot, shutdown, useradd, passwd 等,这些命令通常需要root权限才能执行。
- 普通用户命令:如 ls, cd, cat 等,这些命令可以由普通用户执行。
4. 根据命令行的界面
- 命令行界面 (CLI):如上述的大多数命令。
- 图形用户界面 (GUI):尽管Linux主要是一个CLI系统,但也有许多GUI工具和程序(如Nautilus, GNOME的文件管理器等),它们也提供了与命令行工具类似的功能。
5. 根据与Shell的交互方式
- 管道命令:如 grep, awk, sed 等,它们经常与管道 | 一起使用,从一个命令的输出中读取数据,并处理这些数据。
- 非管道命令:大多数命令都属于此类,它们直接从命令行参数、环境变量或标准输入中读取数据,并将结果输出到标准输出或标准错误。
这只是一个大致的分类,实际上Linux命令的复杂性和多样性远远超过了这些简单的分类。
3.1 查看内部命令
help # 查看有哪些内部命令
man cd |grep -A6 ^NAME # 查看有哪些内部命令
命令示例:
help
man cd |grep -A6 ^NAME
输出结果:
[root@MineGi ~]# help
GNU bash, 版本 4.2.46(2)-release (x86_64-redhat-linux-gnu)
这些 shell 命令是内部定义的。请输入 `help' 以获取一个列表.
输入 `help 名称' 以得到有关函数`名称'的更多信息.
使用 `info bash' 来获得关于 shell 的更多一般性信息
使用 `man -k' 或 `info' 来获取不在列表中的命令的更多信息.
名称旁边的星号 (*) 意味着该命令被禁用.
job_spec [&] history [-c] [-d 偏移量] [n] 或 history -a>
(( 表达式 )) if 命令; then 命令; [ elif 命令; then 叾
. 文件名 [参数] jobs [-lnprs] [任务声明 ...] 或 jobs -x
: kill [-s 信号声明 | -n 信号编号 | -信>
[ 参数... ] let 参数 [参数 ...]
[[ 表达式 ]] local [option] 名称[=值] ...
alias [-p] [名称[=值] ... ] logout [n]
bg [任务声明 ...] mapfile [-n 计数] [-O 起始序号] [-s 计
bind [-lpvsPVS] [-m 键映射] [-f 文件名] [> popd [-n] [+N | -N]
break [n] printf [-v var] 格式 [参数]
builtin [shell 内嵌 [参数 ...]] pushd [-n] [+N | -N | 目录]
caller [表达式] pwd [-LP]
case 词 in [模式 [| 模式]...) 命令 ;;]..> read [-ers] [-a 数组] [-d 分隔符] [-i 缓>
cd [-L|[-P [-e]]] [dir] readarray [-n 计数] [-O 起始序号] [-s 謾
command [-pVv] 命令 [参数 ...] readonly [-aAf] [name[=value] ...] or readonly>
compgen [-abcdefgjksuv] [-o 选项] [-A 动作> return [n]
complete [-abcdefgjksuv] [-pr] [-DE] [-o 选项> select NAME [in 词语 ... ;] do 命令; done
compopt [-o|+o 选项] [-DE] [名称 ...] set [-abefhkmnptuvxBCHP] [-o option-name] [--]>
continue [n] shift [n]
coproc [名称] 命令 [重定向] shopt [-pqsu] [-o] [选项名 ...]
declare [-aAfFgilrtux] [-p] [name[=value] ...] source 文件名 [参数]
dirs [-clpv] [+N] [-N] suspend [-f]
disown [-h] [-ar] [任务声明 ...] test [表达式]
echo [-neE] [参数 ...] time [-p] 管道
enable [-a] [-dnps] [-f 文件名] [名称 ...]> times
eval [参数 ...] trap [-lp] [[参数] 信号声明 ...]
exec [-cl] [-a 名称] [命令 [参数 ...]] [缠 真
exit [n] type [-afptP] 名称 [名称 ...]
export [-fn] [名称[=值] ...] 或 export -p typeset [-aAfFgilrtux] [-p] name[=value] ...
伪 ulimit [-SHacdefilmnpqrstuvx] [限制]
fc [-e 编辑器名] [-lnr] [起始] [终结] 传 umask [-p] [-S] [模式]
fg [任务声明] unalias [-a] 名称 [名称 ...]
for 名称 [in 词语 ... ] ; do 命令; done unset [-f] [-v] [名称 ...]
for (( 表达式1; 表达式2; 表达式3 )); d> until 命令; do 命令; done
function 名称 { 命令 ; } 或 name () { 命⼠ variables - 一些 shell 变量的名称和含>
getopts 选项字符串 名称 [参数] wait [编号]
hash [-lr] [-p 路径名] [-dt] [名称 ...] while 命令; do 命令; done
help [-dms] [模式 ...] { 命令 ; }
[root@MineGi ~]# man cd |grep -A6 ^NAME
NAME
bash, :, ., [, alias, bg, bind, break, builtin, caller, cd, command, compgen, complete,
compopt, continue, declare, dirs, disown, echo, enable, eval, exec, exit, export, false,
fc, fg, getopts, hash, help, history, jobs, kill, let, local, logout, mapfile, popd,
printf, pushd, pwd, read, readonly, return, set, shift, shopt, source, suspend, test,
times, trap, true, type, typeset, ulimit, umask, unalias, unset, wait - bash built-in com‐
mands, see bash(1)
[root@MineGi ~]#
3.2 type命令
区别内部和外部命令
type cd # 内部命令
type mkdir # 外部命令。带命令的绝对路径。
type ls # 默认显示别名
type -a ls # 加上 -a 选项,显示全部
type echo # 内部命令
type -a echo # 既是内部命令,又是外部命令
type type # 本身为内部命令
type MineGi # 显示未找到
echo $PATH # 系统查找命令的路径
命令示例:
type cd
type mkdir
type ls
type -a ls
type echo
type -a echo
type type
type MineGi
echo $PATH
输出结果:
[root@MineGi ~]# type cd
cd 是 shell 内嵌
[root@MineGi ~]# type mkdir
mkdir 已被哈希 (/usr/bin/mkdir)
[root@MineGi ~]# type ls
ls 是 `ls --color=auto' 的别名
[root@MineGi ~]# type -a ls
ls 是 `ls --color=auto' 的别名
ls 是 /usr/bin/ls
[root@MineGi ~]# type echo
echo 是 shell 内嵌
[root@MineGi ~]# type -a echo
echo 是 shell 内嵌
echo 是 /usr/bin/echo
[root@MineGi ~]# type type
type 是 shell 内嵌
[root@MineGi ~]# type MineGi
-bash: type: MineGi: 未找到
[root@MineGi ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@MineGi ~]#
3.3 whereis命令
查找命令具体的位置所在
命令示例:
whereis ls
输出结果:
[root@MineGi ~]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz
[root@MineGi ~]#
3.4 enable命令
开启或关闭内部命令
关闭:enable -n 内部命令
关闭:enable 内部命令
enable |grep cd # 从已启用命令中过滤出 cd 命令
help |grep -w cd # 从已启用命令中过滤出 cd 命令
enable -n cd # 禁用 cd 命令
pwd # 查看一下当前路径
cd /opt # 切换到/opt目录下
pwd # 再次查看当前路径,并没有切换成功
enable |grep cd # 从已启用命令中过滤出 cd 命令,并没有结果
help |grep -w cd # 从已启用命令中过滤出 cd 命令,和之前比,前面家里一个 *
enable cd # 启用 cd 命令
enable |grep cd # 从已启用命令中过滤出 cd 命令
help |grep -w cd # 从已启用命令中过滤出 cd 命令
cd /opt # 切换到/opt目录下
pwd # 查看一下当前路径
cd # 切换到家目录下
命令示例:
enable |grep cd
help |grep -w cd
enable -n cd
pwd
cd /opt
pwd
enable |grep cd
help |grep -w cd
enable cd
enable |grep cd
help |grep -w cd
输出结果:
[root@MineGi ~]# enable |grep cd
enable cd
[root@MineGi ~]# help |grep -w cd
cd [-L|[-P [-e]]] [dir] readarray [-n 计数] [-O 起始序号] [-s ?
[root@MineGi ~]# enable -n cd
[root@MineGi ~]# pwd
/root
[root@MineGi ~]# cd /opt
[root@MineGi ~]# pwd
/root
[root@MineGi ~]# enable |grep cd
[root@MineGi ~]# help |grep -w cd
*cd [-L|[-P [-e]]] [dir] readarray [-n 计数] [-O 起始序号] [-s ?
[root@MineGi ~]# enable cd
[root@MineGi ~]# enable |grep cd
enable cd
[root@MineGi ~]# help |grep -w cd
cd [-L|[-P [-e]]] [dir] readarray [-n 计数] [-O 起始序号] [-s ?
[root@MineGi ~]# cd /opt
[root@MineGi /opt]# pwd
/opt
[root@MineGi /opt]# cd
[root@MineGi ~]#
3.5 hash命令
缓存已使用过的命令
- 外部命令使用过后会被记录在内存(缓存,但是会占用内存,临时性的),可以加快读取速度。
- 如果将缓存的外部命令移到其他默认的目录中,无法再次使用,需要清掉hash值之后 ,才可以继续使用
hash -d # 单独删除缓存里某个的命令
hash -r # 清空 hash 命令缓存
hash -r # 删除所有的 hash
hash # 列出 hash 列表
touch 1.txt # 创建测试文件
ls 1.txt # 列出一下文件
cat 1.txt # 查看一下文件
hash # 列出 hash 列表
hash -d ls # 从 hash 列表删除 ls
hash |grep ls # 从 hash 列表筛选 ls
hash -r # 删除所有的 hash
hash # 列出 hash 列表,此时为空
ls /etc/passwd # 列出一下文件
hash # 列出 hash 列表
mv /usr/bin/ls /usr/sbin/ls # 将 ls 命令移动一下
ls /etc/passwd # 提示命令找不到,是因为 hash 列表记录的还是旧的路径
/usr/sbin/ls /etc/passwd # 使用绝对路径
hash # hash 列表还是旧的命令路径
hash -r # 清空 hash 列表
ls /etc/passwd # 就可以直接使用 ls 命令了
hash # hash列表替换了新的命令路径
mv /usr/sbin/ls /usr/bin/ls # 还原之前的路径
ls /etc/passwd # 这次也能不能直接使用
hash -r # 清空 hash 列表
ls /etc/passwd # 就可以直接使用 ls 命令了
命令示例:
hash -r
hash
touch 1.txt
ls 1.txt
cat 1.txt
hash
hash -d ls
hash |grep ls
hash -r
hash
ls /etc/passwd
hash
mv /usr/bin/ls /usr/sbin/ls
ls /etc/passwd
/usr/sbin/ls /etc/passwd
hash
hash -r
ls /etc/passwd
hash
mv /usr/sbin/ls /usr/bin/ls
ls /etc/passwd
hash -r
ls /etc/passwd
输出结果:
[root@MineGi ~]# hash -r
[root@MineGi ~]# hash
hash: 哈希表为空
[root@MineGi ~]# touch 1.txt
[root@MineGi ~]# ls 1.txt
1.txt
[root@MineGi ~]# cat 1.txt
[root@MineGi ~]# hash
命中 命令
1 /usr/bin/cat
1 /usr/bin/touch
1 /usr/bin/ls
[root@MineGi ~]# hash -d ls
[root@MineGi ~]# hash |grep ls
[root@MineGi ~]# hash -r
[root@MineGi ~]# hash
hash: 哈希表为空
[root@MineGi ~]# ls /etc/passwd
/etc/passwd
[root@MineGi ~]# hash
命中 命令
1 /usr/bin/ls
[root@MineGi ~]# mv /usr/bin/ls /usr/sbin/ls
[root@MineGi ~]# ls /etc/passwd
-bash: /usr/bin/ls: 没有那个文件或目录
[root@MineGi ~]# /usr/sbin/ls /etc/passwd
/etc/passwd
[root@MineGi ~]# hash
命中 命令
1 /usr/bin/mv
2 /usr/bin/ls
[root@MineGi ~]# hash -r
[root@MineGi ~]# ls /etc/passwd
/etc/passwd
[root@MineGi ~]# hash
命中 命令
1 /usr/sbin/ls
[root@MineGi ~]# mv /usr/sbin/ls /usr/bin/ls
[root@MineGi ~]# ls /etc/passwd
-bash: /usr/sbin/ls: 没有那个文件或目录
[root@MineGi ~]# hash -r
[root@MineGi ~]# ls /etc/passwd
/etc/passwd
[root@MineGi ~]#
4、命令执行过程
- 首先判断是内部还是外部命令,如果是内部命令直接执行
- 如果是外部命令,先看缓存空间是否有该外部命令,如果有按照hash缓存空间中的位置执行,如果缓存位置中没有直接报错
- 如果是第一次执行外部命令,去看系统中规定的那五个特殊文件夹中是否有,有就直接执行,没有报错命令不存在
5、命令不存在
输入命令,提示该命令不存在
- 该命令输写错误
- 命令没有安装到 Linux 系统中
- 命令不在规定的路径下
- 运行过该命令,但是命令所在的位置发生了改变
6、命令提示符
# 表示 root 用户登录
$ 表示的是普通用户
useradd 用户名 # 添加用户
su - 用户名 # 切换用户
[testuser@MineGi ~]$ # testuser:当前登录的用户名;MineGi:当前主机名;~:当前用户的家目录
命令示例:
useradd testuser
su - testuser
exit
输出结果:
[root@MineGi ~]# useradd testuser
[root@MineGi ~]# su - testuser
[testuser@MineGi ~]$ exit
登出
[root@MineGi ~]#
二、Linux常用命令
1、Linux 命令的通用格式
1.1 简单说明
命令字 [选项]... [参数]...
命令字
- 是整条命令中最关键的一部分(不可忽略)
- 唯一确定一条命令
选项
- 短格式选项:使用 “-” 符号引导,后面通常是单个字母。多个单字符选项可以组合在一起使用,例如:-a -l -h = -alh
- 长格式选项:使用 “--” 符号引导,后面通常是一个完整单词。
参数
- 是命令字的处理对象,可以是文件名、目录(路径)名或用户名等内容,个数可以是零个到多个。
- 注意:部分命令中选项和参数的位置可以互换
注意
- 没有被括号括起来的表示必选的,例如:命令字
- [] 括起来的表示可有可无
- {a|b|c} 表示 abc 中必须也只能选一个
- ... 表示前面的部分可以出现多个
例如
- 用法:mkdir [选项]... 目录...
- mkdir 目录必须写,选项可有可无,可以出现多个;目录必须写,目录可以出现多个
Linux 命令的通用格式以英文表示模式:
command [options] [arguments]
- command:这是你要执行的命令的名称。
- options(也称为标志、开关或参数):这些是可选的,用于修改命令的行为。通常,它们以短横线 - 开头,后跟一个或多个字母(如 -a、-b)或者两个短横线 -- 开头,后跟一个或多个单词(如 --all、--verbose)。有些命令还支持不带短横线的单字母选项(尽管这不太常见)。
- arguments:这些是传递给命令的参数,用于指定命令操作的对象或数据。它们通常紧跟在选项之后,且没有特定的前缀。
1.2 示例说明
1. ls 命令:列出目录内容
ls -l /home/user
在这里,ls 是命令,-l 是选项(用于长格式列表),/home/user 是参数(指定要列出内容的目录)。
2. cp 命令:复制文件或目录
cp -r source_dir destination_dir
在这里,cp 是命令,-r 是选项(用于递归复制目录),source_dir 和 destination_dir 是参数(分别指定源目录和目标目录)。
3. grep 命令:在文件中搜索匹配的行
grep "pattern" file.txt
在这里,grep 是命令,"pattern" 是要在文件中搜索的模式(尽管它用引号括起来,但它不是选项或参数,而是搜索查询),file.txt 是参数(指定要搜索的文件)。
注意:不是所有的命令都支持选项和参数,而且不同的命令可能有不同的选项和参数。你可以通过 man 命令(如 man ls)查看特定命令的手册页,以获取有关其选项和参数的详细信息。
2、Linux 命令行快捷键
Linux 命令行快捷键在提高工作效率方面起着至关重要的作用。以下是一些常用的 Linux 命令行快捷键,按照不同的功能进行分类:
1. 光标移动
- Ctrl + A:将光标移动到命令行的开头(等价Home键)。
- Ctrl + E:将光标移动到命令行的结尾( 等价End键)。
- Ctrl + B:将光标向前移动一个字符(等价←键)。
- Ctrl + F:将光标向后移动一个字符(等价→键)。
- Alt + B(某些终端可能支持):将光标向左移动一个单词。
- Alt + F(某些终端可能支持):将光标向右移动一个单词。
2. 文本操作
- Ctrl + K:删除光标位置到行尾的所有内容。
- Ctrl + U:删除光标位置到行首的所有内容。
- Ctrl + Y:将之前删除的内容粘贴回来。
- Ctrl + W:删除光标之前的单词。
- Ctrl + T:交换光标所在位置的字符。
3. 命令执行与中断
- Ctrl + C:中断当前正在运行的命令。
- Ctrl + Z:挂起当前进程(即将其放到后台),并返回到Shell界面。
- Ctrl + D(在行首时):退出当前终端。类似输入exit
4. 命令历史
- Ctrl + R:在历史命令中进行反向搜索,输入关键字进行搜索,并显示最近匹配的历史命令。
- Ctrl + P:显示上一个命令。
- Ctrl + N:显示下一个命令。
- Esc + .(点号):获取上一条命令的最后部分,用空格分隔开来的部分。
5. 其他常用快捷键
- Tab:自动补全命令、文件名或路径或者列出符合开头的命令。
- Ctrl + L:清屏(清除终端上的内容)。
- Ctrl + S:锁屏(在某些终端中可能有效)。
- Ctrl + Q:解除锁屏(与Ctrl + S配合使用)。
6. 复制与粘贴
- Ctrl + Ins:复制(在某些终端中可能有效)。
- Shift + Ins:粘贴(在某些终端中可能有效)。
7. 多终端管理
- Ctrl + Alt + →:切换到下一个终端会话(如果支持多终端)。
- Ctrl + Alt + ←:切换到上一个终端会话(如果支持多终端)。
请注意,某些快捷键可能因终端模拟器或Shell的不同而有所差异。此外,某些快捷键(如Alt组合键)在某些键盘布局或终端中可能不可用或需要不同的配置。在使用时,建议查阅特定终端或Shell的文档以获取最准确的信息。
除了上述提到的常用Linux命令行快捷键外,还有一些其他的快捷键和技巧可以帮助你进一步提高工作效率。以下是一些额外的建议:
8. 自动补全与路径补全
- 当你在命令行中输入命令、文件名或路径时,你可以按 Tab 键进行自动补全。如果只有一个匹配项,那么它将被自动补全;如果有多个匹配项,你将听到一个提示音,并且你需要再次按 Tab 键来查看所有可能的匹配项。
- 如果你想补全一个目录路径,但不确定具体的目录名,你可以输入部分目录名,然后连续按两次 Tab 键,这样系统就会列出所有匹配的目录。
9. 命令别名
- 你可以使用 alias 命令为常用命令或复杂的命令组合设置别名,以提高输入速度。例如,你可以使用 alias ll='ls -l' 命令来设置 ll 作为 ls -l 的别名。
- 别名设置后,你可以直接在命令行中输入别名来执行相应的命令。
10. 命令历史与导航
- 除了使用 Ctrl + R 进行历史命令搜索外,你还可以使用 Ctrl + P(或上箭头)和 Ctrl + N(或下箭头)来浏览历史命令。
- 如果你想重新执行上一个命令,你可以直接按 Enter 键或输入 !! 并按 Enter 键。
- 如果你想重新执行上一个命令并对其进行修改,你可以使用 ! 加命令的开头部分(如 !ls)来重新执行以 ls 开头的最近一个命令。
11. 通配符与扩展
- 在Linux命令行中,你可以使用通配符(如 *、? 和 [])来匹配文件名或路径。例如,ls *.txt 将列出所有以 .txt 结尾的文件。
- 你还可以使用花括号 {} 进行扩展,以生成多个命令或文件名。例如,echo {a,b,c}.txt 将输出 a.txt b.txt c.txt。
- 反斜杠"\",强制换行(续行符)和转义
12. 管道与重定向
- 管道(|)可以将一个命令的输出作为另一个命令的输入。例如,ls | grep .txt 将列出当前目录下所有包含 .txt 的文件名。
- 重定向(>、>> 和 <)可以改变命令的输入和输出方向。例如,ls > output.txt 将 ls 命令的输出保存到 output.txt 文件中;command < input.txt 将 input.txt 文件的内容作为 command 命令的输入。
13. 自定义快捷键
- 如果你发现默认的快捷键不符合你的使用习惯,你可以尝试自定义快捷键。这通常需要在你的终端模拟器或Shell的配置文件中进行设置。具体的设置方法取决于你使用的终端模拟器和Shell。
14. 使用脚本自动化任务
- 对于经常需要重复执行的任务,你可以编写Shell脚本来自动化这些任务。Shell脚本可以包含多个命令和逻辑判断,从而大大提高工作效率。
15. 学习更多快捷键和技巧
- Linux命令行提供了许多其他的快捷键和技巧,你可以通过查阅相关文档、教程或在线资源来学习更多内容。随着你对Linux命令行的熟悉程度加深,你会发现更多的快捷键和技巧可以帮助你提高工作效率。
3、常用命令
3.1 help命令
3.1.1 内部命令
查看shell内部命令的帮助信息
格式说明(扩展)
- [ ]:表示可选项
- CAPS或< >:表示可变化的数据
- ...:表示一个列表可加多个一个或多个
- x|y|z:表示或的意思
- -abc:是-a -b -c (有些命令选项有先后顺序) 多个 -abc 一般而言是没有顺序先后 (绝大多数是没有的,只有少数分先后)
命令示例:
help cd
help
输出结果:
[root@MineGi ~]# help cd
cd: cd [-L|[-P [-e]]] [dir]
Change the shell working directory.
Change the current directory to DIR. The default DIR is the value of the
HOME shell variable.
The variable CDPATH defines the search path for the directory containing
DIR. Alternative directory names in CDPATH are separated by a colon (:).
A null directory name is the same as the current directory. If DIR begins
with a slash (/), then CDPATH is not used.
If the directory is not found, and the shell option `cdable_vars' is set,
the word is assumed to be a variable name. If that variable has a value,
its value is used for DIR.
Options:
-L force symbolic links to be followed
-P use the physical directory structure without following symbolic
links
-e if the -P option is supplied, and the current working directory
cannot be determined successfully, exit with a non-zero status
The default is to follow symbolic links, as if `-L' were specified.
Exit Status:
Returns 0 if the directory is changed, and if $PWD is set successfully when
-P is used; non-zero otherwise.
[root@MineGi ~]# help
GNU bash, 版本 4.2.46(2)-release (x86_64-redhat-linux-gnu)
这些 shell 命令是内部定义的。请输入 `help' 以获取一个列表.
输入 `help 名称' 以得到有关函数`名称'的更多信息.
使用 `info bash' 来获得关于 shell 的更多一般性信息
使用 `man -k' 或 `info' 来获取不在列表中的命令的更多信息.
名称旁边的星号 (*) 意味着该命令被禁用.
......
3.1.2 外部命令
“--help”选项,适用于大多数外部命令
命令示例:
which --help
输出结果:
[root@MineGi ~]# which --help
Usage: /usr/bin/which [options] [--] COMMAND [...]
Write the full path of COMMAND(s) to standard output.
--version, -[vV] Print version and exit successfully.
--help, Print this help and exit successfully.
--skip-dot Skip directories in PATH that start with a dot.
--skip-tilde Skip directories in PATH that start with a tilde.
--show-dot Don't expand a dot to current directory in output.
--show-tilde Output a tilde for HOME directory for non-root.
--tty-only Stop processing options on the right if not on tty.
--all, -a Print all matches in PATH, not just the first
--read-alias, -i Read list of aliases from stdin.
--skip-alias Ignore option --read-alias; don't read stdin.
--read-functions Read shell functions from stdin.
--skip-functions Ignore option --read-functions; don't read stdin.
Recommended use is to write the output of (alias; declare -f) to standard
input, so that which can show aliases and shell functions. See which(1) for
examples.
If the options --read-alias and/or --read-functions are specified then the
output can be a full alias or function definition, optionally followed by
the full path of each command used inside of those.
Report bugs to <which-bugs@gnu.org>.
[root@MineGi ~]#
3.2 man命令
man 1(可省略) 命令 # 显示命令的用法
man 5 passwd # 查看passwd文件的格式
使用 man 命令阅读手册页
适用于大多数外部命令
- 翻屏:
- 向后翻一屏:SPACE PgDn
- 向前翻一屏:b PgUp
- 向后翻一行:ENTER 下键
- 向前翻一行:k 上键
- 查找:
- /KEYWORD: 向后
- n: 下一个
- N:前一个
- ?KEYWORD:向前
- n: 下一个
- N:前一个
- qQ: 退出
按 Q 或 q 键退出阅读环境、按“/” 键从上往下查找内容(“?”从下往上查找)n表示下一个,N表示上一个
man 7 man
1:用户命令(user command)(/bin, /usr/bin, /usr/local/bin)
2:系统调用(system calls)
3:库用户(library calls)
4:特殊文件(设备文件)(special files)
5:文件格式与协议(配置文件的语法)(file formats and conventions)
6:游戏(games)
7:公约和杂项(conventions and Miscellaneous)
8:管理命令(system management commands)(/sbin, /usr/sbin, /usr/local/sbin)
9:内核程序(kernel routines)
man 5 passwd # 可能没有相关内容
yum install -y man-pages # 需要安装 man-pages 包
man 5 passwd # 查看 passwd 文件的帮助信息
man mkdir # 查看 mkdir 的帮助信息,按 q 退出
命令示例:
man 5 passwd
yum install -y man-pages
man 5 passwd
man mkdir
输出结果:
[root@MineGi ~]# man 5 passwd
在第 5 节中没有关于 passwd 的手册页条目。
[root@MineGi ~]# yum install -y -q man-pages
软件包 man-pages-3.53-5.el7.noarch 已安装并且是最新版本
[root@MineGi ~]# man 5 passwd
[root@MineGi ~]# man mkdir
[root@MineGi ~]#
3.3 info命令
查看帮助,适用于大多数外部命令,按 q 退出
命令示例:
info mkdir
输出结果:
[root@MineGi ~]# info mkdir
[root@MineGi ~]#
Linux七大文件属性
字符 | 说明 |
- | 普通文件文本文件,包括纯文本文件、二进制文件、各种压缩文件等 |
c | 字符设备文件,一次只能录入一个字符,比如键盘 |
b | 块设备文件,保存大块数据的设备,硬盘读写文件时是一整块一整块的读和写,比如最常见的硬盘 |
s | 套接字文件,根据ip和端口号传递信息,一般隐藏在 /var/run/ 目录下,用于进程间的网络通信 |
p | 管道文件,主要用于进程间通信单向传输 (| 表示管道) |
l | 符号软连接快捷方式 |
d | 目录文件 |
命令示例:
find / -type p
ls -ld /run/dmeventd-client /dev/{console,sda,log,stdin,pts} /etc/passwd
输出结果:
[root@MineGi ~]# find / -type p
/run/dmeventd-client
/run/dmeventd-server
/run/systemd/inhibit/1.ref
/run/systemd/sessions/23.ref
/run/systemd/sessions/22.ref
/run/systemd/sessions/3.ref
/run/systemd/sessions/2.ref
/run/systemd/initctl/fifo
[root@MineGi ~]# ls -ld /run/dmeventd-client /dev/{console,sda,log,stdin,pts} /etc/passwd
crw------- 1 root root 5, 1 11月 29 08:14 /dev/console
srw-rw-rw- 1 root root 0 11月 29 08:13 /dev/log
drwxr-xr-x 2 root root 0 11月 29 08:13 /dev/pts
brw-rw---- 1 root disk 8, 0 11月 29 08:14 /dev/sda
lrwxrwxrwx 1 root root 15 11月 29 08:13 /dev/stdin -> /proc/self/fd/0
-rw-r--r--. 1 root root 879 11月 22 10:07 /etc/passwd
prw------- 1 root root 0 11月 29 08:14 /run/dmeventd-client
[root@MineGi ~]#
3.4 pwd命令
Printworking directory 显示当前所在目录的完整路径
命令示例:
pwd
输出结果:
[root@MineGi ~]# pwd
/root
[root@MineGi ~]#
3.5 cd命令
Changedirectory 改变用户所在的工作路径
绝对路径 | 从根(/)开始描述文件的位置 | 有且唯一 | 无论在哪里都可以准确找到某一个文件 |
相对路径 | 从当前路径开始描述文件的位置 | 有可能不唯一 | 如果参照物变了,就找不到文件 |
pwd # 列出当前所在路径
cd /etc/ # 使用绝对路径,切换到 /etc 目录下
pwd # 列出当前所在路径
cd sysconfig/network-scripts/ # 使用相对路径,切换到 sysconfig/network-scripts/ 目录下,切换成功
pwd # 列出当前所在路径
cd sysconfig/network-scripts/ # 使用相对路径,切换到 sysconfig/network-scripts/ 目录下,切换失败
pwd # 列出当前所在路径
cd /etc/sysconfig/network-scripts # 使用绝对路径,切换路径,不论当前路径在哪,切换多少次都可以
cd /etc/sysconfig/network-scripts # 使用绝对路径,切换路径,不论当前路径在哪,切换多少次都可以
pwd # 列出当前所在路径
cd . # 切换到当前目录
cd .. # 切换到上级目录
pwd # 列出当前所在路径
cd ../.. # 切换到上级的上级目录
pwd # 列出当前所在路径
cd ../.. # 根/的上级的上级还是根/
pwd # 列出当前所在路径
cd # 快速切换到当前登录用户的家目录
pwd # 列出当前所在路径
cd - # 快速切换到上一次所在路径
pwd # 列出当前所在路径
cd ~ # 快速切换到当前登录用户的家目录
pwd # 列出当前所在路径
命令示例:
pwd
cd /etc/
pwd
cd sysconfig/network-scripts/
pwd
cd /etc/sysconfig/network-scripts
cd /etc/sysconfig/network-scripts
pwd
cd .
cd ..
pwd
cd ../..
pwd
cd ../..
pwd
cd
pwd
cd -
pwd
cd ~
pwd
输出结果:
[root@MineGi ~]# pwd
/root
[root@MineGi ~]# cd /etc/
[root@MineGi /etc]# pwd
/etc
[root@MineGi /etc]# cd sysconfig/network-scripts/
[root@MineGi /etc/sysconfig/network-scripts]# pwd
/etc/sysconfig/network-scripts
[root@MineGi /etc/sysconfig/network-scripts]# cd sysconfig/network-scripts/
-bash: cd: sysconfig/network-scripts/: 没有那个文件或目录
[root@MineGi /etc/sysconfig/network-scripts]# pwd
/etc/sysconfig/network-scripts
[root@MineGi /etc/sysconfig/network-scripts]# cd /etc/sysconfig/network-scripts
[root@MineGi /etc/sysconfig/network-scripts]# cd /etc/sysconfig/network-scripts
[root@MineGi /etc/sysconfig/network-scripts]# pwd
/etc/sysconfig/network-scripts
[root@MineGi /etc/sysconfig/network-scripts]# cd .
[root@MineGi /etc/sysconfig/network-scripts]# cd ..
[root@MineGi /etc/sysconfig]# pwd
/etc/sysconfig
[root@MineGi /etc/sysconfig]# cd ../..
[root@MineGi /]# pwd
/
[root@MineGi /]# cd ../..
[root@MineGi /]# pwd
/
[root@MineGi /]# cd
[root@MineGi ~]# pwd
/root
[root@MineGi ~]# cd -
/
[root@MineGi /]# pwd
/
[root@MineGi /]# cd ~
[root@MineGi ~]# pwd
/root
[root@MineGi ~]#
3.6 ls命令
ls - List显示指定目录(文件夹)下文件的详细信息,默认的操作目录为当前目录
用法:ls [选项]... [文件或目录]...
选项 | 说明 |
-l | 以长格式(Long)显示文件和目录的列表 |
-d | 显示目录(Directory)本身的属性,而不是显示目录中的内容 |
-a | 显示所有(All)子目录和文件的信息 |
-A | 与-a选项的作用基本类似,但有两个特殊隐藏目录不会显示,“.”和“..” |
-R | 以递归(Recursive)的方式显示指定目录及其子目录中的所有内容 |
-r | 倒序显示 |
-t | 时间显示 |
-s | 按文件排序显示 |
-i | 显示文件的inode(相当于身份证号码)号 |
-h | 以更人性化(Human)的方式显示出目录或文件的大小,此选项需要结合-l选项一起使用 |
-color=auto | 显示颜色,其中蓝色表示文件夹,黑色表示普通文件,红色表示压缩文件 |
- 红色:压缩文件
- 白色:普通文件
- 蓝色:目录文件
- 绿色:可执行文件
- 青色:链接文件(快捷方式)
- 黄色:设备文件
- 紫色:套接字文件
ls # 列出当前目录下内容
ls /etc/passwd # 列出 /etc/passwd 文件,若有,显示文件路径名称
ls /etc/passwdd # 列出 /etc/passwdd 文件,若无,显示没有那个文件或目录
ls -l /etc/passwd # 以长格式列出 /etc/passwd 文件
ll /etc/passwd # 简写,以长格式列出 /etc/passwd 文件
alias ll # 可以简写 ll 的原因
alias l. # 查看 l. 的简写
l. # 使用 l.
ls -a # 显示所有子目录和文件的信息,包含以 . 开头的隐藏文件
ls -A # 显示所有子目录和文件的信息,包含以 . 开头的隐藏文件,但不包含. 和 ..
ls /etc/dhcp/ # 默认会列出 /etc/dhcp/ 目录下的内容
ls -l /etc/dhcp/ # 以长格式列出 /etc/dhcp/ 目录下的内容
ls -ld /etc/dhcp/ # 以长格式列出 /etc/dhcp/ 目录本身的信息
ls -lt /etc/dhcp/ # 以时间排序列出 /etc/dhcp/ 目录下的内容
ls -lrt /etc/dhcp/ # 以时间逆序列出 /etc/dhcp/ 目录下的内容
ls -lrS /etc/dhcp/ # -S选项用于按照文件大小排序(从大到小)来显示文件和目录列表,加r 表示逆序
ls -R /etc/dhcp/ # 以递归(Recursive)的方式显示指定目录及其子目录中的所有内容
ls -lF /etc/dhcp/ # 用于在文件和目录名后添加一个字符来区分文件类型。这有助于用户快速识别文件的类型
ls -lF /usr/bin/ls # 用于在文件和目录名后添加一个字符来区分文件类型。这有助于用户快速识别文件的类型
ls -li /etc/dhcp/ # 显示文件或目录的 inode 编号
命令示例:
ls
ls /etc/passwd
ls /etc/passwdd
ls -l /etc/passwd
ll /etc/passwd
alias ll
alias l.
l.
ls -a
ls -A
ls /etc/dhcp/
ls -l /etc/dhcp/
ls -ld /etc/dhcp/
ls -lt /etc/dhcp/
ls -lrt /etc/dhcp/
ls -lrS /etc/dhcp/
ls -R /etc/dhcp/
ls -lF /etc/dhcp/
ls -lF /usr/bin/ls
ls -li /etc/dhcp/
输出结果:
[root@MineGi ~]# ls
1.txt
[root@MineGi ~]# ls /etc/passwd
/etc/passwd
[root@MineGi ~]# ls /etc/passwdd
ls: 无法访问/etc/passwdd: 没有那个文件或目录
[root@MineGi ~]# ls -l /etc/passwd
-rw-r--r--. 1 root root 879 11月 22 10:07 /etc/passwd
[root@MineGi ~]# ll /etc/passwd
-rw-r--r--. 1 root root 879 11月 22 10:07 /etc/passwd
[root@MineGi ~]# alias ll
alias ll='ls -l --color=auto'
[root@MineGi ~]# alias l.
alias l.='ls -d .* --color=auto'
[root@MineGi ~]# l.
. .. .bash_history .bash_logout .bash_profile .bashrc .cshrc .tcshrc .viminfo
[root@MineGi ~]# ls -a
. .. 1.txt .bash_history .bash_logout .bash_profile .bashrc .cshrc .tcshrc .viminfo
[root@MineGi ~]# ls -A
1.txt .bash_history .bash_logout .bash_profile .bashrc .cshrc .tcshrc .viminfo
[root@MineGi ~]# ls /etc/dhcp/
dhclient.d dhclient-exit-hooks.d
[root@MineGi ~]# ls -l /etc/dhcp/
总用量 0
drwxr-xr-x. 2 root root 23 11月 22 09:54 dhclient.d
drwxr-xr-x. 2 root root 28 11月 22 09:53 dhclient-exit-hooks.d
[root@MineGi ~]# ls -ld /etc/dhcp/
drwxr-x---. 4 root root 53 11月 22 09:53 /etc/dhcp/
[root@MineGi ~]# ls -lt /etc/dhcp/
总用量 0
drwxr-xr-x. 2 root root 23 11月 22 09:54 dhclient.d
drwxr-xr-x. 2 root root 28 11月 22 09:53 dhclient-exit-hooks.d
[root@MineGi ~]# ls -lrt /etc/dhcp/
总用量 0
drwxr-xr-x. 2 root root 28 11月 22 09:53 dhclient-exit-hooks.d
drwxr-xr-x. 2 root root 23 11月 22 09:54 dhclient.d
[root@MineGi ~]# ls -lrS /etc/dhcp/
总用量 0
drwxr-xr-x. 2 root root 23 11月 22 09:54 dhclient.d
drwxr-xr-x. 2 root root 28 11月 22 09:53 dhclient-exit-hooks.d
[root@MineGi ~]# ls -R /etc/dhcp/
/etc/dhcp/:
dhclient.d dhclient-exit-hooks.d
/etc/dhcp/dhclient.d:
chrony.sh
/etc/dhcp/dhclient-exit-hooks.d:
azure-cloud.sh
[root@MineGi ~]# ls -lF /etc/dhcp/
总用量 0
drwxr-xr-x. 2 root root 23 11月 22 09:54 dhclient.d/
drwxr-xr-x. 2 root root 28 11月 22 09:53 dhclient-exit-hooks.d/
[root@MineGi ~]# ls -lF /usr/bin/ls
-rwxr-xr-x. 1 root root 117608 8月 20 2019 /usr/bin/ls*
[root@MineGi ~]# ls -li /etc/dhcp/
总用量 0
134481033 drwxr-xr-x. 2 root root 23 11月 22 09:54 dhclient.d
358887 drwxr-xr-x. 2 root root 28 11月 22 09:53 dhclient-exit-hooks.d
[root@MineGi ~]#
通配符
- 在生产环境一千万个文件的情况下,使用 ls 会卡死,需结合通配符使用
- 匹配文件的名字,通常用于 ls 命令中的选项
? # 匹配一个字符
* # 匹配所有非隐藏的字符无论多长多短,但不匹配 "." 开头的文件,即隐藏文件
{1..10} # 1到10
{a..z} # a b c ...z
{A..Z} # A B C .....Z
[123] # 此处括号里值代表一个字符,取其中之一有就显示没有也无所谓
[a-z] # 单个小写字母,范围需要注意
[0-9] # 单个数字,匹配数字范围(0123456789)
\ # 转义符,表示原来的意思
[^MineGi] # 匹配列表中的所有字符以外的字符
[[:lower:]] # 小写字母表示 a-z
[[:upper:]] # 大写字母表示 A-Z
[[:digit:]] # 任意数字,相当于[0-9]
[[:alpha:]] # 任意字母
命令示例:
touch {1..10}.txt
ls
ls *.txt
ls ??.txt
echo {a..z}
echo {A..Z}
ls [123].txt
ls [0-9].txt
ls [^123].txt
ls [[:digit:]].txt
输出结果:
[root@MineGi ~]# touch {1..10}.txt
[root@MineGi ~]# ls
10.txt 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt
[root@MineGi ~]# ls *.txt
10.txt 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt
[root@MineGi ~]# ls ??.txt
10.txt
[root@MineGi ~]# echo {a..z}
a b c d e f g h i j k l m n o p q r s t u v w x y z
[root@MineGi ~]# echo {A..Z}
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
[root@MineGi ~]# ls [123].txt
1.txt 2.txt 3.txt
[root@MineGi ~]# ls [0-9].txt
1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt
[root@MineGi ~]# ls [^123].txt
4.txt 5.txt 6.txt 7.txt 8.txt 9.txt
[root@MineGi ~]# ls [[:digit:]].txt
1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt
[root@MineGi ~]#
3.7 du命令
查看当前所在文件夹占用了磁盘的多少单元块。默认1单元块为4K, 不满4K按4k算。
用法:du [选项]... [文件或目录]...
disk usage 用于统计指定目录(或文件)所占用磁盘空间的大小,du命令常用的几个选项如下:
- -a:统计磁盘空间占用时包括所有的文件,而不仅仅只统计目录
- -h:以更人性化的方式(默认以KB计数,但不显示单位)显示出统计结果
- -s:只统计所占用空间总的(Summary)大小,而不是统计每个子目录、文件的大小
- --max-depth=n (-d 1):最大显示到第n层
- exclude:不计入统计
du /etc/dhcp/ # 默认方式查看
du -h /etc/dhcp/ # 使用人类可读方式查看
du -s /etc/dhcp/ # 只统计所占用空间总大小
du -sh /etc/dhcp/ # 结合使用
du -a /etc/dhcp/ # 查看所有文件大小
du -ah /etc/dhcp/ # 结合使用
命令示例:
du /etc/dhcp/
du -h /etc/dhcp/
du -s /etc/dhcp/
du -sh /etc/dhcp/
du -a /etc/dhcp/
du -ah /etc/dhcp/
输出结果:
[root@MineGi ~]# du /etc/dhcp/
4 /etc/dhcp/dhclient-exit-hooks.d
4 /etc/dhcp/dhclient.d
8 /etc/dhcp/
[root@MineGi ~]# du -h /etc/dhcp/
4.0K /etc/dhcp/dhclient-exit-hooks.d
4.0K /etc/dhcp/dhclient.d
8.0K /etc/dhcp/
[root@MineGi ~]# du -s /etc/dhcp/
8 /etc/dhcp/
[root@MineGi ~]# du -sh /etc/dhcp/
8.0K /etc/dhcp/
[root@MineGi ~]# du -a /etc/dhcp/
4 /etc/dhcp/dhclient-exit-hooks.d/azure-cloud.sh
4 /etc/dhcp/dhclient-exit-hooks.d
4 /etc/dhcp/dhclient.d/chrony.sh
4 /etc/dhcp/dhclient.d
8 /etc/dhcp/
[root@MineGi ~]# du -ah /etc/dhcp/
4.0K /etc/dhcp/dhclient-exit-hooks.d/azure-cloud.sh
4.0K /etc/dhcp/dhclient-exit-hooks.d
4.0K /etc/dhcp/dhclient.d/chrony.sh
4.0K /etc/dhcp/dhclient.d
8.0K /etc/dhcp/
[root@MineGi ~]#
拓展问题:
ls -l命令与du命令都有查看文件大小的功能,为什么显示同一个文件的大小却不一样
- ls -l:显示文件的真实大小
- du:显示文件所占磁盘多少单元块,默认1单元块为4K, 不满4K按4k算,按每4k往上递增
3.8 touch命令
用法:touch [选项]... 文件...
如果文件已存在,更新文件的访问时间和修改时间;若文件未存在,则创建新的空文件
- -a 只更改访问时间
- -c, --no-create 不创建任何文件
- -d, --date=字符串 使用指定字符串表示时间而非当前时间
- -m 只更改修改时间
每个文件有三个时间戳:
- access time 访问时间,atime,读取文件内容
- modify time 修改时间,mtime,改变文件内容(数据)
- change time 改变时间,ctime,元数据发生改变(元数据、文件大小、权限 )
ll /etc/passwd # 查看已存在的文件
touch /etc/passwd # touch已存在的文件
ll /etc/passwd # 查看已存在的文件
ll file1 a.txt # 查看不存在的文件
touch file1 a.txt # touch不存在的文件
ll file1 a.txt # 查看新创建的文件
命令示例:
ll /etc/passwd
touch /etc/passwd
ll /etc/passwd
ll file1 a.txt
touch file1 a.txt
ll file1 a.txt
输出结果:
[root@MineGi ~]# ll /etc/passwd
-rw-r--r--. 1 root root 879 11月 22 10:07 /etc/passwd
[root@MineGi ~]# touch /etc/passwd
[root@MineGi ~]# ll /etc/passwd
-rw-r--r--. 1 root root 879 11月 29 18:19 /etc/passwd
[root@MineGi ~]# ll file1 a.txt
ls: 无法访问file1: 没有那个文件或目录
ls: 无法访问a.txt: 没有那个文件或目录
[root@MineGi ~]# touch file1 a.txt
[root@MineGi ~]# ll file1 a.txt
-rw-r--r-- 1 root root 0 11月 29 18:19 a.txt
-rw-r--r-- 1 root root 0 11月 29 18:19 file1
[root@MineGi ~]#
3.9 mkdir命令
make directory 创建新的目录文件
用法:mkdir [选项]... 目录...
- -p 嵌套创建多层目录
- -v 显示详细信息
ls dir1 # 查看有无 dir1
mkdir dir1 # 创建 dir1 目录
mkdir dir1 # 再次创建 dir1 目录,提示已存在
mkdir -p dir1 # 再次创建 dir1 目录,使用 -p 选项,不会提示已存在
touch dir1 # 并不会创建同名文件,而只是刷新 dir1 目录的时间戳
touch file1 # 创建 file1 文件
mkdir file1 # 并不会创建同名目录,提示已存在,Linux 一切皆文件,同级目录下不允许创建同名文件
mkdir -v dir1/dir2 # 显示创建 dir2 目录的过程
mkdir aaa/bbb/ccc # 无法直接多层目录
mkdir -p aaa/bbb/ccc # 使用 -p 选项即可
mkdir -pv 111/222/333 # 可以组合使用
mkdir -p dir/{a..f}/dir{1..10} # 也可以使用 {} 创建
ls dir # 查看一下
ls dir/a/ # 查看一下
命令示例:
mkdir dir1
touch dir1
touch file1
mkdir file1
mkdir -v dir1/dir2
mkdir aaa/bbb/ccc
mkdir -p aaa/bbb/ccc
mkdir -pv 111/222/333
mkdir -p dir/{a..f}/dir{1..10}
ls dir
ls dir/a/
输出结果:
[root@MineGi ~]# mkdir dir1
[root@MineGi ~]# touch dir1
[root@MineGi ~]# touch file1
[root@MineGi ~]# mkdir file1
mkdir: 无法创建目录"file1": 文件已存在
[root@MineGi ~]# mkdir -v dir1/dir2
mkdir: 已创建目录 "dir1/dir2"
[root@MineGi ~]# mkdir aaa/bbb/ccc
mkdir: 无法创建目录"aaa/bbb/ccc": 没有那个文件或目录
[root@MineGi ~]# mkdir -p aaa/bbb/ccc
[root@MineGi ~]# mkdir -pv 111/222/333
mkdir: 已创建目录 "111"
mkdir: 已创建目录 "111/222"
mkdir: 已创建目录 "111/222/333"
[root@MineGi ~]# mkdir -p dir/{a..f}/dir{1..10}
[root@MineGi ~]# ls dir
a b c d e f
[root@MineGi ~]# ls dir/a/
dir1 dir10 dir2 dir3 dir4 dir5 dir6 dir7 dir8 dir9
[root@MineGi ~]#
3.10 cp命令
copy 将需要复制的文件或目录(源)重建一份,并保存为新的文件或目录
用法:cp [选项]... 源文件... 目录
- -f:覆盖目标同名文件或目录时不进行提醒,直接强制复制
- -i:覆盖目标同名文件或目录时提醒用户确认
- -r:复制目录时必须使用此选项,表示递归复制所有文件及子目录
- -p:复制时保持源文件的权限、属主及时间标记等属性不变
- -a:完整保留所有的信息
- -v:显示过程
- -u:相当于增备,差异备份(只对文件夹起作用)
注意:系统中默认设置了cp别名
touch file1 # 创建 file1 文件
ls file1 # 列出一下
cp file1 file2 # 复制 file1 文件为 file2,file2 之前不存在
cp file1 file2 # 复制 file1 文件为 file2,file2 已存在,提示是否覆盖,输入 y 表示同意,n 表示取消,演示输入 y
ls file2 # 列出一下
alias cp # 之所以会提示是否会覆盖,系统给做的别名
\cp file1 file2 # 取消别名,不会提示是否覆盖
mkdir -p dir1 # 创建 dir1 目录
cp file1 dir1/newname1 # 将 file1 文件复制到 dir1 目录里,并重命名为 newname1
ls dir1/ # 列出一下
cp file1 file2 dir1/ # 一次复制多个文件
ls dir1/ # 列出一下
cp dir1 dir2 # 复制 dir1 目录为 dir2,提示略过目录,表示没成功
cp -r dir1 dir2 # 复制目录时,需要使用 -r 选项
ls dir2 # 列出一下
cp file1 file3 # 复制 file1 文件为 file3
cp -a file1 file4 # 复制 file1 文件为 file4,并使用 -a 选项,表示完整保留所有的信息,比如时间戳等
ls file* # 列出一下
ll file* # 长格式列出一下
命令示例:
touch file1
ls file1
cp file1 file2
cp file1 file2
ls file2
alias cp
\cp file1 file2
mkdir -p dir1
cp file1 dir1/newname1
ls dir1/
cp file1 file2 dir1/
ls dir1/
cp dir1 dir2
cp -r dir1 dir2
ls dir2
cp file1 file3
cp -a file1 file4
ls file*
ll file*
输出结果:
[root@MineGi ~]# ls
10.txt 1.txt 3.txt 5.txt 7.txt 9.txt a.txt dir1
111 2.txt 4.txt 6.txt 8.txt aaa dir file1
[root@MineGi ~]#
[root@MineGi ~]# touch file1
[root@MineGi ~]# ls file1
file1
[root@MineGi ~]# cp file1 file2
[root@MineGi ~]# cp file1 file2
cp:是否覆盖"file2"? y
[root@MineGi ~]# ls file2
file2
[root@MineGi ~]# alias cp
alias cp='cp -i'
[root@MineGi ~]# \cp file1 file2
[root@MineGi ~]# mkdir -p dir1
[root@MineGi ~]# cp file1 dir1/newname1
[root@MineGi ~]# ls dir1/
dir2 newname1
[root@MineGi ~]# cp file1 file2 dir1/
[root@MineGi ~]# ls dir1/
dir2 file1 file2 newname1
[root@MineGi ~]# cp dir1 dir2
cp: 略过目录"dir1"
[root@MineGi ~]# cp -r dir1 dir2
[root@MineGi ~]# ls dir2
dir2 file1 file2 newname1
[root@MineGi ~]# cp file1 file3
[root@MineGi ~]# cp -a file1 file4
[root@MineGi ~]# ls file*
file1 file2 file3 file4
[root@MineGi ~]# ll file*
-rw-r--r-- 1 root root 0 11月 29 19:39 file1
-rw-r--r-- 1 root root 0 11月 29 19:39 file2
-rw-r--r-- 1 root root 0 11月 29 19:40 file3
-rw-r--r-- 1 root root 0 11月 29 19:39 file4
[root@MineGi ~]#
3.11 mv命令
move 将指定的文件或目录转移位置如果目标位置与源位置相同,则相当于执行重命名操作
mv [选项]... 源文件... 目录
- -f, --force:覆盖前不询问,强制覆盖,如果目标文件已经存在,则不询问,直接强制覆盖
- -i, --interactive:覆盖前询问,交互移动,如果目标文件已经存在,则询问用户是否覆盖(默认选项)
- -n, --no-clobber:不覆盖已存在文件,如果目标文件已经存在,则不会覆盖移动,而且不询问用户
- -v:显示文件或目录的移动过程
- -t:用于指定目标目录。这与常规使用mv命令时目标目录作为最后一个参数的方式有所不同,-t选项提供了另一种指定目标位置的方法。
如果您指定了-i、-f、-n 中的多个,仅最后一个生效。
注意:系统中默认设置了mv别名
ls # 列出一下
mv [1-5].txt dir/ # 移动 1.txt 到 5.txt 的文件到 dir 目录下
ls # 列出一下
mv *.txt dir/ # 移动所有以 .txt 结尾的文件到 dir 目录下
ls # 列出一下
ls dir1/ # 列出一下
mv file1 file5 # 将 file1 文件改名为 file5,file5之前不存在
ls file* # 列出一下
mv file2 file5 # 将 file2 文件改名为 file5,file5之前已存在,则为是否覆盖,默认会提示
mv -n file2 file5 # -n 选项:不覆盖已存在文件,如果目标文件已经存在,则不会覆盖移动,而且不询问用户
ls file* # 列出一下
mv -f file2 file5 # -f 选项:覆盖前不询问,强制覆盖,如果目标文件已经存在,则不询问,直接强制覆盖
ls file* # 列出一下
alias mv # 系统做的别名
ls file1 # 列出一下
mv dir1/file1 ./ # 常规移动:mv 源文件 目标文件或目录
ls file1 # 列出一下
ls file2 # 列出一下
mv -t ./ dir1/file2 # 使用-t选项:mv -t 目标文件或目录 源文件
ls file2 # 列出一下
命令示例:
ls
mv [1-5].txt dir/
ls
mv *.txt dir/
ls
ls dir/
mv file1 file5
ls file*
mv file2 file5
mv -n file2 file5
ls file*
mv -f file2 file5
ls file*
alias mv
ls file1
mv dir1/file1 ./
ls file1
ls file2
mv -t ./ dir1/file2
ls file2
输出结果:
[root@MineGi ~]# ls
10.txt 1.txt 3.txt 5.txt 7.txt 9.txt a.txt dir1 file1 file3
111 2.txt 4.txt 6.txt 8.txt aaa dir dir2 file2 file4
[root@MineGi ~]# mv [1-5].txt dir/
[root@MineGi ~]# ls
10.txt 111 6.txt 7.txt 8.txt 9.txt aaa a.txt dir dir1 dir2 file1 file2 file3 file4
[root@MineGi ~]# mv *.txt dir/
[root@MineGi ~]# ls
111 aaa dir dir1 dir2 file1 file2 file3 file4
[root@MineGi ~]# ls dir/
10.txt 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt a a.txt b c d e f
[root@MineGi ~]# mv file1 file5
[root@MineGi ~]# ls file*
file2 file3 file4 file5
[root@MineGi ~]# mv file2 file5
mv:是否覆盖"file5"? n
[root@MineGi ~]# mv -n file2 file5
[root@MineGi ~]# ls file*
file2 file3 file4 file5
[root@MineGi ~]# mv -f file2 file5
[root@MineGi ~]# ls file*
file3 file4 file5
[root@MineGi ~]# alias mv
alias mv='mv -i'
[root@MineGi ~]# ls file1
ls: 无法访问file1: 没有那个文件或目录
[root@MineGi ~]# mv dir1/file1 ./
[root@MineGi ~]# ls file1
file1
[root@MineGi ~]# ls file2
ls: 无法访问file2: 没有那个文件或目录
[root@MineGi ~]# mv -t ./ dir1/file2
[root@MineGi ~]# ls file2
file2
[root@MineGi ~]#
3.12 rm命令
remove 删除指定的文件或目录
用法:rm [选项]... 文件...
- -f 删除文件或目录时不进行提醒,直接强制删除(非常危险的选项)
- -i 删除文件或目录时提醒用户确认
- -r 删除目录时必须使用此选项,表示递归删除所有文件及子目录
注意:系统中默认设置了rm别名
注意:工作中千万不要用 rm -rf
ls # 列出一下
ls dir # 列出一下
rm dir/1.txt dir/10.txt # 删除dir目录下的文件,提示会是否删除,输入 y 表示同意,n 表示取消,演示为 y
ls dir # 列出一下
alias rm # 系统做的别名
\rm dir/{2..8}.txt # 取消别名
ls dir # 列出一下
rm -f dir/{9.txt,a.txt} # 使用 -f 选项强制删除
ls dir # 列出一下
rm aaa # 删除 aaa 目录
rm -f aaa # 强制删除?
ls -R aaa # 列出一下
rm -r aaa # 删除目录时,使用 -r 选项,演示输入 y
ls -R 111 # 列出一下
rm -rf 111 # 使用 -rf 组合时,不会有任何提示,超级危险,使用前一定要仔细
ls # 列出一下
rm -rf dir1 # 删除目录本身时,加不加 / ,都一样
rm -rf dir2/ # 删除目录本身时,加不加 / ,都一样
ls dir1/ # 列出一下
ls dir2/ # 列出一下
ls # 列出一下
cp -r dir dir2 # 复制一下
ls # 列出一下
ln -s ~/dir dir_link # 给目录做个软链接
ln -s ~/dir2 dir2_link # 给目录做个软链接
ll *link # 列出一下
ls dir/ # 列出一下
ls dir2/ # 列出一下
rm -rf dir_link # 删除目录软链接时,不加 / ,是删除软链接本身
rm -rf dir2_link/ # 删除目录软链接时,加 / ,是删除软链接指向的目录里的内容
ls dir/ # 列出一下
ls dir2/ # 列出一下
ll *link # 列出一下
命令示例:
ls
ls dir
rm dir/1.txt dir/10.txt
ls dir
alias rm
\rm dir/{2..8}.txt
ls dir
rm -f dir/{9.txt,a.txt}
ls dir
rm aaa
rm -f aaa
ls -R aaa
rm -r aaa
ls -R 111
rm -rf 111
ls
rm -rf dir1
rm -rf dir2/
ls dir1/
ls dir2/
ls
cp -r dir dir2
ls
ln -s ~/dir dir_link
ln -s ~/dir2 dir2_link
ll *link
ls dir/
ls dir2/
rm -rf dir_link
rm -rf dir2_link/
ls dir/
ls dir2/
ll *link
输出结果:
[root@MineGi ~]# ls
111 aaa dir dir1 dir2 file1 file2 file3 file4 file5
[root@MineGi ~]# ls dir
10.txt 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt a a.txt b c d e f
[root@MineGi ~]# rm dir/1.txt dir/10.txt
rm:是否删除普通空文件 "dir/1.txt"?y
rm:是否删除普通空文件 "dir/10.txt"?y
[root@MineGi ~]# ls dir
2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt a a.txt b c d e f
[root@MineGi ~]# alias rm
alias rm='rm -i'
[root@MineGi ~]# \rm dir/{2..8}.txt
[root@MineGi ~]# ls dir
9.txt a a.txt b c d e f
[root@MineGi ~]# rm -f dir/{9.txt,a.txt}
[root@MineGi ~]# ls dir
a b c d e f
[root@MineGi ~]# rm aaa
rm: 无法删除"aaa": 是一个目录
[root@MineGi ~]# rm -f aaa
rm: 无法删除"aaa": 是一个目录
[root@MineGi ~]# ls -R aaa
aaa:
bbb
aaa/bbb:
ccc
aaa/bbb/ccc:
[root@MineGi ~]# rm -r aaa
rm:是否进入目录"aaa"? y
rm:是否进入目录"aaa/bbb"? y
rm:是否删除目录 "aaa/bbb/ccc"?y
rm:是否删除目录 "aaa/bbb"?y
rm:是否删除目录 "aaa"?y
[root@MineGi ~]# ls -R 111
111:
222
111/222:
333
111/222/333:
[root@MineGi ~]# rm -rf 111
[root@MineGi ~]# ls
dir dir1 dir2 file1 file2 file3 file4 file5
[root@MineGi ~]# rm -rf dir1
[root@MineGi ~]# rm -rf dir2/
[root@MineGi ~]# ls
dir file1 file2 file3 file4 file5
[root@MineGi ~]# cp -r dir dir2
[root@MineGi ~]# ls
dir dir2 file1 file2 file3 file4 file5
[root@MineGi ~]# ln -s ~/dir dir_link
[root@MineGi ~]# ln -s ~/dir2 dir2_link
[root@MineGi ~]# ll *link
lrwxrwxrwx 1 root root 10 11月 30 09:46 dir2_link -> /root/dir2
lrwxrwxrwx 1 root root 9 11月 30 09:46 dir_link -> /root/dir
[root@MineGi ~]# ls dir/
a b c d e f
[root@MineGi ~]# ls dir2/
a b c d e f
[root@MineGi ~]# rm -rf dir_link
[root@MineGi ~]# rm -rf dir2_link/
[root@MineGi ~]# ls dir/
a b c d e f
[root@MineGi ~]# ls dir2/
[root@MineGi ~]# ll *link
lrwxrwxrwx 1 root root 10 11月 30 09:46 dir2_link -> /root/dir2
[root@MineGi ~]#
# rm与mv相同的危险操作是什么?
rm -rf /*
mv -f ./file1 ./dir1/file1 #说明:./file1和./dir1/file1两个文件同名且同时存在数据
3.13 which 和 whereis 命令
查看命令程序文件路径
搜索范围由环境变量 PATH 的值决定(echo $PATH)
- -a 查询是遍历PATH值中的所有目录
命令示例:
which whereis
which which
whereis which
whereis whereis
which ls
whereis ls
echo $PATH
输出结果:
[root@MineGi ~]# which whereis
/usr/bin/whereis
[root@MineGi ~]# which which
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
/usr/bin/alias
/usr/bin/which
[root@MineGi ~]# whereis which
which: /usr/bin/which /usr/share/man/man1/which.1.gz
[root@MineGi ~]# whereis whereis
whereis: /usr/bin/whereis /usr/share/man/man1/whereis.1.gz
[root@MineGi ~]# which ls
alias ls='ls --color=auto'
/usr/bin/ls
[root@MineGi ~]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
[root@MineGi ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@MineGi ~]#
3.14 locate命令
查找文件或目录
用法:locate 选项 查找条件
- -i:不区分大小写的搜索
- -n N:只列举前N个匹配项目
locate passwd |wc -l # 查看包含 passwd 的文件或目录,并统计个数
yum install -y mlocate # 使用 locate 命令需要安装 mlocate 包
locate passwd |wc -l # 再次查看
updatedb # 需要更新数据库
locate passwd |wc -l # 再次查看
ls # 列出一下
locate file1 # 查看包含 file1 的文件或目录
touch newfile # 创建一个 newfile 文件
locate newfile # 查看包含 newfile 的文件或目录
updatedb # 需要更新数据库
locate newfile # 查看包含 newfile 的文件或目录
rm -rf newfile # 删除 newfile 文件
locate newfile # 查看包含 newfile 的文件或目录
updatedb # 需要更新数据库
locate newfile # 查看包含 newfile 的文件或目录
locate -n 3 passwd # 查看包含 passwd 的文件或目录,只看前三行
命令示例:
locate passwd |wc -l
yum install -y mlocate
locate passwd |wc -l
updatedb
locate passwd |wc -l
ls
locate file1
touch newfile
locate newfile
updatedb
locate newfile
rm -rf newfile
locate newfile
updatedb
locate newfile
locate -n 3 passwd
输出结果:
[root@MineGi ~]# locate passwd |wc -l
-bash: /usr/bin/locate: 没有那个文件或目录
0
[root@MineGi ~]# yum install -y -q mlocate
软件包 mlocate-0.26-8.el7.x86_64 已安装并且是最新版本
[root@MineGi ~]# locate passwd |wc -l
locate: 无法执行 stat () `/var/lib/mlocate/mlocate.db': 没有那个文件或目录
0
[root@MineGi ~]# updatedb
[root@MineGi ~]# locate passwd |wc -l
123
[root@MineGi ~]# ls
dir dir2 dir2_link file1 file2 file3 file4 file5
[root@MineGi ~]# locate file1
/root/file1
/test/cat/file1.txt
[root@MineGi ~]# touch newfile
[root@MineGi ~]# locate newfile
[root@MineGi ~]# updatedb
[root@MineGi ~]# locate newfile
/root/newfile
[root@MineGi ~]# rm -rf newfile
[root@MineGi ~]# locate newfile
/root/newfile
[root@MineGi ~]# updatedb
[root@MineGi ~]# locate newfile
[root@MineGi ~]# locate -n 3 passwd
/etc/passwd
/etc/passwd-
/etc/pam.d/passwd
[root@MineGi ~]#
3.15 find命令
采用递归方式,根据目标的名称、类型、大小等不同属性进行精细查找
用法:find [查找路径] [OPTION] [查找条件] [处理动作]
- 查找路径:指定具体目标路径;不指定为默认为当前目录
- 查找条件:可以对文件名、大小、类型、权限等标准进行查找;默认为找出指定路径下的所有文件
- 处理动作:对符合条件的文件做操作,默认输出至屏幕(print)
查找类型:
- -name:根据目标文件的名称进行查找,允许使用“*”及“?”通配符,-iname不区分大小写
- -size:根据目标文件的大小进行查找、 一般使用“+”、“-”号设置超过或小于指定的大小作为查找条件、常用的容量单位包括 kB(注意 k 是小写)、MB、GB
- -user:根据文件是否属于目标用户进行查找
- -type:根据文件的类型进行查找文件类型包括普通文件(f)、目录(d)、块设备文件(b)、字符设备文件(c)等
- 文件类型包括普通文件(f)、目录(d)、块设备文件(b)、字符设备文件(c)链接文件(l)、管道文件(p)、套接字文件(s:socket)等、块设备是指成块读取数据的设备(如硬盘、内存等),字符设备是指按单个字符读取数据的设备(如键盘、鼠标等)
- -inum:根据文件inode号查找
- -perm:按文件权限查找
- -maxdepth level:最大搜索目录深度,将你的文件已分级的形式查找
- -mindepth level:最小搜索目录深度,将你的文件已分级的形式查找
3.15.1 根据文件名和inode查找
ls # 列出一下
find ./ -name file1 # 在当前目录下,查找名为 file1 的目录或文件
find /etc -name passwd* # 在 /etc 目录下,查找包含 passwd 的目录或文件
find ./ -name file* # 在当前目录下,查找包含 file 的目录或文件,好像报错了
find ./ -name "file*" # 在当前目录下,查找包含 file 的目录或文件,用引号引起来
ls -i file2 # 列出一下 file2 的 inode 号
find . -inum 269873059 # 在当前目录下,查找 inode 号为 269873059 的文件
命令示例:
ls
find ./ -name file1
find /etc -name passwd*
find ./ -name file*
find ./ -name "file*"
ls -i file2
find . -inum 269873059
输出结果:
[root@MineGi ~]# ls
dir dir2 dir2_link file1 file2 file3 file4 file5
[root@MineGi ~]# find ./ -name file1
./file1
[root@MineGi ~]# find /etc -name passwd*
/etc/passwd-
/etc/passwd
/etc/pam.d/passwd
[root@MineGi ~]# find ./ -name file*
find: 路径必须在表达式之前: file2
用法: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
[root@MineGi ~]# find ./ -name "file*"
./file3
./file4
./file5
./file1
./file2
[root@MineGi ~]# ls -i file2
269873059 file2
[root@MineGi ~]# find . -inum 269873059
./file2
[root@MineGi ~]#
3.15.2 根据属主属组查找
-user USERNAME #查找属主为指定用户(UID)的文件
-group GRPNAME #查找属组为指定组(GID)的文件
-uid UserID #查找属主为指定的UID号的文件
-gid GroupID #查找属组为指定的GID号的文件
-nouser #查找没有属主的文件(无主)
-nogroup #查找没有属组的文件
3.15.3 根据文件类型
find /etc/ssh/ -type f # 在 /etc/ssh/ 目录下查找类型为文件的内容
find /etc/ssh/ -type d # 在 /etc/ssh/ 目录下查找类型为目录的内容
find /etc/dhcp/ -type d # 在 /etc/dhcp/ 目录下查找类型为目录的内容
命令示例:
find /etc/ssh/ -type f
find /etc/ssh/ -type d
find /etc/dhcp/ -type d
输出结果:
[root@MineGi ~]# find /etc/ssh/ -type f
/etc/ssh/moduli
/etc/ssh/sshd_config
/etc/ssh/ssh_config
/etc/ssh/ssh_host_rsa_key
/etc/ssh/ssh_host_rsa_key.pub
/etc/ssh/ssh_host_ecdsa_key
/etc/ssh/ssh_host_ecdsa_key.pub
/etc/ssh/ssh_host_ed25519_key
/etc/ssh/ssh_host_ed25519_key.pub
[root@MineGi ~]# find /etc/ssh/ -type d
/etc/ssh/
[root@MineGi ~]# find /etc/dhcp/ -type d
/etc/dhcp/
/etc/dhcp/dhclient-exit-hooks.d
/etc/dhcp/dhclient.d
[root@MineGi ~]#
3.15.3 处理动作
处理动作 | 说明 |
| 默认的处理动作,显示至屏幕 |
-ls | 类似于对查找到的文件执行"ls -dils"命令格式输出 |
-fls file | 查找到的所有文件的长格式信息保存至指定文件中,相当于-ls > file |
-delete | 删除查找到的文件,慎用! |
-ok COMMAND {} \; | 对查找到的每个文件执行由COMMAND指定的命令,对于每个文件执行命令之前,都会交互式要求用户确认 |
-exec COMMAND {}\; | 对查找到的每个文件执行由COMMAND指定的命令 |
3.15.4 组合条件
组合条件 | 说明 |
-a | 与 |
-o | 或 |
-not | 非 |
3.16 alias命令
查看系统已设置的别名
用法:alias 自定义的命令=“命令条”
alias # 查看全部别名
alias cdnet="cd /etc/sysconfig/network-scripts" # 设置别名
alias cdnet # 查看指定别名
pwd # 查看当前路径
cdnet # 使用自定义别名
pwd # 查看当前路径
命令示例:
alias
alias cdnet="cd /etc/sysconfig/network-scripts"
alias cdnet
pwd
cdnet
pwd
输出结果:
[root@MineGi ~]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@MineGi ~]# alias cdnet="cd /etc/sysconfig/network-scripts"
[root@MineGi ~]# alias cdnet
alias cdnet='cd /etc/sysconfig/network-scripts'
[root@MineGi ~]# pwd
/root
[root@MineGi ~]# cdnet
[root@MineGi /etc/sysconfig/network-scripts]# pwd
/etc/sysconfig/network-scripts
[root@MineGi /etc/sysconfig/network-scripts]#
3.17 unalias命令
取消别名
alias cdnet # 查看指定别名
unalias cdnet # 取消别名
alias cdnet # 查看指定别名
alias unalias=pwd # 设置 unalias 等于 pwd
unalias # 变成 pwd 命令了
unalias unalias # 取消不了
\unalias unalias # 取消别名,正常使用
unalias # 恢复 unalias 本来命令
命令示例:
alias cdnet
unalias cdnet
alias cdnet
alias unalias=pwd
unalias
unalias unalias
\unalias unalias
unalias
输出结果:
[root@MineGi /etc/sysconfig/network-scripts]# alias cdnet
alias cdnet='cd /etc/sysconfig/network-scripts'
[root@MineGi /etc/sysconfig/network-scripts]# unalias cdnet
[root@MineGi /etc/sysconfig/network-scripts]# alias cdnet
-bash: alias: cdnet: 未找到
[root@MineGi /etc/sysconfig/network-scripts]# alias unalias=pwd
[root@MineGi /etc/sysconfig/network-scripts]# unalias
/etc/sysconfig/network-scripts
[root@MineGi /etc/sysconfig/network-scripts]# unalias unalias
/etc/sysconfig/network-scripts
[root@MineGi /etc/sysconfig/network-scripts]# \unalias unalias
[root@MineGi /etc/sysconfig/network-scripts]# unalias
unalias: 用法:unalias [-a] 名称 [名称 ...]
[root@MineGi /etc/sysconfig/network-scripts]#
2.18 date命令
查看和修改系统时间
命令示例:
cd
date
date +%F
date -s "12:00:00"
date
输出结果:
[root@MineGi /etc/sysconfig/network-scripts]# cd
[root@MineGi ~]# date
2024年 11月 30日 星期六 14:34:22 CST
[root@MineGi ~]# date +%F
2024-11-30
[root@MineGi ~]# date -s "12:00:00"
2024年 11月 30日 星期六 12:00:00 CST
[root@MineGi ~]# date
2024年 11月 30日 星期六 12:00:01 CST
[root@MineGi ~]#
2.19 cal命令
查看系统日历
命令示例:
cal
cal -y
cal 9 1752
cal 1 2024
cal 2 2323
输出结果:
[root@MineGi ~]# cal
十一月 2024
日 一 二 三 四 五 六
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
[root@MineGi ~]# cal -y
2024
一月 二月 三月
日 一 二 三 四 五 六 日 一 二 三 四 五 六 日 一 二 三 四 五 六
1 2 3 4 5 6 1 2 3 1 2
7 8 9 10 11 12 13 4 5 6 7 8 9 10 3 4 5 6 7 8 9
14 15 16 17 18 19 20 11 12 13 14 15 16 17 10 11 12 13 14 15 16
21 22 23 24 25 26 27 18 19 20 21 22 23 24 17 18 19 20 21 22 23
28 29 30 31 25 26 27 28 29 24 25 26 27 28 29 30
31
四月 五月 六月
日 一 二 三 四 五 六 日 一 二 三 四 五 六 日 一 二 三 四 五 六
1 2 3 4 5 6 1 2 3 4 1
7 8 9 10 11 12 13 5 6 7 8 9 10 11 2 3 4 5 6 7 8
14 15 16 17 18 19 20 12 13 14 15 16 17 18 9 10 11 12 13 14 15
21 22 23 24 25 26 27 19 20 21 22 23 24 25 16 17 18 19 20 21 22
28 29 30 26 27 28 29 30 31 23 24 25 26 27 28 29
30
七月 八月 九月
日 一 二 三 四 五 六 日 一 二 三 四 五 六 日 一 二 三 四 五 六
1 2 3 4 5 6 1 2 3 1 2 3 4 5 6 7
7 8 9 10 11 12 13 4 5 6 7 8 9 10 8 9 10 11 12 13 14
14 15 16 17 18 19 20 11 12 13 14 15 16 17 15 16 17 18 19 20 21
21 22 23 24 25 26 27 18 19 20 21 22 23 24 22 23 24 25 26 27 28
28 29 30 31 25 26 27 28 29 30 31 29 30
十月 十一月 十二月
日 一 二 三 四 五 六 日 一 二 三 四 五 六 日 一 二 三 四 五 六
1 2 3 4 5 1 2 1 2 3 4 5 6 7
6 7 8 9 10 11 12 3 4 5 6 7 8 9 8 9 10 11 12 13 14
13 14 15 16 17 18 19 10 11 12 13 14 15 16 15 16 17 18 19 20 21
20 21 22 23 24 25 26 17 18 19 20 21 22 23 22 23 24 25 26 27 28
27 28 29 30 31 24 25 26 27 28 29 30 29 30 31
[root@MineGi ~]# cal 9 1752
九月 1752
日 一 二 三 四 五 六
1 2 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
[root@MineGi ~]# cal 1 2024
一月 2024
日 一 二 三 四 五 六
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
[root@MineGi ~]# cal 2 2323
二月 2323
日 一 二 三 四 五 六
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
[root@MineGi ~]#