Linux 命令行
控制台
Console,也就是控制台,也称为终端,提供命令行输入数据和展示命令行输出数据。
- 物理控制台,用于与系统交互的硬件接口。
- 虚拟控制台, 名称为tty1--tty6,通过ctrl + alt + F1...F6切换。tty1是图形化程序所在的终端。
ctrl + alt + F3 进入3号虚拟控制台,登录后,执行tty命令,效果如下:
输入密码的时候,屏幕是不显示任何字符的。
shell 介绍
shell,用于解析终端中输入的字符串,交由kernel执行,并将执行结果显示到终端中。Linux命
令行由shell程序提供。在Linux发展过程中,产生了多种shell程序。Bash是类UNIX系统中最成
功的shell,也是用户的默认shell。
命令提示符
作用:提示计算机正在等待用户的输入。
# 代表普通用户提示符,提示符最后是有一个空格。 [laoma@CentOS7 ~]$ date # 代表管理员提示符 [root@CentOS7 ~]#shell 解析字符串的语法
shell命令行,通过空格分隔,包涵三个部分:
- 命令,第一部分必须是命令,代表要执行的程序,其后可能跟着选项或参数。
- 选项,调整命令的行为或作用。通常以一个或两个 - 符号开头,例如(-a --all)。
- 参数,典型的参数是命令的目标,命令后面可能接多个参数。
Enter键,shell开始解析命令行中字符串,并交由kernel执行。
示例:
[laoma@CentOS7 ~]$ ls -1 /home # ls,是命令 # -l,是选项 # /home,是参数 # 只用命令情况 [laoma@CentOS7 ~]$ ls 公共 模板 视频 图片 文档 下载 音乐 桌面 # 使用不同格式选项,-a和--all都是用于显示所有文件,包涵.开头的隐藏文件 [laoma@CentOS7 ~]$ ls -a . .bashrc .esd_auth .mozilla 模板 下载 .. .cache .gnupg .ssh 视频 音乐 .bash_logout .config .ICEauthority .Xauthority 图片 桌面 .bash_profile .dbus .local 公共 文档 [laoma@CentOS7 ~]$ ls --all . .bashrc .esd_auth .mozilla 模板 下载 .. .cache .gnupg .ssh 视频 音乐 .bash_logout .config .ICEauthority .Xauthority 图片 桌面 .bash_profile .dbus .local 公共 文档 # 使用参数,指定目标,查看目录/etc/yum文件清单 [laoma@CentOS7 ~]$ ls /etc/yum fssnap.d pluginconf.d protected.d vars version-groups.conf命令行语法示例
[laoma@CentOS7 ~]$ date --help 用法:date [选项]... [+格式] 或:date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]] Display the current time in the given FORMAT, or set the system date. Mandatory arguments to long options are mandatory for short options too. -d, --date=STRING display time described by STRING, not 'now' -f, --file=DATEFILE like --date once for each line of DATEFILE ... ... [laoma@CentOS7 ~]$ date 2022年 11月 03日 星期四 13:38:59 CST [laoma@CentOS7 ~]$ date -u 2022年 11月 03日 星期四 05:39:09 UTC [laoma@CentOS7 ~]$ date --utc 2022年 11月 03日 星期四 05:40:06 UTC [laoma@CentOS7 ~]$ date --universal 2022年 11月 03日 星期四 05:40:31 UTC [laoma@CentOS7 ~]$ date -d '-1 day' 2022年 11月 02日 星期三 13:42:03 CST [laoma@CentOS7 ~]$ date --date '-1 day' 2022年 11月 02日 星期三 13:42:29 CST [laoma@CentOS7 ~]$ date --date='-1 day' 2022年 11月 02日 星期三 13:43:04 CST [laoma@CentOS7 ~]$ date --date='100 day' 2023年 02月 11日 星期六 13:44:00 CST # 特殊案例 [laoma@CentOS7 ~]$ date -d "-1 year +3 month +10 days" 2022年 02月 13日 星期日 14:20:59 CST [laoma@CentOS7 ~]$ date +%Y%m%d 20221103 [laoma@CentOS7 ~]$ date +%Y-%m-%d 2022-11-03 [laoma@CentOS7 ~]$ date +%Y/%m/%d 2022/11/03一行要执行多个命令,使用分号(;)分隔。
[laoma@CentOS7 ~]$ ls 公共 模板 视频 图片 文档 下载 音乐 桌面 [laoma@CentOS7 ~]$ date 2022年 11月 03日 星期四 13:52:05 CST [laoma@CentOS7 ~]$ ls;date 公共 模板 视频 图片 文档 下载 音乐 桌面 2022年 11月 03日 星期四 13:52:08 CST # 错误的示例 [laoma@CentOS7 ~]$ ls date ls: 无法访问date: 没有那个文件或目录bash 执行命令
passwd
[laoma@CentOS7 ~]$ passwd --help 用法: passwd [选项...] <帐号名称> # 普通用户修改密码 [laoma@CentOS7 ~]$ passwd 更改用户 laoma 的密码 。 为 laoma 更改 STRESS 密码。 (当前)UNIX 密码:`redhat` 新的 密码:`123`·` 无效的密码: 密码少于 8 个字符 新的 密码:`12345678` 无效的密码: 密码未通过字典检查 - 过于简单化/系统化 新的 密码:`55TurnK3y` 重新输入新的 密码:`55TurnK3y` passwd:所有的身份验证令牌已经成功更新。 # root 用户管理账户密码 [root@CentOS79 ~]# passwd laoma 更改用户 laoma 的密码 。 新的 密码:`redhat` 无效的密码: 密码少于 8 个字符 重新输入新的 密码:`redhat` passwd:所有的身份验证令牌已经成功更新。 # 删除用户密码,实现用户登录不需要密码 [root@CentOS79 ~]# passwd -d laoma 清除用户的密码 laoma。 passwd: 操作成功 # 锁定用户密码,用户将无法登录 [root@CentOS79 ~]# passwd -l laoma 锁定用户 laoma 的密码 。 passwd: 操作成功 # 解锁用户密码,用户可以正常登录 [root@CentOS79 ~]# passwd -u laoma 解锁用户 laoma 的密码。 passwd: 操作成功file
[laoma@CentOS7 ~]$ file --help Usage: file [OPTION...] [FILE...] Determine type of FILEs. [laoma@CentOS7 ~]$ file /etc/yum /etc/yum: directory [laoma@CentOS7 ~]$ file /etc/fstab /etc/fstab: ASCII text # /bin/file是二进制程序 [laoma@CentOS7 ~]$ file /bin/file /bin/file: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=58f6c8b82ee70887d7136a7181ffc8fa18030cde, strippedcat
[laoma@CentOS7 ~]$ cat --help 用法:cat [选项]... [文件]... 将[文件]或标准输入组合输出到标准输出。 [laoma@CentOS7 ~]$ cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 10.1.8.88 CentOS79.redhat.fun CentOS79 # -A选项等于-vET,用于显示行尾加个$,用^I显示制表符(tab键) [laoma@CentOS7 ~]$ cat -A /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4$ ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6$ 10.1.8.88^ICentOS79.redhat.fun^ICentOS79$ # 一次性查看多个文件 [laoma@CentOS7 ~]$ cat /etc/hosts /etc/fstab 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 10.1.8.88 CentOS79.redhat.fun CentOS79 # # /etc/fstab # Created by anaconda on Wed Nov 2 17:31:41 2022 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # /dev/mapper/centos-root / xfs defaults 0 0 UUID=047729d6-683f-4aec-b614-28d20ce0a90c /boot xfs defaults 0 0 /dev/mapper/centos-home /home xfs defaults 0 0 /dev/mapper/centos-swap swap swap defaults 0 0head
[laoma@CentOS7 ~]$ head --help 用法:head [选项]... [文件]... Print the first 10 lines of each FILE to standard output. With more than one FILE, precede each with a header giving the file name. With no FILE, or when FILE is -, read standard input. [laoma@CentOS7 ~]$ head /etc/profile # /etc/profile # System wide environment and startup programs, for login setup # Functions and aliases go in /etc/bashrc # It's NOT a good idea to change this file unless you know what you # are doing. It's much better to create a custom.sh shell script in # /etc/profile.d/ to make custom changes to your environment, as this # will prevent the need for merging in future updates. # 只看前4行内容 [laoma@CentOS7 ~]$ head -n 4 /etc/profile # /etc/profile # System wide environment and startup programs, for login setup # Functions and aliases go in /etc/bashrc # 查看文件所有内容,除了最后3行 [laoma@CentOS7 ~]$ head -n -3 /etc/profiletail
[laoma@CentOS7 ~]$ tail --help 用法:tail [选项]... [文件]... Print the last 10 lines of each FILE to standard output. With more than one FILE, precede each with a header giving the file name. With no FILE, or when FILE is -, read standard input. [laoma@CentOS7 ~]$ tail /etc/profile if [ "${-#*i}" != "$-" ]; then . "$i" else . "$i" >/dev/null fi fi done unset i unset -f pathmunge [laoma@CentOS7 ~]$ tail -n 4 /etc/profile done unset i unset -f pathmunge # 从第四行到最后所有行内容 [laoma@CentOS7 ~]$ tail -n +4 /etc/profileless
# 使用less查看多页文档,可以翻页,搜索等 [laoma@CentOS7 ~]$ less -N /etc/profile # 在less查看文件过程中常见指令: # / 搜索,n(next)搜索下一个,N搜索上一个。 # 上下方向键 逐行查看,左右键左右翻页 # pageup和pagedown 上下翻页 # q,退出文档查看wc
[laoma@CentOS7 ~]$ wc --help 用法:wc [选项]... [文件]... 或:wc [选项]... --files0-from=F Print newline, word, and byte counts for each FILE, and a total line if more than one FILE is specified. With no FILE, or when FILE is -, read standard input. A word is a non-zero-length sequence of characters delimited by white space. The options below may be used to select which counts are printed, always in the following order: newline, word, character, byte, maximum line length. -c, --bytes print the byte counts -m, --chars print the character counts -l, --lines print the newline counts --files0-from=文件 从指定文件读取以NUL 终止的名称,如果该文件被 指定为"-"则从标准输入读文件名 -L, --max-line-length 显示最长行的长度 -w, --words 显示单词计数 --help 显示此帮助信息并退出 --version 显示版本信息并退出 [laoma@CentOS7 ~]$ cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 10.1.8.88 CentOS79.redhat.fun CentOS79 [laoma@CentOS7 ~]$ wc /etc/hosts 3 13 197 /etc/hosts # 统计文件line数量 [laoma@CentOS7 ~]$ wc -l /etc/hosts 3 /etc/hosts # 统计文件word数量 [laoma@CentOS7 ~]$ wc -w /etc/hosts 13 /etc/hosts # 统计文件bytes数量 [laoma@CentOS7 ~]$ wc -c /etc/hosts 197 /etc/hosts # 统计多个文件数据 [laoma@CentOS7 ~]$ wc /etc/hosts /etc/fstab 3 13 197 /etc/hosts 12 60 541 /etc/fstab 15 73 738 总用量tab 按键
补全功能:补全命令、补全选项、补全参数
# tab 一次,就出结果,说明匹配结果 # tab 两次,才补全出结果,说明匹配结果不唯一 # 补全命令 [laoma@CentOS7 ~]$ pas<tab><tab> passwd paste pasuspender [laoma@CentOS7 ~]$ pass<tab> # 自动显示为 [laoma@CentOS7 ~]$ passwd # 补全选项 [laoma@CentOS7 ~]$ passwd --d<tab> # 自动显示为 [laoma@CentOS7 ~]$ passwd --delete # 补全参数 [laoma@CentOS7 ~]$ file /e<tab> # 自动显示为 [laoma@CentOS7 ~]$ file /etc/history 命令
# history命令查看帮助不能使用history --help [laoma@centos7 ~]$ history --help -bash: history: --: 无效选项 history: 用法:history [-c] [-d 偏移量] [n] 或 history -anrw [文件名] 或 history -ps 参数 [参数...] [laoma@CentOS7 ~]$ help history history: history [-c] [-d 偏移量] [n] 或 history -anrw [文件名] 或 history -ps 参数 [参数...] 显示或操纵历史列表。 带行号显示历史列表,将每个被修改的条目加上前缀 `*'。 参数 N 会仅列出最后的 N 个条目。 选项: -c 删除所有条目从而清空历史列表。 -d 偏移量 从指定位置删除历史列表。 -a 将当前绘画的历史行追加到历史文件中 -n 从历史文件中读取所有未被读取的行 -r 读取历史文件并将内容追加到历史列表中 -w 将当前历史写入到历史文件中,并追加到历史列表中 -p 对每一个 ARG 参数展开历史并显示结果,而不存储到历史列表中 -s 以单条记录追加 ARG 到历史列表中 如果给定了 FILENAME 文件名,则它将被作为历史文件。否则 如果 $HISTFILE 变量有值的话使用之,不然使用 ~/.bash_history 文件。 如果 $HISTTIMEFORMAT 变量被设定并且不为空,它的值会被用于 strftime(3) 的格式字符串来打印与每一个显示的历史条目想关联的时 间戳,否则不打印时间戳。 退出状态: 返回成功,除非使用了无效的选项或者发生错误。 [laoma@CentOS7 ~]$ history 1 date 2 date -u 3 date --utc 4 date --universal 5 date -d '-1 day' ... ... # 情况历史操作记录 [laoma@CentOS7 ~]$ history -c # 使用上下键查找历史命令的时候,只能查找现有的历史命令中命令 [laoma@CentOS7 ~]$ history 1 history 2 echo hello world 3 ls 4 hostname 5 history # 调用历史第2条命令,显示第2条命令后,然后执行该命令 [laoma@CentOS7 ~]$ !2 echo hello world hello world # 从下网上匹配最后一个以ho开头的命令,显示并执行该命令 [laoma@CentOS7 ~]$ !ho hostname CentOS79.wanho.net # 使用-w选将历史命令写入到文件~/.bash_history [laoma@CentOS7 ~]$ history -w [laoma@CentOS7 ~]$ cat ~/.bash_history history echo hello world ls hostname history echo hello world hostname history history -wscript 命令
[laoma@centos7 ~]$ script --help 用法: script [选项] [文件] 选项: -a, --append 追加输出 -c, --command <命令> 运行命令而不是交互式 shell -e, --return 返回子进程的退出代码 -f, --flush 每次 write(写) 后运行 flush(冲刷) --force 即使输出文件是链接也依然使用 -q, --quiet 安静模式 -t, --timing[=<文件>] 将时间数据输出到标准错误(或文件) -V, --version 输出版本信息并退出 -h, --help 显示此帮助并退出 # 记录终端中历史操作,并保存到指定文件,还可以同时记录操作时间 [laoma@centos7 ~]$ script --timing=time.log record.log # 或者 [laoma@centos7 ~]$ script -ttime.log record.log [laoma@centos7 ~]$ echo hello world hello world [laoma@centos7 ~]$ hostname centos79.linux.com [laoma@centos7 ~]$ date 2022年 12月 13日 星期二 17:10:13 CST [laoma@centos7 ~]$ history -c [laoma@centos7 ~]$ echo goodbye goodbye [laoma@centos7 ~]$ exit exit Script done, file is record.log # 查看日志内容 [laoma@centos7 ~]$ cat record.log 脚本启动于 2022年12月13日 星期二 17时10分02秒 [laoma@centos7 ~]$ echo hello world hello world [laoma@centos7 ~]$ hostname centos79.linux.com [laoma@centos7 ~]$ date 2022年 12月 13日 星期二 17:10:13 CST [laoma@centos7 ~]$ history -c [laoma@centos7 ~]$ echo goodbye goodbye [laoma@centos7 ~]$ exit exit Script done on 2022年12月13日 星期二 17时10分27秒 # 动态回放日志 [laoma@centos7 ~]$ scriptreplay -t time.log record.logbash 快捷键
| 快捷键 | 作用 |
| Ctrl+a 或者 Home | 光标定位到命令行开头 |
| Ctrl+e 或者 End | 光标定位到命令行末尾 |
| Ctrl+u | 删除光标位置到命令行开头 |
| Ctrl+k | 删除光标位置到命令行末尾 |
| Ctrl+<- | 光标向左跳转一个单词 |
| Ctrl+-> | 光标向右跳转一个单词 |
| Ctrl+r | 搜索历史命令 |
| Ctrl+w | 向左删除一个参数 |
| Esc+. | 打印上一个命令的最后一个参数 |
| Ctrl+l(L的小写) | 清空屏幕 |
| Ctrl+d | 登出终端 |
命令行管理文件
文件系统层次结构
FHS
Linux遵循开源协议,所以任何人都可以根据Linux的核心代码制作和发行版本。如果每个人都按
自己的喜好,在/目录下创建目录、存放文件,将导致其他人无法快速使用他人的linux系统。为避
免这样的情况,FHS就应运而生,对linux文件系统目录结构进行规范化。
FHS(Filesystem Hierarchy Standard)采用树形结构组织文件,定义了系统中每个区域的用途、所
需要的最小构成的文件和目录,同时还给出了例外处理与矛盾处理。
FHS定义了两层规范:
- 定义了 / 下面的各个目录应该要放什么文件数据。例如/etc应该要放置设置文件,/bin与/sbin则应该要放置可执行文件等等。
- 针对/usr及/var这两个目录的子目录来定义。例如/var/log放置系统登录文件、/usr/share放置共享数据等等。
USR,Unix System Resource
| 位置 | 用途 |
| /usr | 系统安装的软件、共享的库。重要的子目录有:/usr/bin: 用户命令。/usr/sbin: 系统管理命令。 /usr/lib、/usr/lib64:应用程序可调用的通用库文件。 /usr/local: 本地自定义软件。 |
| /etc | 系统配置文件。 |
| /var | variable,系统可变数据,在系统启动之间保持永久性,如数据库、 缓存目录、 日志文件、打印机后台处理文档和网站内容。 |
| /run | 自本次系统启动以来运行中的进程的运行数据,包括进程ID文件和锁定文件,等 等。此目录中的内容在重启时重新创建。此目录合并了早期版本的Linux 中 的/var/run和/var/lock。 |
| /home | 主目录是普通用户存储其个人数据和配置文件的位置。 每个用户有自己的位置, 例如 /home/laoma。 |
| /root | 管理员root的主目录。 |
| /tmp | 供临时文件使用的全局可写空间。 10天内未访问、未更改或未修改的文件将自动 从该目录中删除。 还有一个临时目录/var/tmp,该目录中的文件如果在30天内未 曾访问、更改或修改过, 将被自动删除。 |
| /boot | 开机启动过程所需的文件。 |
| /dev | device,包含特殊的设备文件,供系统用于访问硬件。 |
文件路径导航
导航路径 cd 和 pwd
常见路径说明:
- 绝对路径:以根(/)开头的路径,用于指定文件的确切位置。
- 当前工作路径:当前所处的位置。
- 相对路径:不是以根(/)开头的路径,相对当前工作目录的路径。
# 显示当前工作目录 [laoma@centos7 ~]$ pwd /home/laoma # 切换路径 [laoma@centos7 ~]$ cd /usr/bin [laoma@centos7 bin]$ pwd /usr/bin [laoma@centos7 bin]$ ls [ mpartition a2p mpls_dump ab mpris-proxy abrt-action-analyze-backtrace mpstat ... ... # 切换到其他目录 [laoma@centos7 bin]$ cd /usr/share/doc # 切换上一次所在目录 [laoma@centos7 doc]$ cd - /usr/bin [laoma@centos7 bin]$ pwd /usr/bin # 切换到家目录 [laoma@centos7 bin]$ cd # 或者 [laoma@centos7 bin]$ cd ~ [laoma@centos7 ~]$ pwd /home/laoma # 上一级目录(父目录),用..表示 [laoma@centos7 ~]$ pwd /home/laoma [laoma@centos7 ~]$ ls /home laoma [laoma@centos7 ~]$ cd .. [laoma@centos7 home]$ pwd /home # 当前目录用.表示,通常不写. [laoma@centos7 ~]$ file ./.bash_history ./.bash_history: UTF-8 Unicode text [laoma@centos7 ~]$ file .bash_history .bash_history: UTF-8 Unicode text [laoma@centos7 ~]$ cd /usr/share/doc/at-spi2-core-2.28.0/ # 使用相对路径切换到/usr/share/ [laoma@centos7 at-spi2-core-2.28.0]$ cd ../../ [laoma@centos7 share]$ pwd /usr/share [laoma@centos7 ~]$ cd /usr/share/doc/at-spi2-core-2.28.0/ # 使用相对路径切换到 /etc/yum [laoma@centos7 share]$ cd ../../../../etc/yum [laoma@centos7 yum]$ pwd /etc/yum思考:什么时候使用相对路径?什么时候使用绝对路径?
答案:一般情况是怎么方便怎么来。但是在变量定义文件的路径时候,尽量使用绝对路径。
ls 命令补充
# -1(数字1)选项以单列格式展示 # 图形化界面,并且用户登录系统后才会有这些文件。 [laoma@centos7 ~]$ ls -1 公共 模板 视频 图片 文档 下载 音乐 桌面 # -R 选项递归查看子目录中文件 [laoma@centos7 ~]$ ls /etc/yum fssnap.d pluginconf.d protected.d vars version-groups.conf [laoma@centos7 ~]$ ls -R /etc/yum /etc/yum: fssnap.d pluginconf.d protected.d vars version-groups.conf /etc/yum/fssnap.d: /etc/yum/pluginconf.d: fastestmirror.conf langpacks.conf /etc/yum/protected.d: systemd.conf /etc/yum/vars: contentdir infra # -l选项 长列表格式查看文件详细信息 [laoma@centos7 ~]$ ls /etc/hosts /etc/hosts [laoma@centos7 ~]$ ls -l /etc/hosts -rw-r--r--. 1 root root 197 11月 3 14:35 /etc/hosts # ls 命令默认按文件名先后顺序正向排序 [laoma@centos7 ~]$ ls -l 总用量 0 -rw-rw-r--. 1 laoma laoma 0 11月 3 17:18 abc -rw-rw-r--. 1 laoma laoma 0 11月 3 17:17 file-1 -rw-rw-r--. 1 laoma laoma 0 11月 3 17:18 file-2 -rw-rw-r--. 1 laoma laoma 0 11月 3 17:18 hello ... ... # 使用 -r 选项反向排序 [laoma@centos7 ~]$ ls -rl 总用量 0 -rw-rw-r--. 1 laoma laoma 0 11月 3 17:18 hello -rw-rw-r--. 1 laoma laoma 0 11月 3 17:18 file-2 -rw-rw-r--. 1 laoma laoma 0 11月 3 17:17 file-1 -rw-rw-r--. 1 laoma laoma 0 11月 3 17:18 abc # 使用 -t 选项按时间排序 [laoma@centos7 ~]$ ls -tl 总用量 0 -rw-rw-r--. 1 laoma laoma 0 11月 3 17:22 hello -rw-rw-r--. 1 laoma laoma 0 11月 3 17:18 abc -rw-rw-r--. 1 laoma laoma 0 11月 3 17:18 file-2 -rw-rw-r--. 1 laoma laoma 0 11月 3 17:17 file-1 # 使用 -d 选项只查看对象本身 [laoma@centos7 ~]$ ls -ld /etc/yum drwxr-xr-x. 6 root root 100 12月 13 10:42 /etc/yum [laoma@centos7 ~]$ ls -l /etc/yum 总用量 4 drwxr-xr-x. 2 root root 6 10月 2 2020 fssnap.d drwxr-xr-x. 2 root root 54 12月 13 10:45 pluginconf.d drwxr-xr-x. 2 root root 26 12月 13 10:42 protected.d drwxr-xr-x. 2 root root 37 10月 2 2020 vars -rw-r--r--. 1 root root 444 10月 2 2020 version-groups.conftree 命令
# 安装tree工具 [root@centos7 ~]# yum install -y tree [laoma@centos7 ~]$ tree /etc/yum /etc/yum ├── fssnap.d ├── pluginconf.d │ ├── fastestmirror.conf │ └── langpacks.conf ├── protected.d │ └── systemd.conf ├── vars │ ├── contentdir │ └── infra └── version-groups.conf 4 directories, 6 filesstat 命令
查看文件的元数据属性。
[laoma@centos7 ~]$ stat /etc/fstab 文件:"/etc/fstab" 大小:541 块:8 IO 块:4096 普通文件 设备:fd00h/64768d Inode:67160130 硬链接:1 权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root) 环境:system_u:object_r:etc_t:s0 最近访问:2022-12-14 11:09:49.628179033 +0800 最近更改:2022-12-13 10:38:44.625179973 +0800 最近改动:2022-12-13 10:53:38.563774524 +0800 创建时间:-touch 命令(创建文件)
[laoma@centos7 ~]$ touch --help 用法:touch [选项]... 文件... Update the access and modification times of each FILE to the current time. A FILE argument that does not exist is created empty, unless -c or -h is supplied. A FILE argument string of - is handled specially and causes touch to change the times of the file associated with standard output. Mandatory arguments to long options are mandatory for short options too. -a 只更改访问时间 -c, --no-create 不创建任何文件 -d, --date=字符串 使用指定字符串表示时间而非当前时间 -f (忽略) -h, --no-dereference 会影响符号链接本身,而非符号链接所指示的目的地 (当系统支持更改符号链接的所有者时,此选项才有用) -m 只更改修改时间 -r, --reference=FILE use this file's times instead of current time -t STAMP use [[CC]YY]MMDDhhmm[.ss] instead of current time --time=WORD change the specified time: WORD is access, atime, or use: equivalent to -a WORD is modify or mtime: equivalent to -m --help 显示此帮助信息并退出 --version 显示版本信息并退出 请注意,-d 和-t 选项可接受不同的时间/日期格式。 # 更新现有文件时间戳:包括访问时间,更改时间和修改时间。 [laoma@centos7 ~]$ ls -l zhang.txt -rw-rw-r--. 1 laoma laoma 0 12月 14 10:51 zhang.txt [laoma@centos7 ~]$ touch zhang.txt [laoma@centos7 ~]$ ls -l zhang.txt -rw-rw-r--. 1 laoma laoma 0 12月 14 13:36 zhang.txt [laoma@centos7 ~]$ date 2022年 12月 14日 星期三 13:36:42 CST [laoma@centos7 ~]$ stat zhang.txt 文件:"zhang.txt" 大小:0 块:0 IO 块:4096 普通空文件 设备:fd02h/64770d Inode:116 硬链接:1 权限:(0664/-rw-rw-r--) Uid:( 1000/ laoma) Gid:( 1000/ laoma) 环境:unconfined_u:object_r:user_home_t:s0 最近访问:2022-12-14 13:36:36.396932594 +0800 最近更改:2022-12-14 13:36:36.396932594 +0800 最近改动:2022-12-14 13:36:36.396932594 +0800 创建时间:- # 如果文件不存在,则创建文件 [laoma@centos7 ~]$ ls zhang-f1.txt ls: 无法访问zhang-f1.txt: 没有那个文件或目录 [laoma@centos7 ~]$ touch zhang-f1.txt [laoma@centos7 ~]$ ls zhang-f1.txt zhang-f1.txt # 使用-c选项时候,如果文件不存在,则不会创建文件。 [laoma@centos7 ~]$ ls zhang-f2.txt ls: 无法访问zhang-f2.txt: 没有那个文件或目录 [laoma@centos7 ~]$ touch -c zhang-f2.txt [laoma@centos7 ~]$ ls zhang-f2.txt ls: 无法访问zhang-f2.txt: 没有那个文件或目录| 操作 | 单一来源 | 多来源 |
| 复制文件 | cp file1 file2 | cp file1 file2 file3 dir |
| 移动文件 | mv file1 file2 | mv file1 file2 file3 dir |
| 删除文件 | rm file1 | rm -f file1 file2 file3 |
| 创建目录 | mkdir dir | mkdir -p dir1/dir2/dir3 |
| 复制目录 | cp -r dir1 dir2 | cp -r dir1 dir2 dir3 dir4 |
| 移动目录 | mv dir1 dir2 | mv dir1 dir2 dir3 dir4 |
| 删除目录 | rm -r dir1 或 rmdir dir1 | rm -rf dir1 dir2 dir3 |
mkdir 命令(创建文件夹)
# 创建目录 [laoma@centos7 ~]$ mkdir lab [laoma@centos7 ~]$ ls lab 公共 模板 视频 图片 文档 下载 音乐 桌面 [laoma@centos7 ~]$ mkdir lab1 lab2 [laoma@centos7 ~]$ ls lab lab1 lab2 公共 模板 视频 图片 文档 下载 音乐 桌面 # -p 选项级联创建目录 [laoma@centos7 ~]$ mkdir dir1/dir2/dir3/dir4 mkdir: 无法创建目录"dir1/dir2/dir3/dir4": 没有那个文件或目录 [laoma@centos7 ~]$ mkdir -p dir1/dir2/dir3/dir4 [laoma@centos7 ~]$ tree dir1 dir1 └── dir2 └── dir3 └── dir4 3 directories, 0 filescp 命令
复制文件
# 复制单个文件到目标位置 [laoma@centos7 ~]$ cd lab [laoma@centos7 lab]$ ls [laoma@centos7 lab]$ cp /etc/hosts . [laoma@centos7 lab]$ ls hosts # 复制过来并且重命名 [laoma@centos7 lab]$ cp /etc/hosts ./hosts-1 [laoma@centos7 lab]$ ls hosts hosts-1 # 当前目录下有同名称文件,不会提示直接覆盖[laoma@centos7 lab]$ cp /etc/hosts ./hosts-1 # 通过 -i 选项,提示是否覆盖 [laoma@centos7 lab]$ cp -i /etc/hosts ./hosts-1 cp:是否覆盖"./hosts-1"? yes # 复制多个文件,目标只能是目录 [laoma@centos7 lab]$ cp /etc/passwd /etc/host.conf ./hosts-all cp: 目标"./hosts-all" 不是目录 [laoma@centos7 lab]$ cp /etc/passwd /etc/host.conf . [laoma@centos7 lab]$ ls host.conf hosts hosts-1 passwd复制目录
# 使用 -r 选项复制目录 [laoma@centos7 lab]$ cp /etc/yum . cp: 略过目录"/etc/yum" [laoma@centos7 lab]$ cp -r /etc/yum . [laoma@centos7 lab]$ ls host.conf hosts hosts-1 passwd yum # 复制过来,并使用新的名称 [laoma@centos7 lab]$ cp -r /etc/yum ./yum-1 [laoma@centos7 lab]$ ls host.conf hosts hosts-1 passwd yum yum-1 # 当前目录下有同名称目录,则将源目录放到相同目录下面,而不是覆盖当前目录 [laoma@centos7 lab]$ ls yum fssnap.d pluginconf.d protected.d vars version-groups.conf [laoma@centos7 lab]$ cp -r /etc/yum ./yum [laoma@centos7 lab]$ ls host.conf hosts hosts-1 passwd yum yum-1 [laoma@centos7 lab]$ ls yum fssnap.d pluginconf.d protected.d vars version-groups.conf yum # 复制多个目录,目标必须是已经存在的目录 [laoma@centos7 lab]$ cp -r /etc /home ./mydir cp: 目标"./mydir" 不是目录 [laoma@centos7 lab]$ cp -r /etc /home . ... ... cp: 无法打开"/etc/sudo.conf" 读取数据: 权限不够 cp: 无法访问"/etc/sudoers.d": 权限不够 ... ... # 以上信息提示权限不够,暂不用理会 [laoma@centos7 lab]$ ls etc home host.conf hosts hosts-1 passwd yum yum-1 [laoma@centos7 lab]$ ls etc homemv 命令
# 我们使用前面创建的lab1目录 [laoma@centos7 lab]$ ls -dl ../lab1 drwxrwxr-x. 2 laoma laoma 6 11月 4 10:32 ../lab1 # 移动单个文件 [laoma@centos7 lab]$ ls etc home host.conf hosts hosts-1 passwd yum yum-1 [laoma@centos7 lab]$ mv hosts-1 ../lab1 [laoma@centos7 lab]$ ls etc home host.conf hosts passwd yum yum-1 [laoma@centos7 lab]$ ls ../lab1 hosts-1 # 移动多个文件,目标位置只能是目录 [laoma@centos7 lab]$ mv passwd hosts /home/laoma/lab1/ [laoma@centos7 lab]$ ls etc home host.conf yum yum-1 [laoma@centos7 lab]$ ls ../lab1 hosts hosts-1 passwd # 重命名文件 [laoma@centos7 lab]$ ls etc home host.conf yum yum-1 [laoma@centos7 lab]$ mv host.conf host.conf-new [laoma@centos7 lab]$ ls etc home host.conf-new yum yum-1 # 移动单个目录 [laoma@centos7 lab]$ mv etc ../lab1 [laoma@centos7 lab]$ ls home host.conf-new yum yum-1 [laoma@centos7 lab]$ ls ../lab1 etc hosts hosts-1 passwd # 移动多个目录 [laoma@centos7 lab]$ mv home/ yum/ yum-1/ ../lab1 [laoma@centos7 lab]$ ls host.conf-new [laoma@centos7 lab]$ ls ../lab1 etc home hosts hosts-1 passwd yum yum-1rmdir 命令
# 删除空目录 [laoma@centos7 lab]$ rmdir ../lab2 [laoma@centos7 lab1]$ rmdir etc rmdir: 删除 "etc" 失败: 目录非空rm 命令
[laoma@centos7 lab]$ cd ../lab1 [laoma@centos7 lab1]$ ls etc home hosts hosts-1 passwd yum yum-1 # 删除文件 [laoma@centos7 lab1]$ rm hosts [laoma@centos7 lab1]$ ls etc home hosts-1 passwd yum yum-1 [laoma@centos7 lab1]$ rm hosts-1 passwd [laoma@centos7 lab1]$ ls etc home yum yum-1 # 强制删除具有写保护的文件, [laoma@centos7 lab1]$ cp /etc/pki/ca-trust/extracted/java/cacerts . [laoma@centos7 lab1]$ rm cacerts rm:是否删除有写保护的普通文件 "cacerts"?yes [laoma@centos7 lab1]$ ls cacerts ls: 无法访问cacerts: 没有那个文件或目录 # 使用-f选项,直接删除 [laoma@centos7 lab1]$ cp /etc/pki/ca-trust/extracted/java/cacerts . [laoma@centos7 lab1]$ rm -f cacerts # -r选项递归删除目录 [laoma@centos7 lab1]$ rm -r yum [laoma@centos7 lab1]$ ls etc home yum-1 # 递归强制删除目录 -fr选项 [laoma@centos7 lab1]$ rm -fr etc [laoma@centos7 lab1]$ ls home yum-1 # 删除多个目录 [laoma@centos7 lab1]$ rm -fr home/ yum-1/ [laoma@centos7 lab1]$ ls # 清理实验环境 [laoma@centos7 lab1]$ cd .. [laoma@centos7 ~]$ ls dir1 lab lab1 lab2 公共 模板 视频 图片 文档 下载 音乐 桌面 [laoma@centos7 ~]$ rm -fr lab lab1 lab2 dir1 [laoma@centos7 ~]$ ls 公共 模板 视频 图片 文档 下载 音乐 桌面