前言:本篇是 Linux 基本操作篇章的内容!
笔者使用的环境是基于腾讯云服务器:CentOS 7.6 64bit。
学习集:
目录索引:
1. 基本语法及功能
2. 注意点说明
3. 创建文件测试
- - 3.1 不指定路径创建文件
- - 3.2 指定路径创建目录
- - 3.3 指定在其他路径下创建文件
4. 修改文件创建时间
- - 4.1 基本操作
- - 4.2 测试:重建式的修改,会影响文件中的内容吗?
- - 4.3 关于 [-a / -m] 可选选项修改存取/更改时间测试
5. 相关文章或系列推荐
1. 基本语法及功能
语法:
- touch [选项]… 文件…
功能:
- touch命令参数可更改文档或目录的日期时间,包括存取时间和更改时间,或者新建一个不存在的文件。
2. 注意点说明
- touch 命令只能用于创建普通文件,不能创建目录!!!
- touch 命令可以修改文档或目录的日期时间,包括存、取时间和更改时间(access、modify、change)。
3. 常用选项及测试
以下示例为同一环境操作,故前后具有测试连续性!
可选路径创建文件测试:
- 不指定路径创建文件:即在当前所在目录创建文件
- 指定路径创建文件:即在指令目录下创建文件
- 是否可以指定在其他路径下创建文件?可以
3.1 不指定路径创建文件
/* 查看当前所在目录 */
[Mortal@VM-12-16-centos ~]$ pwd
/home/Mortal
[Mortal@VM-12-16-centos ~]$ cd StudyingOrder_Linux/
/* 创建测试目录 */
[Mortal@VM-12-16-centos StudyingOrder_Linux]$ mkdir test_touch
[Mortal@VM-12-16-centos StudyingOrder_Linux]$ ls
test_touch test.txt
/* 不指定路径创建文件 */
[Mortal@VM-12-16-centos StudyingOrder_Linux]$ touch test1.txt
[Mortal@VM-12-16-centos StudyingOrder_Linux]$ ls
test1.txt test_touch test.txt
3.2 指定路径创建目录
选项作用:以时间排序。(按最新修改时间排序)
/* 指定路径创建目录 */
[Mortal@VM-12-16-centos StudyingOrder_Linux]$ touch test_touch/test2.txt
[Mortal@VM-12-16-centos StudyingOrder_Linux]$ ls
test1.txt test2.txt test_touch test.txt
[Mortal@VM-12-16-centos StudyingOrder_Linux]$ ls test_touch/
test2.txt
3.3 指定在其他路径下创建文件
[Mortal@VM-12-16-centos StudyingOrder_Linux]$ ls /home/Mortal
StudyingOrder_Linux test1 test2 test3
[Mortal@VM-12-16-centos StudyingOrder_Linux]$ touch /home/Mortal/test1/testtxt.txt
[Mortal@VM-12-16-centos StudyingOrder_Linux]$ ls /home/Mortal/test1
testtxt.txt
4. 修改文件创建时间
4.1 基本操作测试
修改文件创建/修改时间(重建同名文件):
- 提问:若文件中已有内容,重建式的修改,会影响文件中的内容吗?不会
/* 查看上例中 StudyingOrder_Linux/test.txt 的创建/修改时间 */
[Mortal@VM-12-16-centos StudyingOrder_Linux]$ ll test.txt
-rw-rw-r-- 1 Mortal Mortal 0 May 26 15:17 /home/Mortal/StudyingOrder_Linux/test.txt
/* test.txt 文件的创建时间为:May 26 15:17 */
/* 重新创建同名文件并查看创建/修改时间 */
[Mortal@VM-12-16-centos StudyingOrder_Linux]$ touch test.txt
[Mortal@VM-12-16-centos StudyingOrder_Linux]$ ll test.txt
-rw-rw-r-- 1 Mortal Mortal 0 May 26 16:44 /home/Mortal/StudyingOrder_Linux/test.txt
/* 新时间:May 26 16:44 */
4.2 重建式的修改,会影响文件中的内容吗?
/* 重建式的修改,会影响文件中的内容吗?不会 */
[Mortal@VM-12-16-centos StudyingOrder_Linux]$ echo "hello" > text.txt /* 重定向方式像文件中写入信息 */
[Mortal@VM-12-16-centos StudyingOrder_Linux]$ cat text.txt /* cat:查看指定文件中的内容! */
hello
[Mortal@VM-12-16-centos StudyingOrder_Linux]$ touch text.txt
[Mortal@VM-12-16-centos StudyingOrder_Linux]$ cat text.txt
hello
4.3 关于 [-a / -m] 可选选项修改存取/更改时间测试
-a 或–time=atime或–time=access或–time=use只更改存取时间;
-m 或–time=mtime或–time=modify 只更改变动时间。
(可能有点出入!)
/* stat + 文件:查看文件的操作时间 */
[Mortal@VM-12-16-centos StudyingOrder_Linux]$ stat text.txt
File: ‘text.txt’
Size: 6 Blocks: 8 IO Block: 4096 regular file
Device: fd01h/64769d Inode: 657455 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1001/ Mortal) Gid: ( 1001/ Mortal)
Access: 2023-05-26 16:51:49.780719889 +0800 /* 显示时间点:存取 */
Modify: 2023-05-26 16:51:48.250722863 +0800 /* 显示时间点:更改 */
Change: 2023-05-26 16:51:48.250722863 +0800 /* 显示时间点:修改 */
Birth: -
[Mortal@VM-12-16-centos StudyingOrder_Linux]$ touch -a text.txt
[Mortal@VM-12-16-centos StudyingOrder_Linux]$ stat text.txt
File: ‘text.txt’
Size: 6 Blocks: 8 IO Block: 4096 regular file
Device: fd01h/64769d Inode: 657455 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1001/ Mortal) Gid: ( 1001/ Mortal)
Access: 2023-05-26 16:55:50.785251530 +0800 /* 显示时间点:存取(变动) */
Modify: 2023-05-26 16:51:48.250722863 +0800 /* 显示时间点:更改 */
Change: 2023-05-26 16:55:49.252254509 +0800 /* 显示时间点:修改(变动) */
Birth: -
[Mortal@VM-12-16-centos StudyingOrder_Linux]$ touch -m text.txt
[Mortal@VM-12-16-centos StudyingOrder_Linux]$ stat text.txt
File: ‘text.txt’
Size: 6 Blocks: 8 IO Block: 4096 regular file
Device: fd01h/64769d Inode: 657455 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1001/ Mortal) Gid: ( 1001/ Mortal)
Access: 2023-05-26 16:56:04.788224316 +0800 /* 显示时间点:存取(变动) */
Modify: 2023-05-26 16:56:02.851228080 +0800 /* 显示时间点:更改 */
Change: 2023-05-26 16:56:02.851228080 +0800 /* 显示时间点:修改(变动) */
Birth: -
5. 相关文章或系列推荐
2. Linux :: 【基础指令篇 :: 文件及目录操作:(4)】:: mkdir :: 创建目录:指定路径单个目录创建及一次性创建多级目录 ;