目录
ls
cp
mkdir
mv
pwd
cd
ls
ls 来源于英文单词中的list,其功能是列举出指定目录下的文件名称及其属性。经常与cd和pwd命令搭配使用。
# 语法格式 ls [参数] 文件
常用参数:
| -a | 显示所有文件及目录,包括隐藏文件 |
| -l | long list 使用长格式列出文件及目录的详细信息 |
| -r | reverse list 将文件以相反次序显示(默认英文字母次序) |
| -t | time list 根据最后的修改时间排序 |
| -A | 同-a,但不列出“.”和“..” |
| -S | size list 根据文件大小排序 |
| -R | recurrence list 递归列出所有子目录 |
| -d | dir list 查看目录信息而不是文件信息 |
| -X | 按文件扩展名排序 |
| -m | 水平列出文件,以逗号间隔 |
案例演示
# ls shell study % ls shell-study.docx test.txt testDir shell01.sh test02.sh # -a shell study % ls -a . .DS_Store shell01.sh test02.sh .. shell-study.docx test.txt testDir # -A shell study % ls -A shell-study.docx shell01.sh test.txt test02.sh testDir # -l 数据中的第四列为文件的大小,以字节为单位 shell study % ls -l total 7144 -rw-r--r--@ 1 root staff 3642186 12 6 15:08 shell-study.docx -rw-r--r--@ 1 root staff 84 5 3 09:13 shell01.sh -rw-r--r--@ 1 root staff 28 5 14 09:10 test.txt -rw-r--r-- 1 root staff 605 5 8 15:22 test02.sh drwxr-xr-x 7 root staff 224 5 14 10:42 testDir # -S shell study % ls -S shell-study.docx testDir test.txt test02.sh shell01.sh # -r shell study % ls -r testDir test.txt shell-study.docx test02.sh shell01.sh # -R shell study % ls -R shell-study.docx test.txt testDir shell01.sh test02.sh ./testDir: xyy_1.jpg xyy_2.jpg xyy_3.jpg xyy_4.jpg xyy_5.jpg # -m shell study % ls -m shell-study.docx, shell01.sh, test.txt, test02.sh, testDir
cp
cp 来源于英文单词中的copy,用于将一个或多个文件或目录复制到指定位置,常用语文件备份。
# 语法格式 cp [参数] 源文件 目标文件
常用参数:
| -f | force 若目标问价已存在,则直接覆盖原文件 |
| -i | inquire 若目标文件已存在,则询问是否覆盖 |
| -r | recurrence 递归复制文件和目录 |
案例演示
# -f cp -f test02.sh test03.sh # -i shell study % cp -i test02.sh test03.sh overwrite test03.sh? (y/n [n]) y # -r cp -r testDir testDir01
mkdir
mkdir 来源于英文词组“make directories”的缩写,用于创建目录文件。
# 语法格式 mkdir [参数] 目录
常用参数:
| -p | 递归创建多级目录 |
| -m | 建立目录的同时设置目录的权限 |
本小节实验目录如下:
案例演示
# -p shell study % mkdir dir01/dir02/dir03 mkdir: A/B: No such file or directory shell study % mkdir -p dir01/dir02/dir03 # -m shell study % mkdir -m 700 dir04 shell study % ls -l drwx------ 2 root staff 64 5 14 14:36 dir04
rmdir
rmdir 来源于英文词组“remove directories”,用于删除目录文件。
# 语法格式 rmdir [参数] 目录
常用参数:
| -p | parents 删除当前目录后,若父目录为空,则删除父目录 |
案例演示
# 当目录不为空时,无法删除当前目录 shell study % rmdir dir01 rmdir: dir01: Directory not empty # 正常删除,只删掉了dir03 rmdir dir01/dir02/dir03 # -p shell study % rmdir -p dir01/dir02
「在删除目录时遇到的问题:」
shell study % rmdir -p dir01/dir02 rmdir: dir01: Directory not empty
问题:当用-p命令删除dir02时,系统提示dir01不为空,但查看目录发现dir01目录下已经没有内容了,这是为什么?
解决办法:可以使用 ls -A 目录 查看目录中是否含有隐藏文件。
最终结果:发现在本小节中的dir01中存在一个隐藏文件,因此dir01无法被删除,将隐藏文件删除后,即可操作rmdir命令啦~
mv
mv 来源于英文单词move的缩写,用于对文件进行剪切和重命名。
# 语法格式 mv [参数] 源文件 目标文件
常用参数:
| -i | inquire 若存在同文件名,向用户询问是否覆盖 |
| -f | force 覆盖已有文件,不进行任何提示 |
本小节实验目录如下:
案例演示
# -i shell study % mv -i A B overwrite B/A? (y/n [n]) y # -f shell study % mv -f A B
pwd
pwd 来源于英文词组“print working directory”的缩写,用于显示当前工作的目录路径(绝对路径)
cd
cd 来源于英文词组“change directory”的缩写,用于切换目录



