文章目录
1.linux文件和目录基本命令
命令 | 描述 |
---|---|
cd | 切换工作目录 |
ls | 显示目录内容信息 |
pwd | 显示当前绝对路径的目录 |
mkdir | 创建目录 |
touch | 创建文件 |
cp | 复制文件和目录 |
mv | 移动文件和目录 |
rm | 删除文件和目录 |
tree | 树状列出目录和文件 |
rmdir | 删除空目录 |
dirs | 显示目录堆叠记录 |
pushd | 添加目录到堆叠中 |
popd | 删除目录到堆叠中 |
1.1.cd命令
解释
命令用于改变当前工作目录的命令,切换到指定的路径
选项
-P 目标目录是软链接目录,切换软链接指向的原始目录
-L 目标目录是软链接目录,切换目标目录,cd等于cd -L
- 使用"-"选项时,返回进入此目录之前所在的目录
用法
cd #进入root目录
cd ~ #进入root目录,~等于/root
cd - #返回进入此目录之前所在的目录
cd .. #返回上级目录
cd ../.. #返回上两级目录
cd !$ #把上个命令的参数作为cd参数使用
cd $变量 #进入变量所指路径的目录
1.2.ls命令
解释
命令用于显示指定工作目录下之内容
选项
-a:显示所有文件及目录,以“.”开头的隐藏文件也会列出
-d:只列出目录不递归列出目录内的文件
-l:以长格式显示文件和目录信息,包括权限、所有者、大小、创建时间等
-r:倒序显示文件和目录
-t:将按照修改时间排序,最新的文件在最前面
-m:用“,”号区隔每个文件和目录的名称
-n:以用户识别码和群组识别码替代其名称
-s:显示文件和目录的大小,以区块为单位
-A:同-a,但不列出".“目录及”…"父目录
-R:递归显示目录中的所有文件和子目录
-i:显示文件索引节点号(inode)。一个索引节点代表一个文件
-F:在每个输出项后追加文件的类型标识符,具体含义如下
“”表示具有可执行权限的普通文件
“/”表示目录
“@”表示符号链接
“|”表示命令管道FIFO
“=”表示sockets套接字
文件为普通文件时,不输出任何标识符
–file-type:与“-F”选项的功能相同,但是不显示“”
–full-time:列出完整的日期与时间
–color=auto:使用不同的颜色高亮显示不同类型的
用法
ls -l # 以长格式显示当前目录中的文件和目录
ls -a # 显示当前目录中的所有文件和目录,包括隐藏文件
ls -lh # 以人类可读的方式显示当前目录中的文件和目录大小
ls -lt # 按照修改时间排序显示当前目录中的文件和目录
ls -lR # 递归显示当前目录中的所有文件和子目录
实例
[root@node1 ~]# ls /root -a
. .bash_history .bashrc dashhboard metrics .pydistutils.cfg .ssh
.. .bash_logout components.yaml flannel .pip .python_history .tcshrc
.ansible .bash_profile .cshrc .lesshst .pki .rnd .viminfo
在使用 ls -l 命令时,第一列的字符表示文件或目录的类型和权限,其中第一个字符表示文件类型,如下
- 表示普通文件
d 表示目录
l 表示符号链接
c 表示字符设备文件
b 表示块设备文件
s 表示套接字文件
p 表示管道文件
在使用 ls -l 命令时,第一列的其余 9 个字符表示文件或目录的访问权限,分别对应三个字符一组的 rwx 权限,如下
r 表示读取权限
w 表示写入权限
x 表示执行权限
- 表示没有对应权限
[root@node1 ~]# ls /root -l
total 16
-rw-r--r-- 1 root root 3335 Nov 29 17:24 components.yaml
drwxr-xr-x 2 root root 4096 Dec 1 17:13 dashhboard
drwxr-xr-x 12 root root 4096 Nov 27 11:29 flannel
drwxr-xr-x 2 root root 4096 Dec 1 09:36 metrics
[root@node1 ~]# ls /root -lh
total 16K
-rw-r--r-- 1 root root 3.3K Nov 29 17:24 components.yaml
drwxr-xr-x 2 root root 4.0K Dec 1 17:13 dashhboard
drwxr-xr-x 12 root root 4.0K Nov 27 11:29 flannel
drwxr-xr-x 2 root root 4.0K Dec 1 09:36 metrics
[root@node1 ~]# ls -lt
total 16
drwxr-xr-x 2 root root 4096 Dec 1 17:13 dashhboard
drwxr-xr-x 2 root root 4096 Dec 1 09:36 metrics
-rw-r--r-- 1 root root 3335 Nov 29 17:24 components.yaml
drwxr-xr-x 12 root root 4096 Nov 27 11:29 flannel
[root@node1 ~]# ls /root/metrics/ -lR
/root/metrics/:
total 77912
-rw-r--r-- 1 root root 38580224 Nov 29 18:53 addon.tar.gz
-rw-r--r-- 1 root root 41199616 Nov 29 18:52 metrics-server-amd64-0-3-6.tar.gz
1.3.pwd命令
解释
命令用于显示工作目录
选项
-P:目标目录是软链接目录,显示软链接指向的原始目录
-L:目标目录是软链接目录,显示目标目录,pwd等于pwd -L
用法
pwd #显示当前绝对路径目录
pwd -P #目录是软链接,显示软链接指向的绝对路径目录
pwd -L #目录是软链接,显示当前绝对路径目录
实例
[root@node1 /]# ll
total 60
lrwxrwxrwx. 1 root root 7 Oct 19 11:29 bin -> usr/bin
[root@node1 /]# cd /bin && pwd
/bin
[root@node1 bin]# pwd -P
/usr/bin
[root@node1 bin]# pwd -L
/bin
1.4.mkdir命令
解释
命令用于创建目录
选项
-m<权限>或–mode<权限>:创建目录的同时设置目录的权限
-p或–parents:递归创建目录
用法
mkdir a-dir #创建a-dir目录
mkdir -p a-dir/b-dir #创建a-dir目录并在目录下创建b-dir目录
mkdir -m 777 a-dir #创建a-dir目录并赋予777权限
实例
[root@node1 test]# mkdir a-dir && ll
total 4
drwxr-xr-x 2 root root 4096 Dec 6 17:40 a-dir
[root@node1 test]# mkdir -p a-dir/b-dir && ll -R
.:
total 4
drwxr-xr-x 3 root root 4096 Dec 6 17:41 a-dir
./a-dir:
total 4
drwxr-xr-x 2 root root 4096 Dec 6 17:41 b-dir
./a-dir/b-dir:
total 0
[root@node1 test]# mkdir -m 777 a-dir && ll
total 4
drwxrwxrwx 2 root root 4096 Dec 6 17:42 a-dir
1.5.touch命令
解释
命令用于修改文件或者目录的时间属性,包括存取时间和更改时间。若文件不存在,系统会建立一个新的文件
选项
-a:更改文件的最近访问时间同步系统时间
-m:更改文件的最近更改时间同步系统时间
-c 或–no-create:不建立任何文件
-r <目录或文件>:把指定文件或目录的日期时间,拷贝时间到新文件
-d <date日期格式>:更改文件的最近访问和最近更改,时间格式与date指令相同,可以使用各种不同的日期格式
-t <YYMMDDHHMM.SS>:更改文件的最近访问和最近更改,时间格式与date指令相同
–help:列出指令格式
–version:列出版本讯息
stat 文件名:查看文件创建详细信息
文件: ‘file1’
大小: 8 块: 8 IO 块: 4096 普通文件
设备: fd01h/64769d Inode: 1180209 硬链接: 1
权限: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
最近访问: 2023-12-05 00:00:00.000000000 +0800
最近更改: 2023-12-05 00:00:00.000000000 +0800
最近改动: 2023-12-06 18:19:07.842393220 +0800
创建时间: -
用法
touch a-file #创建a-file文件
touch -m a-file #更改a-file文件的最近更改和最近改动时间同步系统时间
touch -t 2006151230.30 a-file #更改a-file文件
实例
[root@node1 test]# touch a-file && ll
total 0
-rw-r--r-- 1 root root 0 Dec 6 18:20 a-file
[root@node1 test]# stat a-file
File: ‘a-file’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd01h/64769d Inode: 1180205 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2023-12-06 18:20:10.437928062 +0800
Modify: 2023-12-06 18:20:10.437928062 +0800
Change: 2023-12-06 18:20:10.437928062 +0800
Birth: -
[root@node1 test]# touch -m a-file && stat a-file
File: ‘a-file’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd01h/64769d Inode: 1180205 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2023-12-06 18:20:10.437928062 +0800
Modify: 2023-12-06 18:29:00.510395144 +0800
Change: 2023-12-06 18:29:00.510395144 +0800
Birth: -
[root@node1 test]# touch -m a-file && stat a-file
File: ‘a-file’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd01h/64769d Inode: 1180205 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2023-12-06 18:20:10.437928062 +0800
Modify: 2023-12-06 18:29:00.510395144 +0800
Change: 2023-12-06 18:29:00.510395144 +0800
Birth: -
[root@node1 test]# touch -t 2006151230.30 a-file && stat a-file
File: ‘a-file’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd01h/64769d Inode: 1180205 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2020-06-15 12:30:30.000000000 +0800
Modify: 2020-06-15 12:30:30.000000000 +0800
Change: 2023-12-06 18:35:11.601433591 +0800
Birth: -
1.6.cp命令
解释
命令主要用于复制文件或目录
选项
-a:此参数的效果和同时指定"-dpR"参数相同
-d:当复制符号连接时,把目标文件或目录也建立为符号连接,并指向与源文件或目录连接的原始文件或目录
-p:保留源文件或目录的属性
-R/r:递归处理,将指定目录下的所有文件与子目录一并处理
-f:强行复制文件或目录,不论目标文件或目录是否已存在
-l:对源文件建立硬链接,而非复制文件
-s:对源文件建立符号链接,而非复制文件
-u:使用这项参数后只会在源文件的更改时间较目标文件更新时或是名称相互对应的目标文件并不存在时,才复制文件
-S <备份字尾字符串>:在备份文件时,用指定的后缀代替文件的默认后缀
-b:覆盖已存在的文件目标前将目标文件备份
-i:覆盖既有文件之前先询问用户
-n:不覆盖现有文件且不会提示覆盖
-v:详细显示命令执行的操作
用法
cp -a dir/ dir-1/ #递归复制目录并改名,且属性、时间和源文件相同
cp -s /root/test/dir/cfile dir-1/file #对源文件创建软链接
cp -b -S _bak dir/cfile dir-1/cfile #复制文件并对源文件进行备份
实例
[root@node1 test]# ll dir/
total 16
drwxr-xr-x 2 root root 4096 Dec 7 10:30 adir
drwxr-xr-x 2 root root 4096 Dec 7 10:30 bdir
-rw-r--r-- 1 root root 12 Dec 7 11:08 cfile
-rw-r--r-- 1 root root 4 Dec 7 10:55 dfile
lrwxrwxrwx 1 root root 14 Dec 7 10:33 dir -> /root/test/dir
drwxr-xr-x 4 root root 4096 Dec 7 10:48 dir
[root@node1 test]# cp -a dir/ dir-1/
[root@node1 test]# ll dir-1
total 16
drwxr-xr-x 2 root root 4096 Dec 7 10:30 adir
drwxr-xr-x 2 root root 4096 Dec 7 10:30 bdir
-rw-r--r-- 1 root root 12 Dec 7 11:08 cfile
-rw-r--r-- 1 root root 4 Dec 7 10:55 dfile
lrwxrwxrwx 1 root root 14 Dec 7 10:33 dir -> /root/test/dir
[root@node1 test]# cp -s /root/test/dir/cfile dir-1/file
[root@node1 test]# ll dir-1/
total 16
drwxr-xr-x 2 root root 4096 Dec 7 10:30 adir
drwxr-xr-x 2 root root 4096 Dec 7 10:30 bdir
-rw-r--r-- 1 root root 12 Dec 7 11:08 cfile
-rw-r--r-- 1 root root 4 Dec 7 10:55 dfile
lrwxrwxrwx 1 root root 14 Dec 7 10:33 dir -> /root/test/dir
lrwxrwxrwx 1 root root 20 Dec 7 11:21 file -> /root/test/dir/cfile
[root@node1 test]# cat dir/cfile
111
222
[root@node1 test]# cat dir-1/cfile
111
[root@node1 test]# cp -b -S _bak dir/cfile dir-1/cfile
cp: overwrite ‘dir-1/cfile’? y
[root@node1 test]# ll dir-1/
total 20
drwxr-xr-x 2 root root 4096 Dec 7 10:30 adir
drwxr-xr-x 2 root root 4096 Dec 7 10:30 bdir
-rw-r--r-- 1 root root 8 Dec 7 11:28 cfile
-rw-r--r-- 1 root root 4 Dec 7 11:26 cfile_bak
-rw-r--r-- 1 root root 4 Dec 7 10:55 dfile
lrwxrwxrwx 1 root root 14 Dec 7 10:33 dir -> /root/test/dir
lrwxrwxrwx 1 root root 20 Dec 7 11:21 file -> /root/test/dir/cfile
[root@node1 test]# cat dir-1/cfile_bak
111
[root@node1 test]# cat dir-1/cfile
111
222
1.7.mv命令
解释
命令用来为文件或目录改名、或将文件或目录移入其它位置
选项
-b: 当目标文件或目录存在时,在执行覆盖前,会为其创建一个备份
-S <备份字尾字符串>:在备份文件时,用指定的后缀代替文件的默认后缀
-i: 如果指定移动的源目录或文件与目标的目录或文件同名,则会先询问是否覆盖旧文件,输入 y 表示直接覆盖,输入 n 表示取消该操作
-f: 如果指定移动的源目录或文件与目标的目录或文件同名,不会询问,直接覆盖旧文件
-n: 不要覆盖任何已存在的文件或目录
-u:当源文件比目标文件新或者目标文件不存在时,才执行移动操作
用法
mv dir/ a/dir-1 #移动目录并改名
实例
[root@node1 test]# ll
total 8
drwxr-xr-x 2 root root 4096 Dec 7 11:49 a
drwxr-xr-x 3 root root 4096 Dec 7 11:50 dir
[root@node1 test]# ll -a dir/
total 12
drwxr-xr-x 3 root root 4096 Dec 7 11:50 .
drwxr-xr-x 4 root root 4096 Dec 7 11:49 ..
drwxr-xr-x 2 root root 4096 Dec 7 11:49 adir
-rw-r--r-- 1 root root 0 Dec 7 11:49 cfile
-rw-r--r-- 1 root root 0 Dec 7 11:50 .e.txt
[root@node1 test]# mv dir/ a/dir-1
[root@node1 test]# ll -a a/dir-1/
total 12
drwxr-xr-x 3 root root 4096 Dec 7 11:50 .
drwxr-xr-x 3 root root 4096 Dec 7 11:50 ..
drwxr-xr-x 2 root root 4096 Dec 7 11:49 adir
-rw-r--r-- 1 root root 0 Dec 7 11:49 cfile
-rw-r--r-- 1 root root 0 Dec 7 11:50 .e.txt
1.8.rm命令
解释
命令用于删除一个文件或者目录
选项
-d:直接把欲删除的目录的硬连接数据删除成0,删除该目录
-f:强制删除文件或目录
-i:删除已有文件或目录之前先询问用户
-r或-R:递归处理,将指定目录下的所有文件与子目录一并处理
–preserve-root:不对根目录进行递归操作
-v:显示指令的详细执行过程
用法
rm -rf a/dir-1 #递归强制删除目录下所有文件
实例
[root@node1 test]# ll -a a/dir-1/
total 12
drwxr-xr-x 3 root root 4096 Dec 7 11:50 .
drwxr-xr-x 3 root root 4096 Dec 7 11:50 ..
drwxr-xr-x 2 root root 4096 Dec 7 11:49 adir
-rw-r--r-- 1 root root 0 Dec 7 11:49 cfile
-rw-r--r-- 1 root root 0 Dec 7 11:50 .e.txt
[root@node1 test]# rm -rf a/dir-1
[root@node1 test]# ll a
total 0
1.9.tree命令
解释
命令用于以树状图列出目录的内容,它会列出指定目录下的所有文件,包括子目录里的文件
选项
-a:显示所有文件和目录,以及带".“的影藏文件
-A:使用ASNI绘图字符显示树状图而非以ASCII字符组合
-C:在文件和目录清单加上色彩,便于区分各种类型
-d:显示目录名称而非内容
-D:列出文件或目录的更改时间
-f:在每个文件或目录之前,显示完整的相对路径名称
-F:在执行文件,目录,Socket,符号连接,管道名称名称,各自加上”*“,”/“,”=“,”@“,”|“号
-g:列出文件或目录的所属群组名称,没有对应的名称时,则显示群组识别码
-i:不以阶梯状列出文件或目录名称
-L:level 限制目录显示层级
-l:如遇到性质为符号连接的目录,直接列出该连接所指向的原始目录
-n:不在文件和目录清单加上色彩
-N:直接列出文件和目录名称,包括控制字符
-p:列出权限标示
-P<范本样式> :只显示符合范本样式的文件或目录名称
-q:用”?"号取代控制字符,列出文件和目录名称
-s:列出文件或目录大小
-t:用文件和目录的更改时间排序
-u:列出文件或目录的拥有者名称,没有对应的名称时,则显示用户识别码
-x:将范围局限在现行的文件系统中,若指定目录下的某些子目录,其存放于另一个文件系统上,则将该子目录予以排除在寻找范围外
用法
tree -a dir/ #递归显示目录下所有文件和目录、影藏文件
tree -a -C dir/ #递归显示目录下所有文件和目录、影藏文件,显示颜色
tree -a -C -i dir/ #递归显示目录下所有文件和目录、影藏文件,显示颜色,以阶梯型显示
tree -a -C -d dir/ #递归显示目录下所有文件和目录、影藏文件,显示颜色,只显示目录
tree -a -C -f -h -p dir/ #递归显示目录下所有文件和目录、影藏文件,显示颜色、路径、大小、权限
tree -a -C -P *file dir/ #递归显示目录下所有文件和目录、影藏文件,显示颜色,只显示匹配*file的文件
tree -a -C -L 2 dir/ #递归显示目录下所有文件和目录、影藏文件,显示颜色,只显示下2级目录内容
实例
[root@node1 test]# tree -a dir/
dir/
├── adir
│ ├── bdir
│ │ └── ufile
│ └── dfile
├── cfile
└── .txt
2 directories, 4 files
[root@node1 test]# tree -a -C -i dir/
dir/
adir
bdir
ufile
dfile
cfile
.txt
2 directories, 4 files
[root@node1 test]# tree -a -C -d dir/
dir/
└── adir
└── bdir
2 directories
[root@node1 test]# tree -a -C -f -h -p dir/
dir
├── [drwxr-xr-x 4.0K] dir/adir
│ ├── [drwxr-xr-x 4.0K] dir/adir/bdir
│ │ └── [-rw-r--r-- 0] dir/adir/bdir/ufile
│ └── [-rw-r--r-- 0] dir/adir/dfile
├── [-rw-r--r-- 0] dir/cfile
└── [-rw-r--r-- 0] dir/.txt
2 directories, 4 files
[root@node1 test]# tree -a -C dir/
dir/
├── adir
│ ├── bdir
│ │ └── ufile
│ └── dfile
├── cfile
└── .txt
2 directories, 4 files
[root@node1 test]# tree -a -C -P *file dir/
dir/
├── adir
│ ├── bdir
│ │ └── ufile
│ └── dfile
└── cfile
2 directories, 3 files
[root@node1 test]# tree -a -C dir/
dir/
├── adir
│ ├── bdir
│ │ └── ufile
│ └── dfile
├── cfile
└── .txt
2 directories, 4 files
[root@node1 test]# tree -a -C -L 2 dir/
dir/
├── adir
│ ├── bdir
│ └── dfile
├── cfile
└── .txt
2 directories, 3 files
1.10.rmdir命令
解释
命令删除空的目录
选项
-p或–parents:删除指定目录后,若该目录的上层目录已变成空目录,则将其一并删除
–ignore-fail-on-non-empty:此选项使rmdir命令忽略由于删除非空目录时导致的错误信息
-v或-verboes:显示命令的详细执行过程
用法
rmdir -p dir-1/dir/ #递归删除空目录
实例
[root@node1 test]# mkdir -p dir-1/dir
[root@node1 test]# tree dir-1
dir-1
└── dir
1 directory, 0 files
[root@node1 test]# rmdir -p dir-1/dir/
[root@node1 test]# ll
total 4
drwxr-xr-x 3 root root 4096 Dec 7 12:19 dir
1.11.dirs命令
解释
命令用于显示目录记录,显示目录堆叠中的记录
选项
-c:删除目录栈中的所有记录
-l:以完整格式显示
-p:一个目录一行的方式显示
-v:每行一个目录来显示目录栈的内容,每个目录前加上的编号
+N:显示从左到右的第n个目录,数字从0开始
-N:显示从右到左的第n个日录,数字从0开始
用法
dirs -v #显示目录堆叠记录
dirs -c #删除目录堆叠记录
dirs -l #完整显示目录堆叠记录
dirs ±2 #显示正数第二和倒数第二目录记录
实例
[root@node1 test]# dirs -v
0 ~/test
1 /opt/
2 /boot/
3 /mnt
[root@node1 test]# dirs -c
[root@node1 test]# dirs -v
0 ~/test
[root@node1 test]# dirs -l
/root/test /mnt/ /boot/ /opt/
[root@node1 test]# dirs +2
/boot/
[root@node1 test]# dirs -v
0 ~/test
1 /mnt/
2 /boot/
3 /opt/
[root@node1 test]# dirs -2
/mnt/
1.12.pushd命令
解释
命令是将目录加入命令堆叠中
选项
-n:只加入目录到堆叠中,不进行cd操作
+n:切换从左到右的第n个目录,数字从0开始
-n:切换从右到左的第n个目录,数字从0开始
用法
pushd -n /mnt #添加目录到堆叠中
pushd ±1 #切换到堆叠目录下
实例
[root@node1 test]# dirs -v
0 ~/test
1 /boot
[root@node1 test]# pushd -n /mnt
~/test /mnt /boot
[root@node1 test]# dirs -v
0 ~/test
1 /mnt
2 /boot
[root@node1 test]# pushd +1
/mnt /boot ~/test
[root@node1 mnt]# dirs -v
0 /mnt
1 /boot
2 ~/test
[root@node1 mnt]# pushd -1
/boot ~/test /mnt
1.13.popd命令
解释
命令用于删除目录栈中的记录
选项
-n:将目录出栈时,不切换目录。
+n:删除从左到右的第n个目录,数字从0开始
-n:删除从右到左的第n个目录,数字从0开始
用法
popd -n /mnt #删除目录到堆叠中
pushd ±1 #删除到堆叠目录下
实例
[root@node1 boot]# dirs -v
0 /boot
1 ~/test
2 /mnt
[root@node1 boot]# popd -n /mnt
/boot ~/test
[root@node1 boot]# dirs -v
0 /boot
1 ~/test
[root@node1 boot]# popd -1
~/test
[root@node1 test]#