目录
1.准备工作
打开终端窗口,依次输入以下命令:
root@cg:~/Desktop# cd ~/base root@cg:~/base# mkdir dirandfile root@cg:~/base# cd dirandfile root@cg:~/base/dirandfile#
在当前目录下新建文件夹dirandfile,作为本实验的工作目录。
2.mkdir创建目录
语法:
mkdir [-mp] 目录名称
选项和参数:
-
-m :配置文件的权限
-
-p :递归地创建目标目录及全部的上层目录
示例:
root@cg:~/base/dirandfile# mkdir test root@cg:~/base/dirandfile# mkdir test1/test2/test3/test4 mkdir: cannot create directory 'test1/test2/test3/test4': No such file or directory root@cg:~/base/dirandfile# mkdir -p test1/test2/test3/test4 root@cg:~/base/dirandfile#
可以看到有了-p选项之后,可以递归地创建出所需要目录的上层目录。
root@cg:~/base/dirandfile# ls -l total 8 drwxr-xr-x 2 root root 4096 Feb 16 14:42 test drwxr-xr-x 3 root root 4096 Feb 16 14:43 test1 root@cg:~/base/dirandfile# mkdir -m 711 test2 root@cg:~/base/dirandfile# ls -l total 12 drwxr-xr-x 2 root root 4096 Feb 16 14:42 test drwxr-xr-x 3 root root 4096 Feb 16 14:43 test1 drwx--x--x 2 root root 4096 Feb 16 14:44 test2 root@cg:~/base/dirandfile#
如果没有设置-m选项,那么该目录将被设置为默认权限,在上例中该目录通过-m 711设置为了drwxr-sr-x权限。
3.ls列出目录
语法:
ls [-aAdfFhilnrRSt] 目录名称
ls [--color={never,auto,always}] 目录名称
ls [--full-time] 目录名称
选项和参数:
-
-a :全部的文件,包括隐藏文件
-
-d :仅列出目录
-
-l :列出详细的文件属性
示例:
root@cg:~/base/dirandfile# ls -l total 12 drwxr-xr-x 2 root root 4096 Feb 16 14:42 test drwxr-xr-x 3 root root 4096 Feb 16 14:43 test1 drwx--x--x 2 root root 4096 Feb 16 14:44 test2 root@cg:~/base/dirandfile#
4.pwd显示目前所在目录
pwd 是 Print Working Directory 的缩写,也就是显示目前所在目录的命令。
语法:
pwd
示例:
显示出当前目录的绝对路径:
root@cg:~/base/dirandfile# pwd /headless/base/dirandfile root@cg:~/base/dirandfile#
5.cd切换目录
cd是Change Directory的缩写,这是用来变换工作目录的命令。
语法:
cd [相对路径或绝对路径]
示例:
使用绝对路径切换到 test2 目录
root@cg:~/base/dirandfile# cd /headless/base/dirandfile/test2
..表示回到当前目录的上一级目录
root@cg:~/base/dirandfile/test2# cd ..
使用相对路径切换到 test2 目录
root@cg:~/base/dirandfile# cd ./test2
~表示自己的家目录,cd ~表示回到家目录,亦即是 /headless 这个目录
root@cg:~/base/dirandfile# cd /headless/base/dirandfile/test2 root@cg:~/base/dirandfile/test2# cd .. root@cg:~/base/dirandfile# cd ./test2 root@cg:~/base/dirandfile/test2# cd ~ root@cg:~# pwd /headless
-表示切换到当前目录之前的目录
root@cg:~# cd - /headless/base/dirandfile/test2 root@cg:~/base/dirandfile/test2# pwd /headless/base/dirandfile/test2 root@cg:~/base/dirandfile/test2#
6.rmdir删除空目录
语法:
rmdir [-p] 目录名称
选项与参数:
-p :连同上一级『空的』目录也一起删除
示例:
可直接删除空目录
root@cg:~/base/dirandfile# ls -l total 12 drwxr-xr-x 2 root root 4096 Feb 16 14:51 test drwxr-xr-x 3 root root 4096 Feb 16 14:56 test1 drwx--x--x 2 root root 4096 Feb 16 14:44 test2 root@cg:~/base/dirandfile# rmdir test root@cg:~/base/dirandfile# ls -l total 8 drwxr-xr-x 3 root root 4096 Feb 16 14:56 test1 drwx--x--x 2 root root 4096 Feb 16 14:44 test2 root@cg:~/base/dirandfile#
非空目录无法删除
root@cg:~/base/dirandfile# rmdir test1 rmdir: failed to remove 'test1': Directory not empty
递归地删除test1/test2/test3/test4
root@cg:~/base/dirandfile# rmdir -p test1/test2/test3/test4 root@cg:~/base/dirandfile# ls -l total 4 drwx--x--x 2 root root 4096 Feb 16 14:44 test2 root@cg:~/base/dirandfile#
7.touch创建新文件
一是创建新的空文件
二是修改已存在文件的时间戳
语法:
touch [-acdfmrt] 文件名称
选项与参数:
-
-a 或--time=atime或--time=access或--time=use 只更改存取时间。
-
-c 或--no-create 不建立任何文档。
-
-d 使用指定的日期时间,而非现在的时间。
-
-f 此参数将忽略不予处理,仅负责解决BSD版本touch指令的兼容性问题。
-
-m 或--time=mtime或--time=modify 只更改变动时间。
-
-r 把指定文档或目录的日期时间,统统设成和参考文档或目录的日期时间相同。
-
-t 使用指定的日期时间,而非现在的时间。
示例:
创建新文件
root@cg:~/base/dirandfile# touch file1.txt root@cg:~/base/dirandfile# ls -l total 4 -rw-r--r-- 1 root root 0 Feb 16 15:00 file1.txt drwx--x--x 2 root root 4096 Feb 16 14:44 test2 root@cg:~/base/dirandfile# touch file2.txt root@cg:~/base/dirandfile# ls -l total 4 -rw-r--r-- 1 root root 0 Feb 16 15:00 file1.txt -rw-r--r-- 1 root root 0 Feb 16 15:04 file2.txt drwx--x--x 2 root root 4096 Feb 16 14:44 test2 root@cg:~/base/dirandfile#
修改文件的时间戳
root@cg:~/base/dirandfile# ls -l total 4 -rw-r--r-- 1 root root 0 Feb 16 15:00 file1.txt -rw-r--r-- 1 root root 0 Feb 16 15:04 file2.txt drwx--x--x 2 root root 4096 Feb 16 14:44 test2 root@cg:~/base/dirandfile# touch -t 202002161300 file1.txt root@cg:~/base/dirandfile# ls -l total 4 -rw-r--r-- 1 root root 0 Feb 16 13:00 file1.txt -rw-r--r-- 1 root root 0 Feb 16 15:04 file2.txt drwx--x--x 2 root root 4096 Feb 16 14:44 test2 root@cg:~/base/dirandfile#
8.cp 复制文件或目录
cp 即拷贝文件和目录
语法:
cp [-adfilprsu] source destination
cp [options] source1 source2 source3 .... directory
选项与参数:
-
-a:相当于同时使用-pdr
-
-d:若源文件为链接文件,则复制链接文件属性而非文件本身
-
-f:强制复制文件或目录,无论目标文件或目录是否已经存在
-
-i:若目标文件已存在,覆盖时会先询问
-
-l:复制为硬链接
-
-p:连同文件的属性一起复制过去
-
-r:递归地复制各层目录
-
-s:复制为软链接
-
-u:若目标文件比源文件旧才升级目标文件
示例:
root@cg:~/base/dirandfile# cp file1.txt test2/ root@cg:~/base/dirandfile# cp -i file1.txt test2/ cp: overwrite 'test2/file1.txt'? n root@cg:~/base/dirandfile#
9.rm 移除文件或目录
语法:
rm [-fir] 文件或目录
选项与参数:
-
-f :强制执行删除操作,不发出警告;
-
-i :删除前会请求确认
-
-r :递归删除,用于删除多层目录
示例:
root@cg:~/base/dirandfile# rm -i file1.txt rm: remove regular empty file 'file1.txt'? y root@cg:~/base/dirandfile# ls -l total 4 -rw-r--r-- 1 root root 0 Feb 16 15:04 file2.txt drwx--x--x 2 root root 4096 Feb 16 15:06 test2 root@cg:~/base/dirandfile#
10.mv 移动文件或目录,或修改名称
语法:
mv [-fiu] 源 目标
mv [options] 源1 源2 源3 .... 目标
选项与参数:
-
-f :force 强制的意思,如果目标文件已经存在,不会询问而直接覆盖;
-
-i :若目标文件已存在,覆盖时会先询问
-
-u :若目标文件已经存在,且 source 比较新,才会升级 (update)
示例:
root@cg:~/base/dirandfile# mkdir test root@cg:~/base/dirandfile# mv file2.txt test root@cg:~/base/dirandfile# ls -l total 8 drwxr-xr-x 2 root root 4096 Feb 16 15:09 test drwx--x--x 2 root root 4096 Feb 16 15:06 test2 root@cg:~/base/dirandfile# mv test test1 root@cg:~/base/dirandfile# ls -l total 8 drwxr-xr-x 2 root root 4096 Feb 16 15:09 test1 drwx--x--x 2 root root 4096 Feb 16 15:06 test2 root@cg:~/base/dirandfile# mv test2 test1 root@cg:~/base/dirandfile# ls -l total 4 drwxr-xr-x 3 root root 4096 Feb 16 15:10 test1 root@cg:~/base/dirandfile# ls -l test1 total 4 -rw-r--r-- 1 root root 0 Feb 16 15:04 file2.txt drwx--x--x 2 root root 4096 Feb 16 15:06 test2 root@cg:~/base/dirandfile#