Linux :: 【基础指令篇 :: 文件及目录操作:(5)】:: touch:创建普通文件、修改文件创建时间及其他基本操作

前言:本篇是 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. 注意点说明

  1. touch 命令只能用于创建普通文件,不能创建目录!!!
  2. 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. 相关文章或系列推荐

1. Linux 学习目录合集


2. Linux :: 【基础指令篇 :: 文件及目录操作:(4)】:: mkdir :: 创建目录:指定路径单个目录创建及一次性创建多级目录


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NPC的白话文谈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值