首頁 > 軟體

Linux常用基本命令:touch命令

2020-06-16 16:47:23

touch命令作用:建立空檔案,或者改變檔案的時間戳屬性

適用場景:

如果建立一個新檔案有很多種方法,比如:

  • vi file
  • cat > file
  • echo "hello,world" > file
  • touch  file

touch命令不僅可以建立新檔案,而且可以修改檔案的時間屬性;

格式: touch [option] [file]

備註:

1) -t選項的時間格式[[CC]YY]MMDDhhmm[.ss]說明:

2) touch命令的幾點說明:

1,同時建立一個或者多個空檔案

linuxidc@linuxidc:~/linux/mkdir$ ls
linuxidc@linuxidc:~/linux/mkdir$ touch a.txt
linuxidc@linuxidc:~/linux/mkdir$ ls
a.txt
linuxidc@linuxidc:~/linux/mkdir$ touch b.txt c.txt
linuxidc@linuxidc:~/linux/mkdir$ ls
a.txt  b.txt  c.txt
linuxidc@linuxidc:~/linux/mkdir$ touch {1..5}.txt
linuxidc@linuxidc:~/linux/mkdir$ ls
1.txt  2.txt  3.txt  4.txt  5.txt  a.txt  b.txt  c.txt

2,建立檔案,並用stat檢視檔案的屬性狀態資訊

linuxidc@linuxidc:~/linux/mkdir$ touch linuxidc.txt
linuxidc@linuxidc:~/linux/mkdir$ stat linuxidc.txt
  File: 'linuxidc.txt'
  Size: 0            Blocks: 0          IO Block: 4096  regular empty file
linuxidcice: 807h/2055d    Inode: 9569451    Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/ linuxidc)  Gid: ( 1000/ linuxidc)
Access: 2018-05-01 17:25:16.439856937 +0800
Modify: 2018-05-01 17:25:16.439856937 +0800
Change: 2018-05-01 17:25:16.439856937 +0800
 Birth: -

最後有3個時間

Access time: 最後存取時間,檢視檔案的內容時,這個時間會改變,使用cat命令檢視檔案時,可以看出Access time已經變了

linuxidc@linuxidc:~/linux/mkdir$ stat linuxidc.txt
  File: 'linuxidc.txt'
  Size: 0            Blocks: 0          IO Block: 4096  regular empty file
linuxidcice: 807h/2055d    Inode: 9569451    Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/ linuxidc)  Gid: ( 1000/ linuxidc)
Access: 2018-05-01 17:25:16.439856937 +0800
Modify: 2018-05-01 17:25:16.439856937 +0800
Change: 2018-05-01 17:25:16.439856937 +0800
 Birth: -
linuxidc@linuxidc:~/linux/mkdir$ cat linuxidc.txt
linuxidc@linuxidc:~/linux/mkdir$ stat linuxidc.txt
  File: 'linuxidc.txt'
  Size: 0            Blocks: 0          IO Block: 4096  regular empty file
linuxidcice: 807h/2055d    Inode: 9569451    Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/ linuxidc)  Gid: ( 1000/ linuxidc)
Access: 2018-05-01 17:27:51.887215139 +0800
Modify: 2018-05-01 17:25:16.439856937 +0800
Change: 2018-05-01 17:25:16.439856937 +0800
 Birth: -

Modify time:最後修改時間,修改檔案內容,檔案的修改時間會改變。存取時間和狀態改變時間 也發生變化

linuxidc@linuxidc:~/linux/mkdir$ stat linuxidc.txt
  File: 'linuxidc.txt'
  Size: 0            Blocks: 0          IO Block: 4096  regular empty file
linuxidcice: 807h/2055d    Inode: 9569451    Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/ linuxidc)  Gid: ( 1000/ linuxidc)
Access: 2018-05-01 17:27:51.887215139 +0800
Modify: 2018-05-01 17:25:16.439856937 +0800
Change: 2018-05-01 17:25:16.439856937 +0800
 Birth: -
linuxidc@linuxidc:~/linux/mkdir$ vim linuxidc.txt
linuxidc@linuxidc:~/linux/mkdir$ stat linuxidc.txt
  File: 'linuxidc.txt'
  Size: 5            Blocks: 8          IO Block: 4096  regular file
linuxidcice: 807h/2055d    Inode: 9569550    Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/ linuxidc)  Gid: ( 1000/ linuxidc)
Access: 2018-05-01 17:29:34.982228802 +0800
Modify: 2018-05-01 17:29:34.982228802 +0800
Change: 2018-05-01 17:29:35.018231392 +0800
 Birth: -

Change time:狀態改變時間,修改檔案內容,移動檔案,或者改變檔案屬性等,檔案的change time會發生改變.

1,移動檔案( 重新命名 ), change time 發生改變:

linuxidc@linuxidc:~/linux/mkdir$ stat linuxidc.txt
  File: 'linuxidc.txt'
  Size: 5            Blocks: 8          IO Block: 4096  regular file
