五、文件目录类
# 显示当前工作目录的绝对路径 [root@WANFYONG ~]# pwd /root
# 显示当前目录下的所有文件和文件夹 # -a :显示当前目录所有的文件和目录,包括隐藏的。 # -l :以列表的方式显示信息 [root@WANFYONG ~]# ls anaconda-ks.cfg cd initial-setup-ks.cfg 公共 模板 视频 图片 文档 下载 音乐 桌面 自动化部署文件分析.docx [root@WANFYONG ~]# [root@WANFYONG ~]# ls -a . .bash_history .bashrc .config .esd_auth .local .tcshrc 公共 图片 音乐 .. .bash_logout .cache .cshrc .ICEauthority .mozilla .viminfo 模板 文档 桌面 anaconda-ks.cfg .bash_profile cd .dbus initial-setup-ks.cfg .pki .Xauthority 视频 下载 自动化部署文件分析.docx [root@WANFYONG ~]# [root@WANFYONG ~]# [root@WANFYONG ~]# ls -l 总用量 92 -rw-------. 1 root root 1893 5月 3 18:37 anaconda-ks.cfg drwxr-xr-x. 2 root root 4096 5月 6 20:46 cd -rw-r--r--. 1 root root 1941 5月 3 18:43 initial-setup-ks.cfg drwxr-xr-x. 2 root root 4096 5月 3 18:44 公共 drwxr-xr-x. 2 root root 4096 5月 3 18:44 模板 drwxr-xr-x. 2 root root 4096 5月 3 18:44 视频 drwxr-xr-x. 2 root root 4096 5月 3 18:44 图片 drwxr-xr-x. 2 root root 4096 5月 3 18:44 文档 drwxr-xr-x. 2 root root 4096 5月 3 18:44 下载 drwxr-xr-x. 2 root root 4096 5月 3 18:44 音乐 drwxr-xr-x. 2 root root 4096 5月 3 18:44 桌面 -rw-r--r--. 1 root root 46661 5月 5 18:32 自动化部署文件分析.docx
# 切换到指定目录 # cd ~ 或者 cd : 回到自己的家目录, 比如 你是 root , cd ~ 到 /root # cd .. 回到当前目录的上一级目录 [root@WANFYONG ~]# cd /home [root@WANFYONG /home]# [root@WANFYONG /home]# cd [root@WANFYONG ~]# [root@WANFYONG ~]# cd /home [root@WANFYONG /home]# [root@WANFYONG /home]# cd ~ [root@WANFYONG ~]# [root@WANFYONG ~]# [root@WANFYONG ~]# cd .. [root@WANFYONG /]# [root@WANFYONG /]# ls bin boot dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var [root@WANFYONG /]# cd ~
# 用于创建目录 # 常用选项 -p :创建多级目录 [root@WANFYONG /home]# ls wangwang wangyong wangyongyong [root@WANFYONG /home]# [root@WANFYONG /home]# mkdir /home/animals [root@WANFYONG /home]# [root@WANFYONG /home]# ls animals wangwang wangyong wangyongyong [root@WANFYONG /home]# [root@WANFYONG /home]# mkdir -p animals/dogs [root@WANFYONG /home]# [root@WANFYONG /home]# cd animals/ [root@WANFYONG /home/animals]# [root@WANFYONG /home/animals]# ls dogs
# 删除的空目录,如果目录下有内容时无法删除的。 # 如果需要删除非空目录,需要使用 rm -rf 要删除的目录 比如: rm -rf /home/animals [root@WANFYONG /home]# ls animals wangwang wangyong wangyongyong [root@WANFYONG /home]# rmdir animals/ rmdir: 删除 "animals/" 失败: 目录非空 [root@WANFYONG /home]# [root@WANFYONG /home]# rm -rf animals/ [root@WANFYONG /home]# [root@WANFYONG /home]# ls wangwang wangyong wangyongyong
# 创建空文件 [root@WANFYONG /home]# ls wangwang wangyong wangyongyong [root@WANFYONG /home]# [root@WANFYONG /home]# touch hello.txt [root@WANFYONG /home]# [root@WANFYONG /home]# ls hello.txt wangwang wangyong wangyongyong
# 拷贝文件到指定目录 cp [选项] source dest, dest不存在则复制失败 # 常用选项 -r :递归复制整个文件夹 [root@WANFYONG /home]# mkdir bbb [root@WANFYONG /home]# [root@WANFYONG /home]# ls bbb hello.txt wangwang wangyong wangyongyong [root@WANFYONG /home]# [root@WANFYONG /home]# cp hello.txt bbb [root@WANFYONG /home]# [root@WANFYONG /home]# cd bbb/ [root@WANFYONG /home/bbb]# ls hello.txt [root@WANFYONG /home/bbb]# [root@WANFYONG /home/bbb]# cd /home [root@WANFYONG /home]# [root@WANFYONG /home]# cp -r bbb /opt [root@WANFYONG /home]# [root@WANFYONG /home]# cd /opt [root@WANFYONG /opt]# [root@WANFYONG /opt]# ls bbb rh VMwareTools-10.3.22-15902021.tar.gz vmware-tools-distrib [root@WANFYONG /opt]# [root@WANFYONG /opt]# cd /home [root@WANFYONG /home]# [root@WANFYONG /home]# cp hello.txt /ccc [root@WANFYONG /home]# ls bbb hello.txt wangwang wangyong wangyongyong
# 移除文件或目录 # 常用选项 -r :递归删除整个文件夹 -f : 强制删除不提示 [root@WANFYONG /home]# ls bbb hello.txt wangwang wangyong wangyongyong [root@WANFYONG /home]# [root@WANFYONG /home]# rm hello.txt rm:是否删除普通空文件 "hello.txt"?y [root@WANFYONG /home]# [root@WANFYONG /home]# ls bbb wangwang wangyong wangyongyong [root@WANFYONG /home]# [root@WANFYONG /home]# rm -rf bbb [root@WANFYONG /home]# [root@WANFYONG /home]# ls wangwang wangyong wangyongyong
# 移动文件与目录或重命名 # mv oldNameFile newNameFile (功能描述:重命名) # mv /temp/movefile /targetFolder (功能描述:移动文件) [root@WANFYONG /home]# ls cat.txt wangwang wangyong wangyongyong [root@WANFYONG /home]# [root@WANFYONG /home]# mv cat.txt pig.txt [root@WANFYONG /home]# [root@WANFYONG /home]# mv pig.txt /root [root@WANFYONG /home]# [root@WANFYONG /home]# ls wangwang wangyong wangyongyong [root@WANFYONG /home]# [root@WANFYONG /home]# cd /root [root@WANFYONG ~]# [root@WANFYONG ~]# ls anaconda-ks.cfg cd initial-setup-ks.cfg pig.txt 公共 模板 视频 图片 文档 下载 音乐 桌面 自动化部署文件分析.docx [root@WANFYONG ~]# [root@WANFYONG ~]# cd /opt [root@WANFYONG /opt]# [root@WANFYONG /opt]# ls bbb rh VMwareTools-10.3.22-15902021.tar.gz vmware-tools-distrib [root@WANFYONG /opt]# [root@WANFYONG /opt]# mv bbb /home [root@WANFYONG /opt]# [root@WANFYONG /opt]# cd /home [root@WANFYONG /home]# [root@WANFYONG /home]# ls bbb wangwang wangyong wangyongyong
# 查看文件内容 # 常用选项 -n :显示行号 # cat 只能浏览文件,而不能修改文件,为了浏览方便,一般会带上 管道命令 | more [root@WANFYONG /home]# ls bbb hello.c wangwang wangyong wangyongyong [root@WANFYONG /home]# [root@WANFYONG /home]# cat -n hello.c 1 #include<stdio.h> 2 3 int main() 4 { 5 printf("Hello World\n"); 6 return 0; 7 } [root@WANFYONG /home]# cat -n /etc/profile | more
# more 指令是一个基于 VI 编辑器的文本过滤器,它以全屏幕的方式按页显示文本文件的内容。more 指令中内置了若 干快捷键(交互的指令) # 基本语法: more 要查看的文件
[root@WANFYONG /home]# more /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. pathmunge () { case ":${PATH}:" in *:"$1":*) ;; *) if [ "$2" = "after" ] ; then PATH=$PATH:$1 else PATH=$1:$PATH fi esac } if [ -x /usr/bin/id ]; then if [ -z "$EUID" ]; then # ksh workaround EUID=`/usr/bin/id -u` UID=`/usr/bin/id -ru` fi --More--(41%)
# less 指令用来分屏查看文件内容,它的功能与 more 指令类似,但是比 more 指令更加强大,支持各种显示终端。less 指令在显示文件内容时,并不是一次将整个文件加载之后才显示,而是根据显示需要加载内容,对于显示大型文件具有 较高的效率。 # 基本语法: less 要查看的文件 less /etc/profile
# 输出内容到控制台 [root@WANFYONG /home]# echo hello world hello world [root@WANFYONG /home]# [root@WANFYONG /home]# echo "hello world" hello world [root@WANFYONG /home]# [root@WANFYONG /home]# echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin [root@WANFYONG /home]# [root@WANFYONG /home]# echo $HOSTNAME WANFYONG [root@WANFYONG /home]#
# head 用于显示文件的开头部分内容,默认情况下 head 指令显示文件的前 10 行内容 # 基本语法 head 文件 (功能描述:查看文件头 10 行内容) # head -n 5 文件 (功能描述:查看文件头 5 行内容,5 可以是任意行数) [root@WANFYONG /home]# head hello.c #include<stdio.h> int main() { printf("Hello World\n"); return 0; } [root@WANFYONG /home]# [root@WANFYONG /home]# head -n 5 /etc/profile # /etc/profile # System wide environment and startup programs, for login setup # Functions and aliases go in /etc/bashrc [root@WANFYONG /home]# [root@WANFYONG /home]# head -n 3 hello.c #include<stdio.h> int main() [root@WANFYONG /home]#
# tail 用于输出文件中尾部的内容,默认情况下 tail 指令显示文件的前 10 行内容。 # 1) tail 文件 (功能描述:查看文件尾 10 行内容) # 2) tail -n 5 文件 (功能描述:查看文件尾 5 行内容,5 可以是任意行数) # 3) tail -f 文件 (功能描述:实时追踪该文档的所有更新) [root@WANFYONG /home]# [root@WANFYONG /home]# tail hello.c #include<stdio.h> int main() { printf("Hello World\n"); return 0; } [root@WANFYONG /home]# [root@WANFYONG /home]# tail /etc/profile if [ "${-#*i}" != "$-" ]; then . "$i" else . "$i" >/dev/null fi fi done unset i unset -f pathmunge [root@WANFYONG /home]# [root@WANFYONG /home]# tail -n 4 hello.c { printf("Hello World\n"); return 0; } [root@WANFYONG /home]#
# > 输出重定向和 >> 追加 # 1) ls -l >文件 (功能描述:列表的内容写入文件 a.txt 中(覆盖写)) # 2) ls -al >>文件 (功能描述:列表的内容追加到文件 aa.txt 的末尾) # 3) cat 文件1 > 文件2 (功能描述:将文件 1 的内容覆盖到文件 2) # 4) echo "内容">> 文件 (追加) [root@WANFYONG /home]# ls bbb hello.c mydata.txt wangwang wangyong wangyongyong [root@WANFYONG /home]# [root@WANFYONG /home]# ls -l > info.txt [root@WANFYONG /home]# [root@WANFYONG /home]# ls bbb hello.c info.txt mydata.txt wangwang wangyong wangyongyong [root@WANFYONG /home]# [root@WANFYONG /home]# cal >> mycal [root@WANFYONG /home]# [root@WANFYONG /home]# ls bbb hello.c info.txt mycal mydata.txt wangwang wangyong wangyongyong [root@WANFYONG /home]# [root@WANFYONG /home]# cat info.txt 总用量 24 drwxr-xr-x. 2 root root 4096 5月 7 11:11 bbb -rw-r--r--. 1 root root 77 5月 7 11:24 hello.c -rw-r--r--. 1 root root 0 5月 7 14:12 info.txt -rw-r--r--. 1 root root 12 5月 7 12:05 mydata.txt drwx------. 3 wangwang shanghai 4096 5月 6 16:49 wangwang drwx------. 5 wangyong shanghai 4096 5月 6 16:18 wangyong drwx------. 3 wangwang shanghai 4096 5月 6 16:19 wangyongyong [root@WANFYONG /home]# [root@WANFYONG /home]# cat mycal 五月 2022 日 一 二 三 四 五 六 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@WANFYONG /home]#
# 软链接也称为符号链接,类似于 windows 里的快捷方式,主要存放了链接其他文件的路径 # ln -s [原文件或目录] [软链接名] (功能描述:给原文件创建一个软链接) # 当我们使用 pwd 指令查看目录时,仍然看到的是软链接所在目录。 [root@WANFYONG /home]# ls bbb hello.c info.txt mycal mydata.txt wangwang wangyong wangyongyong [root@WANFYONG /home]# [root@WANFYONG /home]# cd bbb [root@WANFYONG /home/bbb]# [root@WANFYONG /home/bbb]# ls hello.c [root@WANFYONG /home/bbb]# [root@WANFYONG /home/bbb]# cd .. [root@WANFYONG /home]# [root@WANFYONG /home]# ln -s bbb mybbb [root@WANFYONG /home]# [root@WANFYONG /home]# ls bbb hello.c info.txt mybbb mycal mydata.txt wangwang wangyong wangyongyong [root@WANFYONG /home]# [root@WANFYONG /home]# cd mybbb [root@WANFYONG /home/mybbb]# [root@WANFYONG /home/mybbb]# ls hello.c [root@WANFYONG /home/mybbb]# [root@WANFYONG /home/mybbb]# pwd /home/mybbb [root@WANFYONG /home/mybbb]#
# 查看已经执行过历史命令 # history 显示所有的历史命令 # history 10 显示最近使用过的 10 个指令。 # !5 执行历史编号为 5 的指令 [root@WANFYONG /home]# history 10 358 pwd 359 cat mycal 360 cd bbb 361 ls 362 cd /home 363 cd wangyong 364 ls 365 cd /home 366 clear 367 history 10 [root@WANFYONG /home]# [root@WANFYONG /home]# !364 ls bbb hello.c info.txt mycal mydata.txt wangwang wangyong wangyongyong [root@WANFYONG /home]# [root@WANFYONG /home]# history