iT邦幫忙

第 12 屆 iThome 鐵人賽

0
自我挑戰組

30天菜鳥學 Linux 系列 第 35

35天-研究 touch 命令

  • 分享至 

  • xImage
  •  

進度 : 鳥哥的 Linux 私房菜 -- 第六章、Linux 檔案與目錄管理
6.3.5 修改檔案時間或建置新檔: touch

查看 touch 用法

test@test:~$ touch --help
Usage: touch [OPTION]... FILE...
Update the access and modification times of each FILE to the current time.

A FILE argument that does not exist is created empty, unless -c or -h
is supplied.

A FILE argument string of - is handled specially and causes touch to
change the times of the file associated with standard output.

Mandatory arguments to long options are mandatory for short options too.
  -a                     change only the access time
  -c, --no-create        do not create any files
  -d, --date=STRING      parse STRING and use it instead of current time
  -f                     (ignored)
  -h, --no-dereference   affect each symbolic link instead of any referenced
                         file (useful only on systems that can change the
                         timestamps of a symlink)
  -m                     change only the modification time
  -r, --reference=FILE   use this file's times instead of current time
  -t STAMP               use [[CC]YY]MMDDhhmm[.ss] instead of current time
      --time=WORD        change the specified time:
                           WORD is access, atime, or use: equivalent to -a
                           WORD is modify or mtime: equivalent to -m
      --help     display this help and exit
      --version  output version information and exit

Note that the -d and -t options accept different time-date formats.

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Full documentation at: <https://www.gnu.org/software/coreutils/touch>
or available locally via: info '(coreutils) touch invocation'

# 測試修改時間的變化
test@test:~$ touch test.txt
test@test:~$ stat test.txt 
  File: test.txt
  Size: 5         	Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 262158      Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/    test)   Gid: ( 1000/    test)
Access: 2020-10-08 01:02:29.734259550 +0000
Modify: 2020-10-08 01:02:29.734259550 +0000
Change: 2020-10-08 01:02:29.738259559 +0000


test@test:~$ echo 'test' > test.txt
test@test:~$ stat test.txt 
  File: test.txt
  Size: 5         	Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 262158      Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/    test)   Gid: ( 1000/    test)
Access: 2020-10-08 01:02:29.734259550 +0000
Modify: 2020-10-08 01:05:33.402647488 +0000
Change: 2020-10-08 01:05:33.402647488 +0000
 Birth: -

# 測試修改時間
test@test:~$ touch -d "2020-01-01" test.txt 
test@test:~$ stat test.txt 
  File: test.txt
  Size: 5         	Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 262158      Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/    test)   Gid: ( 1000/    test)
Access: 2020-01-01 00:00:00.000000000 +0000
Modify: 2020-01-01 00:00:00.000000000 +0000
Change: 2020-10-08 01:27:21.710675139 +0000
 Birth: -

鳥哥提到重要概念 :

複製一個檔案時,複製所有的屬性,但也沒有辦法複製 ctime 這個屬性的
我們平時看的檔案屬性中,比較重要的還是屬於那個 mtime 啊


上一篇
34天-檔案與目錄管理
下一篇
36天-bash 如何像 SQL 一樣 group by 加 sum 資料
系列文
30天菜鳥學 Linux 59
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言