linuxidcice: 807h/2055d    Inode: 9569550    Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/ linuxidc)  Gid: ( 1000/ linuxidc)
Access: 2018-05-01 17:29:34.982228802 +0800
Modify: 2018-05-01 17:29:34.982228802 +0800
Change: 2018-05-01 17:29:35.018231392 +0800
 Birth: -
linuxidc@linuxidc:~/linux/mkdir$ mv linuxidc.txt linuxidc2.txt
linuxidc@linuxidc:~/linux/mkdir$ stat linuxidc2.txt
  File: 'linuxidc2.txt'
  Size: 5            Blocks: 8          IO Block: 4096  regular file
linuxidcice: 807h/2055d    Inode: 9569550    Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/ linuxidc)  Gid: ( 1000/ linuxidc)
Access: 2018-05-01 17:29:34.982228802 +0800
Modify: 2018-05-01 17:29:34.982228802 +0800
Change: 2018-05-01 17:32:25.392863791 +0800

2,修改檔案的許可權,change time發生了改變

linuxidc@linuxidc:~/linux/mkdir$ stat linuxidc2.txt
  File: 'linuxidc2.txt'
  Size: 5            Blocks: 8          IO Block: 4096  regular file
linuxidcice: 807h/2055d    Inode: 9569550    Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/ linuxidc)  Gid: ( 1000/ linuxidc)
Access: 2018-05-01 17:29:34.982228802 +0800
Modify: 2018-05-01 17:29:34.982228802 +0800
Change: 2018-05-01 17:32:25.392863791 +0800
 Birth: -
linuxidc@linuxidc:~/linux/mkdir$ chmod 777 linuxidc2.txt
linuxidc@linuxidc:~/linux/mkdir$ stat linuxidc2.txt
  File: 'linuxidc2.txt'
  Size: 5            Blocks: 8          IO Block: 4096  regular file
linuxidcice: 807h/2055d    Inode: 9569550    Links: 1
Access: (0777/-rwxrwxrwx)  Uid: ( 1000/ linuxidc)  Gid: ( 1000/ linuxidc)
Access: 2018-05-01 17:29:34.982228802 +0800
Modify: 2018-05-01 17:29:34.982228802 +0800
Change: 2018-05-01 17:33:31.602584110 +0800
 Birth: -

touch -m修改檔案的modify和change時間

linuxidc@linuxidc:~/linux/mkdir$ stat linuxidc2.txt
  File: 'linuxidc2.txt'
  Size: 5            Blocks: 8          IO Block: 4096  regular file
linuxidcice: 807h/2055d    Inode: 9569550    Links: 1
Access: (0777/-rwxrwxrwx)  Uid: ( 1000/ linuxidc)  Gid: ( 1000/ linuxidc)
Access: 2018-05-01 17:29:34.982228802 +0800
Modify: 2018-05-01 17:29:34.982228802 +0800
Change: 2018-05-01 17:33:31.602584110 +0800
 Birth: -
linuxidc@linuxidc:~/linux/mkdir$ touch -m linuxidc2.txt
linuxidc@linuxidc:~/linux/mkdir$ stat linuxidc2.txt
  File: 'linuxidc2.txt'
  Size: 5            Blocks: 8          IO Block: 4096  regular file
linuxidcice: 807h/2055d    Inode: 9569550    Links: 1
Access: (0777/-rwxrwxrwx)  Uid: ( 1000/ linuxidc)  Gid: ( 1000/ linuxidc)
Access: 2018-05-01 17:29:34.982228802 +0800
Modify: 2018-05-01 17:34:31.678864985 +0800
Change: 2018-05-01 17:34:31.678864985 +0800

touch -d修改建立後的檔案的修改時間

linuxidc@linuxidc:~/linux/mkdir$ ls -lh linuxidc2.txt
-rwxrwxrwx 1 linuxidc linuxidc 5 5月  1 17:34 linuxidc2.txt
linuxidc@linuxidc:~/linux/mkdir$ touch -d 20190218 linuxidc2.txt
linuxidc@linuxidc:~/linux/mkdir$ ls -lh linuxidc2.txt
-rwxrwxrwx 1 linuxidc linuxidc 5 2月  18  2019 linuxidc2.txt
linuxidc@linuxidc:~/linux/mkdir$ stat linuxidc2.txt
  File: 'linuxidc2.txt'
  Size: 5            Blocks: 8          IO Block: 4096  regular file
linuxidcice: 807h/2055d    Inode: 9569550    Links: 1
Access: (0777/-rwxrwxrwx)  Uid: ( 1000/ linuxidc)  Gid: ( 1000/ linuxidc)
Access: 2019-02-18 00:00:00.000000000 +0800
Modify: 2019-02-18 00:00:00.000000000 +0800
Change: 2018-05-01 17:37:13.031347481 +0800
 Birth: -

Linux公社的RSS地址:https://www.linuxidc.com/rssFeed.aspx

本文永久更新連結地址https://www.linuxidc.com/Linux/2018-09/153844.htm


IT145.com E-mail:sddin#qq.com