pwd命令是“print working directory”中每个单词的首字母缩写,其功能是显示当前工作目录的绝对路径。在实际工作中,我们在命令行操作命令时,经常会在各个目录路径之间进行切换,此时可以使用pwd命令快速查看当前我们所在的目录路径。
pwd [option]
说明:通常情况下,执行pwd命令不需要带任何参数。
| 参数选项 | 解释说明|
| :-------- |: --------:|
| -L | logical首字符缩写,显示链接路径 (默认)|
| -P | physical首字符缩写,表示显示物理路径时如果当前目录路径是软链接文件,则会显示软链接文件对应的源文件,此参数不常用 |
[root@test ~]# pwd
/root
[root@test ~]# ll -d /etc/init.d
lrwxrwxrwx. 1 root root 11 7月 29 23:50 /etc/init.d -> rc.d/init.d #/etc/init.d是/etc/rc.d/init.d目录的软连接,相当于快捷方式。
[root@test ~]# cd /etc/init.d/ #进入/etc/init.d目录
[root@test init.d]# pwd #不加参数显示的是当前软链接目录
/etc/init.d
[root@test init.d]# pwd -P #加上-P参数显示的是当前软链接目录的物理路径
/etc/rc.d/init.d
[root@test init.d]# pwd -L #加上-L参数显示的是当前软链接目录
/etc/init.d
cd命令是"change directory"中每个单词的首字母缩写,其功能是从当前工作目录切换到指定的工作目录。
cd [option] [dir]
cd 【选项】 【目录】
说明:
2)cd是bash shell的内置命令,查看该命令对应的系统帮助需要使用help cd。
| 参数选项 | 解释说明 |
| :-------- |: --------:|
| -P | 如果切换的目标目录是一个软连接,则会直接切换到软链接,则会直接切换到软连接指向的真正物理目标目录 ,和pwd命令的-P选项功能类似,该参数不常用 |
| -L | 功能与-P参数相反,如果切换的目标是一个软链接,则直接切换到软链接所在的目录,和pwd命令的-L选项功能类似,该参数不常用 |
| - | 当只使用“-”选项时,将会从当前目录切换到系统环境变量“OLDPWD”对应值的目录路径,即当前用户上一次所在的目录路径 |
| ~| 当只使用“~”选项时,将会从当前目录切换到系统环境变量“HOME”对应值的目录路径,即当前用户的家目录所在的路径|
| .. | 当只使用“..”选项时,将会从当前目录切换到当前母的上一级目录所在的路径。 |
[root@test ~]# pwd #当前所在目录为root用户的家目录/root
/root
[root@test ~]# cd /etc/nginx/ #切换到/etc/nginx目录
[root@test nginx]# pwd
/etc/nginx
[root@test nginx]# cd /tmp/ #切换到/tmp目录
[root@test tmp]# pwd
/tmp
[root@test tmp]# pwd
/tmp
[root@test tmp]# cd -
/etc/nginx
[root@test nginx]# pwd
/etc/nginx
[root@test nginx]# cd ~
[root@test ~]# pwd
/root
[root@test ~]# pwd
/root
[root@test ~]# cd ..
[root@test /]# pwd
/
ls命令可以理解为英文单词list的缩写,其功能是列出目录的内容及其内容属性信息(list directory contents)。
ls [option] [file]
ls 【选项】 【<文件或目录>】
参数选项 | 解释说明 |
---|---|
-l | 使用长格式列出文件及目录信息 |
-a | 显示目录下的所有文件,包括以“.”字符开始的隐藏文件 |
-t | 根据最后的修改时间(mtime)排序,默认是以文件名排序 |
-r | 依相反次序排序 |
-i | 显示inode节点信息 |
-d | 当遇到目录时,列出目录本身而非目录内的文件,并且不跟随符号链接 |
-h | 以人类可读的信息显示文件或目录大小 |
-S | 按从大到小排序 |
-R | 递归列出所有子目录 |
-X | 根据扩展名排序 |
-u | 配合-t选项,显示并按atime从新到旧排序 |
-c | 根据状态改变时间(ctime)排序 |
--full-time | 以完整的时间格式输出 |
--time-style={full-iso,long-iso,ios,locale} | 以不同的时间格式输出,long-iso效果最好 |
--time={atime,ctime} | 按不同的时间属性输出,atime表示按访问时间,ctime表示按改变权限属性时间,如果不假辞参数则默认为最后的修改时间 |
[root@test1 data]# ls /root/
anaconda-ks.cfg
[root@test1 data]# ls -a /root
. .. anaconda-ks.cfg .bash_history .bash_logout .bash_profile .bashrc .cshrc .pki .tcshrc .viminfo
[root@test1 data]# ll
total 0
-rw-r--r--. 1 root root 0 Jan 1 2020 a.txt
-rw-r--r--. 1 root root 0 Oct 13 14:40 bridge.txt
-rw-r--r--. 1 root root 0 Oct 13 13:51 b.txt
-rw-r--r--. 1 root root 0 Oct 13 13:52 c.txt
-rw-r--r--. 1 root root 0 Oct 13 13:52 d.txt
-rw-r--r--. 1 root root 0 Oct 13 13:52 e.txt
-rw-r--r--. 1 root root 0 Oct 13 13:52 f.txt
-rw-r--r--. 1 root root 0 Oct 13 13:52 g.txt
-rw-r--r--. 1 root root 0 Jan 1 2020 h.txt
[root@test1 data]# ls -l --time-style=long-iso
total 0
-rw-r--r--. 1 root root 0 2020-01-01 00:00 a.txt
-rw-r--r--. 1 root root 0 2019-10-13 14:40 bridge.txt
-rw-r--r--. 1 root root 0 2019-10-13 13:51 b.txt
-rw-r--r--. 1 root root 0 2019-10-13 13:52 c.txt
-rw-r--r--. 1 root root 0 2019-10-13 13:52 d.txt
-rw-r--r--. 1 root root 0 2019-10-13 13:52 e.txt
-rw-r--r--. 1 root root 0 2019-10-13 13:52 f.txt
-rw-r--r--. 1 root root 0 2019-10-13 13:52 g.txt
-rw-r--r--. 1 root root 0 2020-01-01 00:00 h.txt
ls --full-time用于显示完整的时间,等同于ls -l --time-style=long-iso。
[root@test1 data]# ll --time-style=long-iso -t
total 0
-rw-r--r--. 1 root root 0 2020-01-01 00:00 a.txt
-rw-r--r--. 1 root root 0 2020-01-01 00:00 h.txt
-rw-r--r--. 1 root root 0 2019-10-13 14:40 bridge.txt
-rw-r--r--. 1 root root 0 2019-10-13 13:52 c.txt
-rw-r--r--. 1 root root 0 2019-10-13 13:52 d.txt
-rw-r--r--. 1 root root 0 2019-10-13 13:52 e.txt
-rw-r--r--. 1 root root 0 2019-10-13 13:52 f.txt
-rw-r--r--. 1 root root 0 2019-10-13 13:52 g.txt
-rw-r--r--. 1 root root 0 2019-10-13 13:51 b.txt
[root@test1 data]# ll --time-style=long-iso -rt
total 0
-rw-r--r--. 1 root root 0 2019-10-13 13:51 b.txt
-rw-r--r--. 1 root root 0 2019-10-13 13:52 g.txt
-rw-r--r--. 1 root root 0 2019-10-13 13:52 f.txt
-rw-r--r--. 1 root root 0 2019-10-13 13:52 e.txt
-rw-r--r--. 1 root root 0 2019-10-13 13:52 d.txt
-rw-r--r--. 1 root root 0 2019-10-13 13:52 c.txt
-rw-r--r--. 1 root root 0 2019-10-13 14:40 bridge.txt
-rw-r--r--. 1 root root 0 2020-01-01 00:00 h.txt
-rw-r--r--. 1 root root 0 2020-01-01 00:00 a.txt
[root@test1 data]# ll -d /data/
drwxr-xr-x. 2 root root 128 Oct 13 14:29 /data/
[root@test1 data]# ll -h /etc/nginx/
total 60K
drwxr-xr-x. 2 root root 6 May 10 16:14 conf.d
drwxr-xr-x. 2 root root 6 May 10 16:14 default.d
-rw-r--r--. 1 root root 1.1K May 10 16:10 fastcgi.conf
-rw-r--r--. 1 root root 1.1K May 10 16:10 fastcgi.conf.default
-rw-r--r--. 1 root root 1007 May 10 16:10 fastcgi_params
-rw-r--r--. 1 root root 1007 May 10 16:10 fastcgi_params.default
-rw-r--r--. 1 root root 2.8K May 10 16:10 koi-utf
-rw-r--r--. 1 root root 2.2K May 10 16:10 koi-win
-rw-r--r--. 1 root root 3.9K May 10 16:10 mime.types
-rw-r--r--. 1 root root 3.9K May 10 16:10 mime.types.default
-rw-r--r--. 1 root root 2.5K May 10 16:10 nginx.conf
-rw-r--r--. 1 root root 2.6K May 10 16:10 nginx.conf.default
-rw-r--r--. 1 root root 636 May 10 16:10 scgi_params
-rw-r--r--. 1 root root 636 May 10 16:10 scgi_params.default
-rw-r--r--. 1 root root 664 May 10 16:10 uwsgi_params
-rw-r--r--. 1 root root 664 May 10 16:10 uwsgi_params.default
-rw-r--r--. 1 root root 3.6K May 10 16:10 win-utf
[root@test1 data]# ll -hS /etc/nginx/
total 60K
-rw-r--r--. 1 root root 3.9K May 10 16:10 mime.types
-rw-r--r--. 1 root root 3.9K May 10 16:10 mime.types.default
-rw-r--r--. 1 root root 3.6K May 10 16:10 win-utf
-rw-r--r--. 1 root root 2.8K May 10 16:10 koi-utf
-rw-r--r--. 1 root root 2.6K May 10 16:10 nginx.conf.default
-rw-r--r--. 1 root root 2.5K May 10 16:10 nginx.conf
-rw-r--r--. 1 root root 2.2K May 10 16:10 koi-win
-rw-r--r--. 1 root root 1.1K May 10 16:10 fastcgi.conf
-rw-r--r--. 1 root root 1.1K May 10 16:10 fastcgi.conf.default
-rw-r--r--. 1 root root 1007 May 10 16:10 fastcgi_params
-rw-r--r--. 1 root root 1007 May 10 16:10 fastcgi_params.default
-rw-r--r--. 1 root root 664 May 10 16:10 uwsgi_params
-rw-r--r--. 1 root root 664 May 10 16:10 uwsgi_params.default
-rw-r--r--. 1 root root 636 May 10 16:10 scgi_params
-rw-r--r--. 1 root root 636 May 10 16:10 scgi_params.default
drwxr-xr-x. 2 root root 6 May 10 16:14 conf.d
drwxr-xr-x. 2 root root 6 May 10 16:14 default.d
[root@test1 bridge]# ll -R /bridge/
/bridge/:
total 0
-rw-r--r--. 1 root root 0 Oct 13 16:12 1.txt
-rw-r--r--. 1 root root 0 Oct 13 16:12 2.txt
-rw-r--r--. 1 root root 0 Oct 13 16:12 3.txt
drwxr-xr-x. 2 root root 76 Oct 13 16:11 a
drwxr-xr-x. 2 root root 6 Oct 13 16:06 b
drwxr-xr-x. 2 root root 6 Oct 13 16:06 c
drwxr-xr-x. 2 root root 6 Oct 13 16:06 d
drwxr-xr-x. 2 root root 6 Oct 13 16:09 e
dr--r--r--. 2 root root 6 Oct 13 16:09 f
/bridge/a:
total 0
-rw-r--r--. 1 root root 0 Oct 13 16:11 a1.txt
-rw-r--r--. 1 root root 0 Oct 13 16:11 a2.txt
-rw-r--r--. 1 root root 0 Oct 13 16:11 a3.txt
-rw-r--r--. 1 root root 0 Oct 13 16:11 a4.txt
-rw-r--r--. 1 root root 0 Oct 13 16:11 a5.txt
/bridge/b:
total 0
/bridge/c:
total 0
/bridge/d:
total 0
/bridge/e:
total 0
/bridge/f:
total 0
[root@test1 ~]# ll -X /etc/nginx/
total 60
-rw-r--r--. 1 root root 1007 May 10 16:10 fastcgi_params
-rw-r--r--. 1 root root 2837 May 10 16:10 koi-utf
-rw-r--r--. 1 root root 2223 May 10 16:10 koi-win
-rw-r--r--. 1 root root 636 May 10 16:10 scgi_params
-rw-r--r--. 1 root root 664 May 10 16:10 uwsgi_params
-rw-r--r--. 1 root root 3610 May 10 16:10 win-utf
-rw-r--r--. 1 root root 1077 May 10 16:10 fastcgi.conf
-rw-r--r--. 1 root root 2467 May 10 16:10 nginx.conf
drwxr-xr-x. 2 root root 6 May 10 16:14 conf.d
drwxr-xr-x. 2 root root 6 May 10 16:14 default.d
-rw-r--r--. 1 root root 1077 May 10 16:10 fastcgi.conf.default
-rw-r--r--. 1 root root 1007 May 10 16:10 fastcgi_params.default
-rw-r--r--. 1 root root 3957 May 10 16:10 mime.types.default
-rw-r--r--. 1 root root 2656 May 10 16:10 nginx.conf.default
-rw-r--r--. 1 root root 636 May 10 16:10 scgi_params.default
-rw-r--r--. 1 root root 664 May 10 16:10 uwsgi_params.default
-rw-r--r--. 1 root root 3957 May 10 16:10 mime.types
[root@test1 ~]# ll -ut --full-time /data/
total 0
-rw-r--r--. 1 root root 0 2020-01-01 00:00:00.000000000 +0800 a.txt
-rw-r--r--. 1 root root 0 2020-01-01 00:00:00.000000000 +0800 h.txt
-rw-r--r--. 1 root root 0 2019-10-13 14:40:20.000000000 +0800 bridge.txt
-rw-r--r--. 1 root root 0 2019-10-13 13:52:09.605229604 +0800 c.txt
-rw-r--r--. 1 root root 0 2019-10-13 13:52:09.605229604 +0800 d.txt
-rw-r--r--. 1 root root 0 2019-10-13 13:52:09.605229604 +0800 e.txt
-rw-r--r--. 1 root root 0 2019-10-13 13:52:09.605229604 +0800 f.txt
-rw-r--r--. 1 root root 0 2019-10-13 13:52:09.605229604 +0800 g.txt
-rw-r--r--. 1 root root 0 2019-10-13 13:51:49.394213773 +0800 b.txt
touch命令有两个功能:一是创建新的空文件;二是改变已有文件的时间戳属性。
touch [option] [file]
touch 【选项】 【文件】
特别说明:在linux中,一切皆文件。虽然touch命令不能创建目录,但是可以修改目录的时间戳。
参数选项 | 解释说明 |
---|---|
-a | 只更改指定文件的最后访问时间atime和状态改变时间ctime |
-m | 只更改指定文件的最后修改时间mtime和状态改变时间ctime |
-d STRING | 使用字符串STRING代表的时间作为模板设置指定文件的时间属性 |
-r file | 将指定文件的时间属性设置为与模板文件file的时间属性相同 |
-t STAMP | 使用[[CC]YY]MMDDhhmm[.ss]格式的时间设置文件的时间属性。格式的含义从左到右依次为:世纪、年、月、日、时、分、秒 |
说明:文件的时间戳属性分为访问时间(Access)、修改时间(Modify)、状态改变时间(Change)。
[root@test ~]# mkdir -p /data
[root@test ~]# cd /data/
[root@test data]# ll
总用量 0
[root@test data]# touch bridge.txt #创建一个文件
[root@test data]# ls
bridge.txt
[root@test data]# touch a.txt b.txt #创建多个文件
[root@test data]# ls
a.txt bridge.txt b.txt
[root@test data]# touch {c..g}.txt #使用大括号{}创建多个文件
[root@test data]# ls
a.txt bridge.txt b.txt c.txt d.txt e.txt f.txt g.txt
[root@test1 data]# stat bridge.txt #stat命令可以查看文件的时间戳属性
File: ‘bridge.txt’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd00h/64768d Inode: 134272284 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:default_t:s0
Access: 2019-10-13 13:55:01.027363876 +0800
Modify: 2019-10-13 13:51:40.029206437 +0800
Change: 2019-10-13 13:55:01.027363876 +0800
Birth: -
[root@test1 data]# touch -a bridge.txt
[root@test1 data]# stat bridge.txt
File: ‘bridge.txt’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd00h/64768d Inode: 134272284 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:default_t:s0
Access: 2019-10-13 14:14:04.607300017 +0800
Modify: 2019-10-13 13:51:40.029206437 +0800
Change: 2019-10-13 14:14:04.607300017 +0800
Birth: -
[root@test1 data]# stat bridge.txt
File: ‘bridge.txt’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd00h/64768d Inode: 134272284 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:default_t:s0
Access: 2019-10-13 14:14:04.607300017 +0800
Modify: 2019-10-13 13:51:40.029206437 +0800
Change: 2019-10-13 14:14:04.607300017 +0800
Birth: -
[root@test1 data]# touch -m bridge.txt
[root@test1 data]# stat bridge.txt
File: ‘bridge.txt’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd00h/64768d Inode: 134272284 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:default_t:s0
Access: 2019-10-13 14:14:04.607300017 +0800
Modify: 2019-10-13 14:17:48.641883010 +0800
Change: 2019-10-13 14:17:48.641883010 +0800
Birth: -
[root@test1 data]# ll --time-style=long-iso
total 0
-rw-r--r--. 1 root root 0 2019-10-13 13:51 a.txt
-rw-r--r--. 1 root root 0 2019-10-13 14:17 bridge.txt
-rw-r--r--. 1 root root 0 2019-10-13 13:51 b.txt
-rw-r--r--. 1 root root 0 2019-10-13 13:52 c.txt
-rw-r--r--. 1 root root 0 2019-10-13 13:52 d.txt
-rw-r--r--. 1 root root 0 2019-10-13 13:52 e.txt
-rw-r--r--. 1 root root 0 2019-10-13 13:52 f.txt
-rw-r--r--. 1 root root 0 2019-10-13 13:52 g.txt
[root@test1 data]# touch -d 20200101 h.txt #创建h.txt文件,并指定其创建时间为20200101
[root@test1 data]# ll --time-style=long-iso h.txt
-rw-r--r--. 1 root root 0 2020-01-01 00:00 h.txt #显示h.txt的创建时间为20200101
[root@test1 data]# ll --time-style=long-iso bridge.txt
-rw-r--r--. 1 root root 0 2019-10-13 14:17 bridge.txt
[root@test1 data]# touch -d 20200101 bridge.txt #修改bridge.txt的创建时间为20200101
[root@test1 data]# ll --time-style=long-iso bridge.txt
-rw-r--r--. 1 root root 0 2020-01-01 00:00 bridge.txt #bridge.txt文件的创建时间修改为了20200101
[root@test1 data]# ll --time-style=long-iso a.txt
-rw-r--r--. 1 root root 0 2019-10-13 13:51 a.txt
[root@test1 data]# touch -r bridge.txt a.txt
[root@test1 data]# ll --time-style=long-iso a.txt
-rw-r--r--. 1 root root 0 2020-01-01 00:00 a.txt #a.txt文件的修改时间和bridge.txt一致了
[root@test1 data]# ls -lh --full-time bridge.txt
-rw-r--r--. 1 root root 0 2020-01-01 00:00:00.000000000 +0800 bridge.txt
[root@test1 data]# touch -t 201910131440.20 bridge.txt
[root@test1 data]# ls -lh --full-time bridge.txt
-rw-r--r--. 1 root root 0 2019-10-13 14:40:20.000000000 +0800 bridge.txt
mkdir命令是“make directories”的缩写,其功能是创建目录,默认情况下,如果要创建的目录已存在,则会提示此目录已存在;而不会继续创建目录。
mkdir [option] [directory]
mkdir 【选项】 【目录】
参数选项 | 解释说明 |
---|---|
-p | 1.递归创建目录 2.即使要创建的目录事先已存在也不会报错提示目录已存在 |
-m | 设置新创建目录的默认目录对应的权限 |
[root@test1 ~]# mkdir -p /bridge/{a..d}
[root@test1 ~]# ll /bridge/
total 0
drwxr-xr-x. 2 root root 6 Oct 13 16:06 a
drwxr-xr-x. 2 root root 6 Oct 13 16:06 b
drwxr-xr-x. 2 root root 6 Oct 13 16:06 c
drwxr-xr-x. 2 root root 6 Oct 13 16:06 d
[root@test1 bridge]# mkdir e
[root@test1 bridge]# ll -d e
drwxr-xr-x. 2 root root 6 Oct 13 16:09 e
[root@test1 bridge]# mkdir -m 444 f
[root@test1 bridge]# ll -d f
dr--r--r--. 2 root root 6 Oct 13 16:09 f
stat命令用于详细显示文件或文件系统的状态信息(元数据信息)。
stat [option] [file]
stat 【选项】 【文件或目录】
[root@test1 data]# stat a.txt
File: ‘a.txt’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd00h/64768d Inode: 134272285 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:default_t:s0
Access: 2020-01-01 00:00:00.000000000 +0800
Modify: 2020-01-01 00:00:00.000000000 +0800
Change: 2019-10-13 14:36:33.470783988 +0800
Birth: -
cp命令可以理解为英文单词copy的缩写,其功能为复制文件或目录。
cp [option] [source] [dest]
cp 【选项】 【源文件】 【目标文件】
参数选项 | 解释说明 |
---|---|
-p | 复制文件时保持源文件的所有者、权限信息及时间属性 |
-d | 复制链接文件而不是它指向的源文件 |
-r | 递归复制目录,即复制目录下的所有层级的子目录及文件 |
-a | 等同于上面的p、d、r这3个选项功能的总和 |
-i | 覆盖已有文件前提示用户确认 |
说明:我们使用的cp命令被系统默认设置了cp -i的别名了。
[root@test1 cp_test]# ll
total 4
-rw-r--r--. 1 root root 2 Oct 13 17:46 a.txt
[root@test1 cp_test]# cp a.txt a1.txt
[root@test1 cp_test]# ll
total 8
-rw-r--r--. 1 root root 2 Oct 13 18:35 a1.txt
-rw-r--r--. 1 root root 2 Oct 13 17:46 a.txt
[root@test1 cp_test]# cp -p a.txt a2.txt
[root@test1 cp_test]# ll
total 12
-rw-r--r--. 1 root root 2 Oct 13 18:35 a1.txt
-rw-r--r--. 1 root root 2 Oct 13 17:46 a2.txt
-rw-r--r--. 1 root root 2 Oct 13 17:46 a.txt
[root@test1 cp_test]# ll
total 12
-rw-r--r--. 1 root root 2 Oct 13 18:35 a1.txt
-rw-r--r--. 1 root root 2 Oct 13 17:46 a2.txt
-rw-r--r--. 1 root root 2 Oct 13 17:46 a.txt
[root@test1 cp_test]# cp /bridge/a/ .
cp: omitting directory ‘/bridge/a/’
[root@test1 cp_test]# ll
total 12
-rw-r--r--. 1 root root 2 Oct 13 18:35 a1.txt
-rw-r--r--. 1 root root 2 Oct 13 17:46 a2.txt
-rw-r--r--. 1 root root 2 Oct 13 17:46 a.txt
[root@test1 cp_test]# cp -r /bridge/a/ .
[root@test1 cp_test]# ll
total 12
drwxr-xr-x. 2 root root 76 Oct 13 18:39 a
-rw-r--r--. 1 root root 2 Oct 13 18:35 a1.txt
-rw-r--r--. 1 root root 2 Oct 13 17:46 a2.txt
-rw-r--r--. 1 root root 2 Oct 13 17:46 a.txt
mv命令可以理解为英文单词move的缩写,其功能是移动或重命名文件
mv [option] [source] [dest]
mv 【选项】 【源文件】 【目标文件】
参数选项 | 解释说明 |
---|---|
-f | 若目标文件已经存在,则不会询问而是直接覆盖(慎用) |
-i | 若目标文件已经存在,则会询问是否覆盖 |
说明:我们使用的mv命令被系统默认设置了mv -i的别名。
[root@test1 mv_dir]# ll
total 8
-rw-r--r--. 1 root root 8 Oct 13 18:54 a.txt
-rw-r--r--. 1 root root 8 Oct 13 18:54 b.txt
-rw-r--r--. 1 root root 0 Oct 13 18:55 c.txt
[root@test1 mv_dir]# mv a.txt d.txt
[root@test1 mv_dir]# ll
total 8
-rw-r--r--. 1 root root 8 Oct 13 18:54 b.txt
-rw-r--r--. 1 root root 0 Oct 13 18:55 c.txt
-rw-r--r--. 1 root root 8 Oct 13 18:54 d.txt
[root@test1 mv_dir]# cat b.txt
hello b
[root@test1 mv_dir]# cat c.txt
[root@test1 mv_dir]# mv b.txt c.txt
mv: overwrite ‘c.txt’? y
[root@test1 mv_dir]# ll
total 8
-rw-r--r--. 1 root root 8 Oct 13 18:54 c.txt
-rw-r--r--. 1 root root 8 Oct 13 18:54 d.txt
[root@test1 mv_dir]# cat c.txt
hello b
[root@test1 mv_dir]# ll
total 8
-rw-r--r--. 1 root root 8 Oct 13 18:54 c.txt
-rw-r--r--. 1 root root 8 Oct 13 18:54 d.txt
[root@test1 mv_dir]# cat c.txt
hello b
[root@test1 mv_dir]# cat d.txt
hello a
[root@test1 mv_dir]# mv -f d.txt c.txt
[root@test1 mv_dir]# ll
total 4
-rw-r--r--. 1 root root 8 Oct 13 18:54 c.txt
[root@test1 mv_dir]# cat c.txt
hello a
rm命令可以理解为英文单词remove的缩写,其功能是删除一个或多个文件或目录(remove files or directories)。这是Linux系统里最危险的命令之一,请慎用。
rm [option] [file]
rm 【选项】 【<文件或目录>】
参数选项 | 解释说明 |
---|---|
-f | 强制删除。忽略不存在的文件,不提示确认 |
-i | 再删除前需要确认 |
-r | 递归删除目录及其内容 |
说明:我们使用的rm命令被系统默认设置了rm -i的别名。
[root@test1 bridge]# rm 1.txt
rm: remove regular empty file ‘1.txt’? y
[root@test1 bridge]# ll
total 0
-rw-r--r--. 1 root root 0 Oct 13 16:12 2.txt
-rw-r--r--. 1 root root 0 Oct 13 16:12 3.txt
drwxr-xr-x. 2 root root 76 Oct 13 16:11 a
drwxr-xr-x. 2 root root 6 Oct 13 16:06 b
drwxr-xr-x. 2 root root 6 Oct 13 16:06 c
drwxr-xr-x. 2 root root 6 Oct 13 16:06 d
drwxr-xr-x. 2 root root 6 Oct 13 16:09 e
dr--r--r--. 2 root root 6 Oct 13 16:09 f
[root@test1 bridge]# ll
total 0
-rw-r--r--. 1 root root 0 Oct 13 16:12 2.txt
-rw-r--r--. 1 root root 0 Oct 13 16:12 3.txt
drwxr-xr-x. 2 root root 76 Oct 13 16:11 a
drwxr-xr-x. 2 root root 6 Oct 13 16:06 b
drwxr-xr-x. 2 root root 6 Oct 13 16:06 c
drwxr-xr-x. 2 root root 6 Oct 13 16:06 d
drwxr-xr-x. 2 root root 6 Oct 13 16:09 e
dr--r--r--. 2 root root 6 Oct 13 16:09 f
[root@test1 bridge]# rm -rf 2.txt
[root@test1 bridge]# ll
total 0
-rw-r--r--. 1 root root 0 Oct 13 16:12 3.txt
drwxr-xr-x. 2 root root 76 Oct 13 16:11 a
drwxr-xr-x. 2 root root 6 Oct 13 16:06 b
drwxr-xr-x. 2 root root 6 Oct 13 16:06 c
drwxr-xr-x. 2 root root 6 Oct 13 16:06 d
drwxr-xr-x. 2 root root 6 Oct 13 16:09 e
dr--r--r--. 2 root root 6 Oct 13 16:09 f
[root@test1 bridge]# ll
total 0
-rw-r--r--. 1 root root 0 Oct 13 16:12 3.txt
drwxr-xr-x. 2 root root 76 Oct 13 16:11 a
drwxr-xr-x. 2 root root 6 Oct 13 16:06 c
drwxr-xr-x. 2 root root 6 Oct 13 16:06 d
drwxr-xr-x. 2 root root 6 Oct 13 16:09 e
dr--r--r--. 2 root root 6 Oct 13 16:09 f
[root@test1 bridge]# rm c
rm: cannot remove ‘c’: Is a directory
[root@test1 bridge]# ll
total 0
-rw-r--r--. 1 root root 0 Oct 13 16:12 3.txt
drwxr-xr-x. 2 root root 76 Oct 13 16:11 a
drwxr-xr-x. 2 root root 6 Oct 13 16:06 c
drwxr-xr-x. 2 root root 6 Oct 13 16:06 d
drwxr-xr-x. 2 root root 6 Oct 13 16:09 e
dr--r--r--. 2 root root 6 Oct 13 16:09 f
[root@test1 bridge]# rm -rf c
[root@test1 bridge]# ll
total 0
-rw-r--r--. 1 root root 0 Oct 13 16:12 3.txt
drwxr-xr-x. 2 root root 76 Oct 13 16:11 a
drwxr-xr-x. 2 root root 6 Oct 13 16:06 d
drwxr-xr-x. 2 root root 6 Oct 13 16:09 e
dr--r--r--. 2 root root 6 Oct 13 16:09 f
tree命令的中文意思为“树”,功能室以树形结构列出指定目录下的所有内容,包括所有文件、子目录及子目录里的目录和文件。
tree [option] [directory]
tree 【选项】 【目录】
参数选项 | 解释说明 |
---|---|
-d | 只显示目录 |
-f | 显示每个文件的全路径 |
-L level | 遍历目录的最大层数,level为大于0的正整数 |
-a | 显示所有文件,包括隐藏文件 |
[root@test1 bridge]# tree
.
├── 3.txt
├── a
│ ├── a1.txt
│ ├── a2.txt
│ ├── a3.txt
│ ├── a4.txt
│ └── a5.txt
├── d
├── e
└── f
4 directories, 6 files
[root@test1 bridge]# tree -d
.
├── a
├── d
├── e
└── f
4 directories
[root@test1 bridge]# tree -f
.
├── ./3.txt
├── ./a
│ ├── ./a/a1.txt
│ ├── ./a/a2.txt
│ ├── ./a/a3.txt
│ ├── ./a/a4.txt
│ └── ./a/a5.txt
├── ./d
├── ./e
└── ./f
4 directories, 6 files
[root@test1 bridge]# tree -L 1
.
├── 3.txt
├── a
├── d
├── e
└── f
4 directories, 1 file
[root@test1 bridge]# tree /root/
/root/
└── anaconda-ks.cfg
0 directories, 1 file
[root@test1 bridge]# tree -a /root/
/root/
├── anaconda-ks.cfg
├── .bash_history
├── .bash_logout
├── .bash_profile
├── .bashrc
├── .cshrc
├── .pki
│ └── nssdb
├── .tcshrc
└── .viminfo
2 directories, 8 files
[root@test1 ~]# mkdir -p /tmp/a1/{a,b} /tmp/a2
[root@test1 ~]# tree /tmp/
/tmp/
├── a1
│?? ├── a
│?? └── b
└── a2
4 directories, 0 files
查看文件元数据使用stat命令查看,如下:
[root@test1 data]# stat a.txt
File: ‘a.txt’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd00h/64768d Inode: 134272285 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:default_t:s0
Access: 2020-01-01 00:00:00.000000000 +0800
Modify: 2020-01-01 00:00:00.000000000 +0800
Change: 2019-10-13 14:36:33.470783988 +0800
Birth: -
[root@test1 ~]# touch /tmp/tfile-$(date +%Y-%m-%d-%H-%M-%S)
[root@test1 ~]# ll /tmp/
total 0
drwxr-xr-x. 4 root root 24 Oct 13 22:56 a1
drwxr-xr-x. 2 root root 6 Oct 13 22:56 a2
-rw-r--r--. 1 root root 0 Oct 13 22:59 tfile-2019-10-13-22-59-40
[root@test1 ~]# mkdir -p /tmp/mytest1
[root@test1 ~]# cp -rp /etc/p*[^0-9] /tmp/mytest1/
[root@test1 ~]# ll /tmp/mytest1/
total 28
drwxr-xr-x. 2 root root 4096 Oct 13 11:57 pam.d
-rw-r--r--. 1 root root 944 Oct 13 13:16 passwd
-rw-r--r--. 1 root root 882 Oct 13 12:16 passwd-
drwxr-xr-x. 10 root root 116 Oct 13 19:36 pki
drwxr-xr-x. 2 root root 28 Oct 13 11:56 plymouth
drwxr-xr-x. 5 root root 52 Oct 13 19:35 pm
drwxr-xr-x. 2 root root 6 Jun 10 2014 popt.d
drwxr-xr-x. 2 root root 154 Oct 13 19:36 postfix
drwxr-xr-x. 3 root root 123 Oct 13 11:56 ppp
drwxr-xr-x. 2 root root 78 Oct 13 11:56 prelink.conf.d
-rw-r--r--. 1 root root 233 Jun 7 2013 printcap
-rw-r--r--. 1 root root 1819 Oct 31 2018 profile
drwxr-xr-x. 2 root root 280 Oct 13 12:19 profile.d
-rw-r--r--. 1 root root 6545 Oct 31 2018 protocols
drwxr-xr-x. 2 root root 35 Oct 13 11:56 python
[root@test1 ~]# groupadd jack
[root@test1 ~]# useradd tom -u 5001 -d /tmp/tom -s /bin/zsh -G jack
[root@test1 ~]# id tom
uid=5001(tom) gid=5001(tom) groups=5001(tom),1000(jack)
原文:https://www.cnblogs.com/bridge430/p/11668903.html