关于linux:Linux-之-touch-命令

4次阅读

共计 1704 个字符,预计需要花费 5 分钟才能阅读完成。

touch用来创立文件, 用来批改文件的工夫戳。

命令格局

touch [选项]... 文件...

命令参数

  • -a 或 –time=atime 或 –time=access 或 –time=use 只更改存取时间。
  • -c 或 –no-create 不建设任何文档。
  • -d 应用指定的日期工夫,而非当初的工夫。
  • -f 此参数将疏忽不予解决,仅负责解决 BSD 版本 touch 指令的兼容性问题。
  • -m 或 –time=mtime 或 –time=modify 只更改变动工夫。
  • -r 把指定文档或目录的日期工夫,通通设成和参考文档或目录的日期工夫雷同。
  • -t 应用指定的日期工夫,而非当初的工夫。

命令性能

touch 命令参数可更改文档或目录的日期工夫,包含存取时间和更改工夫。

创立不存在的目录

创立一个 1.txt 文件
> touch 1.txt 
同时创立 2.txt 3.txt 文件
> touch 2.txt 3.txt

将 5.txt 的 Access,Modify 工夫改成和 1.txt 一样

> touch -r 1.txt 5.txt
> ls 
-rw-r--r-- 1 root root 0 Feb  3 23:17 1.txt
-rw-r--r-- 1 root root 0 Feb  3 23:17 5.txt

批量创立有法则的文件

创立 file1.txt file2.txt …. file10.txt

> touch file{1..10}.txt

创立文件并指定文件的工夫戳

> touch -t 202102031111 3.txt
> ls -al
-rw-r--r-- 1 root root 0 Feb  3 11:11 3.txt

将 5.txt 的工夫改成 2 天前

> ls -al 5.txt
-rw-r--r-- 1 root root  0 Feb  3 23:17 5.txt
> touch -d "2 days ago" 5.txt
> ls -al 5.txt
> ls
-rw-r--r-- 1 root root 0 Feb  1 23:29 5.txt

只批改 1.txtModifyChange 的工夫

> stat 1.txt 
  File:‘1.txt’Size: 5               Blocks: 8          IO Block: 4096   regular file
Device: fd01h/64769d    Inode: 101371574   Links: 1
Access: (0644/-rw-r--r--)  Uid: (0/    root)   Gid: (0/    root)
Access: 2021-02-03 23:39:45.258947600 +0800
Modify: 2021-02-03 23:40:10.462066771 +0800
Change: 2021-02-03 23:40:10.462066771 +0800
 Birth: -
> touch -m 1.txt
> stat 1.txt
stat 1.txt 
  File:‘1.txt’Size: 5               Blocks: 8          IO Block: 4096   regular file
Device: fd01h/64769d    Inode: 101371574   Links: 1
Access: (0644/-rw-r--r--)  Uid: (0/    root)   Gid: (0/    root)
Access: 2021-02-03 23:39:45.258947600 +0800
Modify: 2021-02-03 23:40:53.068649293 +0800
Change: 2021-02-03 23:40:53.068649293 +0800
 Birth: -

为什么 linux 创立文件是 touch 而不是 create

touch — change file access and modification times (BSD)
touch — change file timestamps (GNU)

touch 的作用原本不是创立文件,而是将指定文件的批改工夫设置为以后工夫。就是伪装“碰”(touch)了一下这个文件,伪装文件被“批改”了,于是文件的批改工夫就是被设置为以后工夫。这带来了一个副作用,就是当 touch 一个不存在的文件的时候,它会创立这个文件。而后,因为 touch 曾经能够实现创立文件的性能了,就不再须要一个独自的 create 了。

原文链接:https://rumenz.com/rumenbiji/linux-touch.html
微信公众号: 入门小站

正文完
 0