专业的微网站公司,seo商学院,渭南住建网,区域名 网站建设公司的销售好做吗Linux 命令行控制台Console#xff0c;也就是控制台#xff0c;也称为终端#xff0c;提供命令行输入数据和展示命令行输出数据。物理控制台#xff0c;用于与系统交互的硬件接口。虚拟控制台#xff0c; 名称为tty1--tty6#xff0c;通过ctrl alt F1...F6切换。tty1是…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。命令提示符作用提示计算机正在等待用户的输入。# 代表普通用户提示符提示符最后是有一个空格。 [laomaCentOS7 ~]$ date # 代表管理员提示符 [rootCentOS7 ~]#shell 解析字符串的语法shell命令行通过空格分隔包涵三个部分命令第一部分必须是命令代表要执行的程序其后可能跟着选项或参数。选项调整命令的行为或作用。通常以一个或两个 - 符号开头例如(-a --all)。参数典型的参数是命令的目标命令后面可能接多个参数。Enter键shell开始解析命令行中字符串并交由kernel执行。示例[laomaCentOS7 ~]$ ls -1 /home # ls是命令 # -l是选项 # /home是参数 # 只用命令情况 [laomaCentOS7 ~]$ ls 公共 模板 视频 图片 文档 下载 音乐 桌面 # 使用不同格式选项-a和--all都是用于显示所有文件包涵.开头的隐藏文件 [laomaCentOS7 ~]$ ls -a . .bashrc .esd_auth .mozilla 模板 下载 .. .cache .gnupg .ssh 视频 音乐 .bash_logout .config .ICEauthority .Xauthority 图片 桌面 .bash_profile .dbus .local 公共 文档 [laomaCentOS7 ~]$ ls --all . .bashrc .esd_auth .mozilla 模板 下载 .. .cache .gnupg .ssh 视频 音乐 .bash_logout .config .ICEauthority .Xauthority 图片 桌面 .bash_profile .dbus .local 公共 文档 # 使用参数指定目标查看目录/etc/yum文件清单 [laomaCentOS7 ~]$ ls /etc/yum fssnap.d pluginconf.d protected.d vars version-groups.conf命令行语法示例[laomaCentOS7 ~]$ 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, --dateSTRING display time described by STRING, not now -f, --fileDATEFILE like --date once for each line of DATEFILE ... ... [laomaCentOS7 ~]$ date 2022年 11月 03日 星期四 13:38:59 CST [laomaCentOS7 ~]$ date -u 2022年 11月 03日 星期四 05:39:09 UTC [laomaCentOS7 ~]$ date --utc 2022年 11月 03日 星期四 05:40:06 UTC [laomaCentOS7 ~]$ date --universal 2022年 11月 03日 星期四 05:40:31 UTC [laomaCentOS7 ~]$ date -d -1 day 2022年 11月 02日 星期三 13:42:03 CST [laomaCentOS7 ~]$ date --date -1 day 2022年 11月 02日 星期三 13:42:29 CST [laomaCentOS7 ~]$ date --date-1 day 2022年 11月 02日 星期三 13:43:04 CST [laomaCentOS7 ~]$ date --date100 day 2023年 02月 11日 星期六 13:44:00 CST # 特殊案例 [laomaCentOS7 ~]$ date -d -1 year 3 month 10 days 2022年 02月 13日 星期日 14:20:59 CST [laomaCentOS7 ~]$ date %Y%m%d 20221103 [laomaCentOS7 ~]$ date %Y-%m-%d 2022-11-03 [laomaCentOS7 ~]$ date %Y/%m/%d 2022/11/03一行要执行多个命令使用分号;分隔。[laomaCentOS7 ~]$ ls 公共 模板 视频 图片 文档 下载 音乐 桌面 [laomaCentOS7 ~]$ date 2022年 11月 03日 星期四 13:52:05 CST [laomaCentOS7 ~]$ ls;date 公共 模板 视频 图片 文档 下载 音乐 桌面 2022年 11月 03日 星期四 13:52:08 CST # 错误的示例 [laomaCentOS7 ~]$ ls date ls: 无法访问date: 没有那个文件或目录bash 执行命令passwd[laomaCentOS7 ~]$ passwd --help 用法: passwd [选项...] 帐号名称 # 普通用户修改密码 [laomaCentOS7 ~]$ passwd 更改用户 laoma 的密码 。 为 laoma 更改 STRESS 密码。 当前UNIX 密码redhat 新的 密码123· 无效的密码 密码少于 8 个字符 新的 密码12345678 无效的密码 密码未通过字典检查 - 过于简单化/系统化 新的 密码55TurnK3y 重新输入新的 密码55TurnK3y passwd所有的身份验证令牌已经成功更新。 # root 用户管理账户密码 [rootCentOS79 ~]# passwd laoma 更改用户 laoma 的密码 。 新的 密码redhat 无效的密码 密码少于 8 个字符 重新输入新的 密码redhat passwd所有的身份验证令牌已经成功更新。 # 删除用户密码实现用户登录不需要密码 [rootCentOS79 ~]# passwd -d laoma 清除用户的密码 laoma。 passwd: 操作成功 # 锁定用户密码用户将无法登录 [rootCentOS79 ~]# passwd -l laoma 锁定用户 laoma 的密码 。 passwd: 操作成功 # 解锁用户密码用户可以正常登录 [rootCentOS79 ~]# passwd -u laoma 解锁用户 laoma 的密码。 passwd: 操作成功file[laomaCentOS7 ~]$ file --help Usage: file [OPTION...] [FILE...] Determine type of FILEs. [laomaCentOS7 ~]$ file /etc/yum /etc/yum: directory [laomaCentOS7 ~]$ file /etc/fstab /etc/fstab: ASCII text # /bin/file是二进制程序 [laomaCentOS7 ~]$ 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[laomaCentOS7 ~]$ cat --help 用法cat [选项]... [文件]... 将[文件]或标准输入组合输出到标准输出。 [laomaCentOS7 ~]$ 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键 [laomaCentOS7 ~]$ 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$ # 一次性查看多个文件 [laomaCentOS7 ~]$ 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 UUID047729d6-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[laomaCentOS7 ~]$ 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. [laomaCentOS7 ~]$ head /etc/profile # /etc/profile # System wide environment and startup programs, for login setup # Functions and aliases go in /etc/bashrc # Its NOT a good idea to change this file unless you know what you # are doing. Its 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行内容 [laomaCentOS7 ~]$ head -n 4 /etc/profile # /etc/profile # System wide environment and startup programs, for login setup # Functions and aliases go in /etc/bashrc # 查看文件所有内容除了最后3行 [laomaCentOS7 ~]$ head -n -3 /etc/profiletail[laomaCentOS7 ~]$ 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. [laomaCentOS7 ~]$ tail /etc/profile if [ ${-#*i} ! $- ]; then . $i else . $i /dev/null fi fi done unset i unset -f pathmunge [laomaCentOS7 ~]$ tail -n 4 /etc/profile done unset i unset -f pathmunge # 从第四行到最后所有行内容 [laomaCentOS7 ~]$ tail -n 4 /etc/profileless# 使用less查看多页文档可以翻页搜索等 [laomaCentOS7 ~]$ less -N /etc/profile # 在less查看文件过程中常见指令 # / 搜索nnext搜索下一个N搜索上一个。 # 上下方向键 逐行查看左右键左右翻页 # pageup和pagedown 上下翻页 # q退出文档查看wc[laomaCentOS7 ~]$ wc --help 用法wc [选项]... [文件]... 或wc [选项]... --files0-fromF 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 显示版本信息并退出 [laomaCentOS7 ~]$ 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 [laomaCentOS7 ~]$ wc /etc/hosts 3 13 197 /etc/hosts # 统计文件line数量 [laomaCentOS7 ~]$ wc -l /etc/hosts 3 /etc/hosts # 统计文件word数量 [laomaCentOS7 ~]$ wc -w /etc/hosts 13 /etc/hosts # 统计文件bytes数量 [laomaCentOS7 ~]$ wc -c /etc/hosts 197 /etc/hosts # 统计多个文件数据 [laomaCentOS7 ~]$ wc /etc/hosts /etc/fstab 3 13 197 /etc/hosts 12 60 541 /etc/fstab 15 73 738 总用量tab 按键补全功能补全命令、补全选项、补全参数# tab 一次就出结果说明匹配结果 # tab 两次才补全出结果说明匹配结果不唯一 # 补全命令 [laomaCentOS7 ~]$ pastabtab passwd paste pasuspender [laomaCentOS7 ~]$ passtab # 自动显示为 [laomaCentOS7 ~]$ passwd # 补全选项 [laomaCentOS7 ~]$ passwd --dtab # 自动显示为 [laomaCentOS7 ~]$ passwd --delete # 补全参数 [laomaCentOS7 ~]$ file /etab # 自动显示为 [laomaCentOS7 ~]$ file /etc/history 命令# history命令查看帮助不能使用history --help [laomacentos7 ~]$ history --help -bash: history: --: 无效选项 history: 用法:history [-c] [-d 偏移量] [n] 或 history -anrw [文件名] 或 history -ps 参数 [参数...] [laomaCentOS7 ~]$ 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) 的格式字符串来打印与每一个显示的历史条目想关联的时 间戳否则不打印时间戳。 退出状态 返回成功除非使用了无效的选项或者发生错误。 [laomaCentOS7 ~]$ history 1 date 2 date -u 3 date --utc 4 date --universal 5 date -d -1 day ... ... # 情况历史操作记录 [laomaCentOS7 ~]$ history -c # 使用上下键查找历史命令的时候只能查找现有的历史命令中命令 [laomaCentOS7 ~]$ history 1 history 2 echo hello world 3 ls 4 hostname 5 history # 调用历史第2条命令显示第2条命令后然后执行该命令 [laomaCentOS7 ~]$ !2 echo hello world hello world # 从下网上匹配最后一个以ho开头的命令显示并执行该命令 [laomaCentOS7 ~]$ !ho hostname CentOS79.wanho.net # 使用-w选将历史命令写入到文件~/.bash_history [laomaCentOS7 ~]$ history -w [laomaCentOS7 ~]$ cat ~/.bash_history history echo hello world ls hostname history echo hello world hostname history history -wscript 命令[laomacentos7 ~]$ script --help 用法 script [选项] [文件] 选项 -a, --append 追加输出 -c, --command 命令 运行命令而不是交互式 shell -e, --return 返回子进程的退出代码 -f, --flush 每次 write(写) 后运行 flush(冲刷) --force 即使输出文件是链接也依然使用 -q, --quiet 安静模式 -t, --timing[文件] 将时间数据输出到标准错误(或文件) -V, --version 输出版本信息并退出 -h, --help 显示此帮助并退出 # 记录终端中历史操作并保存到指定文件还可以同时记录操作时间 [laomacentos7 ~]$ script --timingtime.log record.log # 或者 [laomacentos7 ~]$ script -ttime.log record.log [laomacentos7 ~]$ echo hello world hello world [laomacentos7 ~]$ hostname centos79.linux.com [laomacentos7 ~]$ date 2022年 12月 13日 星期二 17:10:13 CST [laomacentos7 ~]$ history -c [laomacentos7 ~]$ echo goodbye goodbye [laomacentos7 ~]$ exit exit Script done, file is record.log # 查看日志内容 [laomacentos7 ~]$ cat record.log 脚本启动于 2022年12月13日 星期二 17时10分02秒 [laomacentos7 ~]$ echo hello world hello world [laomacentos7 ~]$ hostname centos79.linux.com [laomacentos7 ~]$ date 2022年 12月 13日 星期二 17:10:13 CST [laomacentos7 ~]$ history -c [laomacentos7 ~]$ echo goodbye goodbye [laomacentos7 ~]$ exit exit Script done on 2022年12月13日 星期二 17时10分27秒 # 动态回放日志 [laomacentos7 ~]$ scriptreplay -t time.log record.logbash 快捷键快捷键作用Ctrla 或者 Home光标定位到命令行开头Ctrle 或者 End光标定位到命令行末尾Ctrlu删除光标位置到命令行开头Ctrlk删除光标位置到命令行末尾Ctrl-光标向左跳转一个单词Ctrl-光标向右跳转一个单词Ctrlr搜索历史命令Ctrlw向左删除一个参数Esc.打印上一个命令的最后一个参数CtrllL的小写清空屏幕Ctrld登出终端命令行管理文件文件系统层次结构FHSLinux遵循开源协议所以任何人都可以根据Linux的核心代码制作和发行版本。如果每个人都按自己的喜好在/目录下创建目录、存放文件将导致其他人无法快速使用他人的linux系统。为避免这样的情况FHS就应运而生对linux文件系统目录结构进行规范化。FHS(Filesystem Hierarchy Standard)采用树形结构组织文件定义了系统中每个区域的用途、所需要的最小构成的文件和目录同时还给出了例外处理与矛盾处理。FHS定义了两层规范定义了 / 下面的各个目录应该要放什么文件数据。例如/etc应该要放置设置文件/bin与/sbin则应该要放置可执行文件等等。针对/usr及/var这两个目录的子目录来定义。例如/var/log放置系统登录文件、/usr/share放置共享数据等等。USRUnix System Resource位置用途/usr系统安装的软件、共享的库。重要的子目录有/usr/bin: 用户命令。/usr/sbin:系统管理命令。 /usr/lib、/usr/lib64应用程序可调用的通用库文件。/usr/local: 本地自定义软件。/etc系统配置文件。/varvariable系统可变数据在系统启动之间保持永久性如数据库、 缓存目录、日志文件、打印机后台处理文档和网站内容。/run自本次系统启动以来运行中的进程的运行数据包括进程ID文件和锁定文件等等。此目录中的内容在重启时重新创建。此目录合并了早期版本的Linux 中的/var/run和/var/lock。/home主目录是普通用户存储其个人数据和配置文件的位置。 每个用户有自己的位置例如 /home/laoma。/root管理员root的主目录。/tmp供临时文件使用的全局可写空间。 10天内未访问、未更改或未修改的文件将自动从该目录中删除。 还有一个临时目录/var/tmp该目录中的文件如果在30天内未曾访问、更改或修改过 将被自动删除。/boot开机启动过程所需的文件。/devdevice包含特殊的设备文件供系统用于访问硬件。文件路径导航导航路径 cd 和 pwd常见路径说明绝对路径以根(/)开头的路径用于指定文件的确切位置。当前工作路径当前所处的位置。相对路径不是以根(/)开头的路径相对当前工作目录的路径。# 显示当前工作目录 [laomacentos7 ~]$ pwd /home/laoma # 切换路径 [laomacentos7 ~]$ cd /usr/bin [laomacentos7 bin]$ pwd /usr/bin [laomacentos7 bin]$ ls [ mpartition a2p mpls_dump ab mpris-proxy abrt-action-analyze-backtrace mpstat ... ... # 切换到其他目录 [laomacentos7 bin]$ cd /usr/share/doc # 切换上一次所在目录 [laomacentos7 doc]$ cd - /usr/bin [laomacentos7 bin]$ pwd /usr/bin # 切换到家目录 [laomacentos7 bin]$ cd # 或者 [laomacentos7 bin]$ cd ~ [laomacentos7 ~]$ pwd /home/laoma # 上一级目录父目录用..表示 [laomacentos7 ~]$ pwd /home/laoma [laomacentos7 ~]$ ls /home laoma [laomacentos7 ~]$ cd .. [laomacentos7 home]$ pwd /home # 当前目录用.表示通常不写. [laomacentos7 ~]$ file ./.bash_history ./.bash_history: UTF-8 Unicode text [laomacentos7 ~]$ file .bash_history .bash_history: UTF-8 Unicode text [laomacentos7 ~]$ cd /usr/share/doc/at-spi2-core-2.28.0/ # 使用相对路径切换到/usr/share/ [laomacentos7 at-spi2-core-2.28.0]$ cd ../../ [laomacentos7 share]$ pwd /usr/share [laomacentos7 ~]$ cd /usr/share/doc/at-spi2-core-2.28.0/ # 使用相对路径切换到 /etc/yum [laomacentos7 share]$ cd ../../../../etc/yum [laomacentos7 yum]$ pwd /etc/yum思考什么时候使用相对路径什么时候使用绝对路径答案一般情况是怎么方便怎么来。但是在变量定义文件的路径时候尽量使用绝对路径。ls 命令补充# -1数字1选项以单列格式展示 # 图形化界面并且用户登录系统后才会有这些文件。 [laomacentos7 ~]$ ls -1 公共 模板 视频 图片 文档 下载 音乐 桌面 # -R 选项递归查看子目录中文件 [laomacentos7 ~]$ ls /etc/yum fssnap.d pluginconf.d protected.d vars version-groups.conf [laomacentos7 ~]$ 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选项 长列表格式查看文件详细信息 [laomacentos7 ~]$ ls /etc/hosts /etc/hosts [laomacentos7 ~]$ ls -l /etc/hosts -rw-r--r--. 1 root root 197 11月 3 14:35 /etc/hosts # ls 命令默认按文件名先后顺序正向排序 [laomacentos7 ~]$ 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 选项反向排序 [laomacentos7 ~]$ 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 选项按时间排序 [laomacentos7 ~]$ 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 选项只查看对象本身 [laomacentos7 ~]$ ls -ld /etc/yum drwxr-xr-x. 6 root root 100 12月 13 10:42 /etc/yum [laomacentos7 ~]$ 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工具 [rootcentos7 ~]# yum install -y tree [laomacentos7 ~]$ 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 命令查看文件的元数据属性。[laomacentos7 ~]$ stat /etc/fstab 文件/etc/fstab 大小541 块8 IO 块4096 普通文件 设备fd00h/64768d Inode67160130 硬链接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 命令创建文件[laomacentos7 ~]$ 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, --referenceFILE use this files times instead of current time -t STAMP use [[CC]YY]MMDDhhmm[.ss] instead of current time --timeWORD 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 选项可接受不同的时间/日期格式。 # 更新现有文件时间戳包括访问时间更改时间和修改时间。 [laomacentos7 ~]$ ls -l zhang.txt -rw-rw-r--. 1 laoma laoma 0 12月 14 10:51 zhang.txt [laomacentos7 ~]$ touch zhang.txt [laomacentos7 ~]$ ls -l zhang.txt -rw-rw-r--. 1 laoma laoma 0 12月 14 13:36 zhang.txt [laomacentos7 ~]$ date 2022年 12月 14日 星期三 13:36:42 CST [laomacentos7 ~]$ stat zhang.txt 文件zhang.txt 大小0 块0 IO 块4096 普通空文件 设备fd02h/64770d Inode116 硬链接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 创建时间- # 如果文件不存在则创建文件 [laomacentos7 ~]$ ls zhang-f1.txt ls: 无法访问zhang-f1.txt: 没有那个文件或目录 [laomacentos7 ~]$ touch zhang-f1.txt [laomacentos7 ~]$ ls zhang-f1.txt zhang-f1.txt # 使用-c选项时候如果文件不存在则不会创建文件。 [laomacentos7 ~]$ ls zhang-f2.txt ls: 无法访问zhang-f2.txt: 没有那个文件或目录 [laomacentos7 ~]$ touch -c zhang-f2.txt [laomacentos7 ~]$ ls zhang-f2.txt ls: 无法访问zhang-f2.txt: 没有那个文件或目录操作单一来源多来源复制文件cp file1 file2cp file1 file2 file3 dir移动文件mv file1 file2mv file1 file2 file3 dir删除文件rm file1rm -f file1 file2 file3创建目录mkdir dirmkdir -p dir1/dir2/dir3复制目录cp -r dir1 dir2cp -r dir1 dir2 dir3 dir4移动目录mv dir1 dir2mv dir1 dir2 dir3 dir4删除目录rm -r dir1 或 rmdir dir1rm -rf dir1 dir2 dir3mkdir 命令创建文件夹# 创建目录 [laomacentos7 ~]$ mkdir lab [laomacentos7 ~]$ ls lab 公共 模板 视频 图片 文档 下载 音乐 桌面 [laomacentos7 ~]$ mkdir lab1 lab2 [laomacentos7 ~]$ ls lab lab1 lab2 公共 模板 视频 图片 文档 下载 音乐 桌面 # -p 选项级联创建目录 [laomacentos7 ~]$ mkdir dir1/dir2/dir3/dir4 mkdir: 无法创建目录dir1/dir2/dir3/dir4: 没有那个文件或目录 [laomacentos7 ~]$ mkdir -p dir1/dir2/dir3/dir4 [laomacentos7 ~]$ tree dir1 dir1 └── dir2 └── dir3 └── dir4 3 directories, 0 filescp 命令复制文件# 复制单个文件到目标位置 [laomacentos7 ~]$ cd lab [laomacentos7 lab]$ ls [laomacentos7 lab]$ cp /etc/hosts . [laomacentos7 lab]$ ls hosts # 复制过来并且重命名 [laomacentos7 lab]$ cp /etc/hosts ./hosts-1 [laomacentos7 lab]$ ls hosts hosts-1 # 当前目录下有同名称文件不会提示直接覆盖[laomacentos7 lab]$ cp /etc/hosts ./hosts-1 # 通过 -i 选项提示是否覆盖 [laomacentos7 lab]$ cp -i /etc/hosts ./hosts-1 cp是否覆盖./hosts-1 yes # 复制多个文件目标只能是目录 [laomacentos7 lab]$ cp /etc/passwd /etc/host.conf ./hosts-all cp: 目标./hosts-all 不是目录 [laomacentos7 lab]$ cp /etc/passwd /etc/host.conf . [laomacentos7 lab]$ ls host.conf hosts hosts-1 passwd复制目录# 使用 -r 选项复制目录 [laomacentos7 lab]$ cp /etc/yum . cp: 略过目录/etc/yum [laomacentos7 lab]$ cp -r /etc/yum . [laomacentos7 lab]$ ls host.conf hosts hosts-1 passwd yum # 复制过来并使用新的名称 [laomacentos7 lab]$ cp -r /etc/yum ./yum-1 [laomacentos7 lab]$ ls host.conf hosts hosts-1 passwd yum yum-1 # 当前目录下有同名称目录则将源目录放到相同目录下面而不是覆盖当前目录 [laomacentos7 lab]$ ls yum fssnap.d pluginconf.d protected.d vars version-groups.conf [laomacentos7 lab]$ cp -r /etc/yum ./yum [laomacentos7 lab]$ ls host.conf hosts hosts-1 passwd yum yum-1 [laomacentos7 lab]$ ls yum fssnap.d pluginconf.d protected.d vars version-groups.conf yum # 复制多个目录目标必须是已经存在的目录 [laomacentos7 lab]$ cp -r /etc /home ./mydir cp: 目标./mydir 不是目录 [laomacentos7 lab]$ cp -r /etc /home . ... ... cp: 无法打开/etc/sudo.conf 读取数据: 权限不够 cp: 无法访问/etc/sudoers.d: 权限不够 ... ... # 以上信息提示权限不够暂不用理会 [laomacentos7 lab]$ ls etc home host.conf hosts hosts-1 passwd yum yum-1 [laomacentos7 lab]$ ls etc homemv 命令# 我们使用前面创建的lab1目录 [laomacentos7 lab]$ ls -dl ../lab1 drwxrwxr-x. 2 laoma laoma 6 11月 4 10:32 ../lab1 # 移动单个文件 [laomacentos7 lab]$ ls etc home host.conf hosts hosts-1 passwd yum yum-1 [laomacentos7 lab]$ mv hosts-1 ../lab1 [laomacentos7 lab]$ ls etc home host.conf hosts passwd yum yum-1 [laomacentos7 lab]$ ls ../lab1 hosts-1 # 移动多个文件目标位置只能是目录 [laomacentos7 lab]$ mv passwd hosts /home/laoma/lab1/ [laomacentos7 lab]$ ls etc home host.conf yum yum-1 [laomacentos7 lab]$ ls ../lab1 hosts hosts-1 passwd # 重命名文件 [laomacentos7 lab]$ ls etc home host.conf yum yum-1 [laomacentos7 lab]$ mv host.conf host.conf-new [laomacentos7 lab]$ ls etc home host.conf-new yum yum-1 # 移动单个目录 [laomacentos7 lab]$ mv etc ../lab1 [laomacentos7 lab]$ ls home host.conf-new yum yum-1 [laomacentos7 lab]$ ls ../lab1 etc hosts hosts-1 passwd # 移动多个目录 [laomacentos7 lab]$ mv home/ yum/ yum-1/ ../lab1 [laomacentos7 lab]$ ls host.conf-new [laomacentos7 lab]$ ls ../lab1 etc home hosts hosts-1 passwd yum yum-1rmdir 命令# 删除空目录 [laomacentos7 lab]$ rmdir ../lab2 [laomacentos7 lab1]$ rmdir etc rmdir: 删除 etc 失败: 目录非空rm 命令[laomacentos7 lab]$ cd ../lab1 [laomacentos7 lab1]$ ls etc home hosts hosts-1 passwd yum yum-1 # 删除文件 [laomacentos7 lab1]$ rm hosts [laomacentos7 lab1]$ ls etc home hosts-1 passwd yum yum-1 [laomacentos7 lab1]$ rm hosts-1 passwd [laomacentos7 lab1]$ ls etc home yum yum-1 # 强制删除具有写保护的文件 [laomacentos7 lab1]$ cp /etc/pki/ca-trust/extracted/java/cacerts . [laomacentos7 lab1]$ rm cacerts rm是否删除有写保护的普通文件 cacertsyes [laomacentos7 lab1]$ ls cacerts ls: 无法访问cacerts: 没有那个文件或目录 # 使用-f选项直接删除 [laomacentos7 lab1]$ cp /etc/pki/ca-trust/extracted/java/cacerts . [laomacentos7 lab1]$ rm -f cacerts # -r选项递归删除目录 [laomacentos7 lab1]$ rm -r yum [laomacentos7 lab1]$ ls etc home yum-1 # 递归强制删除目录 -fr选项 [laomacentos7 lab1]$ rm -fr etc [laomacentos7 lab1]$ ls home yum-1 # 删除多个目录 [laomacentos7 lab1]$ rm -fr home/ yum-1/ [laomacentos7 lab1]$ ls # 清理实验环境 [laomacentos7 lab1]$ cd .. [laomacentos7 ~]$ ls dir1 lab lab1 lab2 公共 模板 视频 图片 文档 下载 音乐 桌面 [laomacentos7 ~]$ rm -fr lab lab1 lab2 dir1 [laomacentos7 ~]$ ls 公共 模板 视频 图片 文档 下载 音乐 桌面