文件管理
文件管理命令
pwd命令
命令详解
功能说明
pwd命令是“print working directory”中每个单词的首字母缩写,其功能是显示当前工作目录的绝对路径。在实际工作中,我们在命令行操作命令时,经常会在各个目录路径之间进行切换,此时可以使用pwd命令快速查看当前我们所在的目录路径。
语法格式
pwd [option]
说明:通常情况下,执行pwd命令不需要带任何参数。
选项说明
| 参数选项 | 解释说明|
| :-------- |: --------😐
| -L | logical首字符缩写,显示链接路径 (默认)|
| -P | physical首字符缩写,表示显示物理路径时如果当前目录路径是软链接文件,则会显示软链接文件对应的源文件,此参数不常用 |
使用范例
不带任何选项的pwd命令
[root@test ~]# pwd
/root
对比使用-L和-P参数
[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命令
命令详解
功能说明
cd命令是"change directory"中每个单词的首字母缩写,其功能是从当前工作目录切换到指定的工作目录。
语法格式
cd [option] [dir]
cd 【选项】 【目录】
说明:
- 1)cd命令后面的选项和目录等参数都可以省略。默认情况下,单独执行cd命令,可切换到当前登录用户的家目录(由系统环境变量HOME定义)。
- 2)cd是bash shell的内置命令,查看该命令对应的系统帮助需要使用help cd。
选项说明
| 参数选项 | 解释说明 |
| :-------- |: --------😐
| -P | 如果切换的目标目录是一个软连接,则会直接切换到软链接,则会直接切换到软连接指向的真正物理目标目录 ,和pwd命令的-P选项功能类似,该参数不常用 |
| -L | 功能与-P参数相反,如果切换的目标是一个软链接,则直接切换到软链接所在的目录,和pwd命令的-L选项功能类似,该参数不常用 |
| - | 当只使用“-”选项时,将会从当前目录切换到系统环境变量“OLDPWD”对应值的目录路径,即当前用户上一次所在的目录路径 |
| ~| 当只使用“~”选项时,将会从当前目录切换到系统环境变量“HOME”对应值的目录路径,即当前用户的家目录所在的路径|
| .. | 当只使用“..”选项时,将会从当前目录切换到当前母的上一级目录所在的路径。 |
使用范例
进入/etc/nginx目录后进入/tmp目录
[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命令:显示目录下的内容及相关属性信息
命令详解
功能说明
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= | 以不同的时间格式输出,long-iso效果最好 |
| --time= | 按不同的时间属性输出,atime表示按访问时间,ctime表示按改变权限属性时间,如果不假辞参数则默认为最后的修改时间 |
使用范例
执行ls命令,不带任何参数
[root@test1 data]# ls /root/
anaconda-ks.cfg
使用-a参数显示所有文件,特别是隐藏文件
[root@test1 data]# ls -a /root
. .. anaconda-ks.cfg .bash_history .bash_logout .bash_profile .bashrc .cshrc .pki .tcshrc .viminfo
使用-l参数显示详细信息
[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
使用--time-style=long-iso选项显示完整时间属性
[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
说明:
- --time-style可选的参数值有如下几个,如full-iso、long-iso、iso、locale。默认值是locale。
- 在生产场景中经常会遇到同一目录下的文件及目录时间的显示不一致的问题,所以需要使用ls -l --time-style=long-iso来调整,如果觉得参数太多不好记,则可以设置一个别名管理。
- 执行ls -s等命令时,默认显示的是文件最后一次的修改时间(如果是新文件那么就是创建时间了)。
- ls --full-time用于显示完整的时间,等同于ls -l --time-style=long-iso。
使用-t参数,根据文件修改时间排序
[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
使用-rt参数,根据文件修改时间逆序排序
[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
使用-d参数显示目录本身信息
[root@test1 data]# ll -d /data/
drwxr-xr-x. 2 root root 128 Oct 13 14:29 /data/
使用-h参数以人类刻度的信息显示文件或目录大小
[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
使用-S参数将文件从大到小排序
[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
使用-R参数递归列出所有子目录
[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
使用-X参数根据文件扩展名排序
[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
使用-u配合-t参数,显示并按照atime从新到旧排序
[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命令有两个功能:一是创建新的空文件;二是改变已有文件的时间戳属性。
语法格式
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
使用-a参数更改文件访问时间
[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: -
使用-m参数更改文件的最后修改时间
[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: -
使用-d参数指定文件的创建/修改时间
[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
使用-r选项,修改a.txt的时间属性,使其与bridge.txt的时间属性一致
[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一致了
使用-t选项将文件设置为201910131440.20时间格式:
[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
扩展知识
GNU/Linux的文件有3种类型的时间戳:
- Access:最后访问文件的时间(读取文件内容)
- Modify:最后修改文件的时间(改变文件内容)
- Change:最后改变文件状态的时间(元数据发生改变)
对应ls命令,查看上述时间戳的选项如下:
- mtime:最后修改时间(ls -lt)<==修改文件内容,文件的修改时间(modify time)会改变
- ctime:状态改变时间(ls -lc) <==修改文件内容、移动文件或改变文件属性等,文件的状态改变时间(change time)会改变
- atime:最后访问时间(ls -lu) <==查看文件内容时,文件的访问时间(access time)会改变。
mkdir命令
命令详解
功能说明
mkdir命令是“make directories”的缩写,其功能是创建目录,默认情况下,如果要创建的目录已存在,则会提示此目录已存在;而不会继续创建目录。
语法格式
mkdir [option] [directory]
mkdir 【选项】 【目录】
选项说明
| 参数选项 | 解释说明 |
|---|---|
| -p | 1.递归创建目录 2.即使要创建的目录事先已存在也不会报错提示目录已存在 |
| -m | 设置新创建目录的默认目录对应的权限 |
使用范例
使用-p参数递归创建目录
[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
使用-m参数设置创建目录的默认权限
[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命令用于详细显示文件或文件系统的状态信息(元数据信息)。
语法格式
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: -
- File:文件名
- Size:文件大小
- Blocks:占用block数量
- IO Block:IO块大小
- regular empty file:文件类型为普通空文件
- Device:设备编号的十六进制(h)和十进制(d)
- Inode:文件的inode值
- Links:文件的硬链接数
- Access:文件权限
- UID和Gid:用户和用户组
- Access:最后访问文件的时间(读取文件内容)
- Modify:最后修改文件的时间(改变文件内容)
- Change:最后改变文件状态的时间(元数据发生改变)
cp命令
命令详解
功能说明
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
使用-p参数保持文件属性功能
[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
使用-r参数复制目录
[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命令
命令详解
功能说明
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
使用-f参数直接覆盖目标文件
[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命令
命令详解
功能说明
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
使用-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
使用-r参数删除目录
[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命令的中文意思为“树”,功能室以树形结构列出指定目录下的所有内容,包括所有文件、子目录及子目录里的目录和文件。
语法格式
tree [option] [directory]
tree 【选项】 【目录】
选项说明
| 参数选项 | 解释说明 |
|---|---|
| -d | 只显示目录 |
| -f | 显示每个文件的全路径 |
| -L level | 遍历目录的最大层数,level为大于0的正整数 |
| -a | 显示所有文件,包括隐藏文件 |
使用范例
不带任何参数执行tree明了
[root@test1 bridge]# tree
.
├── 3.txt
├── a
│ ├── a1.txt
│ ├── a2.txt
│ ├── a3.txt
│ ├── a4.txt
│ └── a5.txt
├── d
├── e
└── f
4 directories, 6 files
使用-d参数只显示目录
[root@test1 bridge]# tree -d
.
├── a
├── d
├── e
└── f
4 directories
使用-f参数显示每个文件的全路径
[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
使用-L 1参数遍历目录的第一层
[root@test1 bridge]# tree -L 1
.
├── 3.txt
├── a
├── d
├── e
└── f
4 directories, 1 file
使用-a参数显示所有文件(包括隐藏文件)
[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
命令练习
1.使用命令行展开功能,创建/tmp/a1, /tmp/a2, /tmp/a1/a, /tmp/a1/b,在/tmp目录下创建目录:x_y, x_z, q_y, q_z
[root@test1 ~]# mkdir -p /tmp/a1/{a,b} /tmp/a2
[root@test1 ~]# tree /tmp/
/tmp/
├── a1
│ ├── a
│ └── b
└── a2
4 directories, 0 files
2.文件的元数据信息有哪些,分别表示什么含义,如何查看?如何修改文件的时间戳信息。
查看文件元数据使用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: -
- File:文件名
- Size:文件大小
- Blocks:占用block数量
- IO Block:IO块大小
- regular empty file:文件类型为普通空文件
- Device:设备编号的十六进制(h)和十进制(d)
- Inode:文件的inode值
- Links:文件的硬链接数
- Access:文件权限
- UID和Gid:用户和用户组
- Access:最后访问文件的时间(读取文件内容)
- Modify:最后修改文件的时间(改变文件内容)
- Change:最后改变文件状态的时间(元数据发生改变)
修改文件时间戳可以使用touch命令
3.在/tmp目录下创建以tfile开头,后跟当前日期和时间的文件,文件名形如:tfile-2016-05-27-09-32-22。
[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
4.复制/etc目录下所有以p开头,以非数字结尾的文件或目录到/tmp/mytest1目录中。
[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
5.创建用户tom,指定UID为5001,指定家目录为/tmp/tom, 指定shell为/bin/zsh, 指定基本组为tom,附加组为jack.
[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)

浙公网安备 33010602011771号