Linux Touch命令的详细信息

时间:2020-03-21 11:46:08  来源:igfitidea点击:

Unix中的touch命令和Linux/unix机器中的touch命令语法

其中我们通过示例解释了Linux中的touch命令

众所周知,在Unix/Linux中,每个文件都与时间戳相关联,并且我们还看到每个文件都存储了上次访问时间,上次修改时间和上次更改时间的信息。

这就是为什么每当用户在linux中创建新文件,访问或者修改现有文件时,该文件的时间戳都会自动更改,或者我们可以说它已更新。

每个使用linux/unix的人都使用了此命令。

大多数人都用它来创建一个空文件。
但这不仅用于创建一个空文件,还不止于此。
尽管这个主题与文件有关,但让我对UNIX/Linux中的文件概念进行一些介绍。

就像你们一样,所有人都听说过

“在UNIX/LINUX系统上,所有内容都是文件;如果不是文件,则表示进程.”

让我们对此声明做一个简短的说明,以使文件概念更清晰。

我发现这句话是真的bcz,因为我们都知道,还有一些特殊文件,而不仅仅是文件(命名管道和套接字),对吗?

但是,为了使这些事情保持简单,说Unix和Linux中的所有内容都是一个文件,这是可以接受的概括。

“ Linux就像UNIX,它在文件和目录之间没有区别.”

我希望你们在听到上述声明后不要感到震惊,尽管如此,我还是喜欢用一些简单的概念来震惊人们。

让我解释一下我对以上语句的看法:

因为目录只是一个包含其他文件名的文件。

程序,服务,文本,图像等都是文件。

根据系统,输入和输出设备(通常是所有设备)被视为文件。

现在,让我介绍一下Linux中我最喜欢的命令“ touch command”。

几乎每个linux/unix用户都使用过此命令,但只有很少几个人知道此命令的强度。

Linux本身非常强大,但是它的某些命令显示了真正的Linux实力。

touch命令示例说明:

1.创建一个空文件

#touch file1

此命令将创建一个名为file1的空文件。

2.在Linux中创建多个空文件

#touch  a  b  c  d  e  f   g   h
#touch  1 2 3 4 5 6 7 8 9

3.创建从A到Z的文件

#touch {A..Z}

4,从1到20一次创建20个文件。

#touch {1..20}

5,从1到1000创建1000个文本文件。

[root@localhost theitroad]# touch {1..1000}.txt
[root@localhost theitroad]# ls
1000.txt  182.txt  264.txt  346.txt  428.txt  50.txt   592.txt  674.txt  756.txt  838.txt  91.txt
100.txt   183.txt  265.txt  347.txt  429.txt  510.txt  593.txt  675.txt  757.txt  839.txt  920.txt
101.txt   184.txt  266.txt  348.txt  42.txt   511.txt  594.txt  676.txt  758.txt  83.txt   921.txt
102.txt   185.txt  267.txt  349.txt  430.txt  512.txt  595.txt  677.txt  759.txt  840.txt  922.txt
103.txt   186.txt  268.txt  34.txt   431.txt  513.txt  596.txt  678.txt  75.txt   841.txt  923.txt
104.txt   187.txt  269.txt  350.txt  432.txt  514.txt  597.txt  679.txt  760.txt  842.txt  924.txt
105.txt   188.txt  26.txt   351.txt  433.txt  515.txt  598.txt  67.txt   761.txt  843.txt  925.txt
106.txt   189.txt  270.txt  352.txt  434.txt  516.txt  599.txt  680.txt  762.txt  844.txt  926.txt
107.txt   18.txt   271.txt  353.txt  435.txt  517.txt  59.txt   681.txt  763.txt  845.txt  927.txt
108.txt   190.txt  272.txt  354.txt  436.txt  518.txt  5.txt    682.txt  764.txt  846.txt  928.txt
109.txt   191.txt  273.txt  355.txt  437.txt  519.txt  600.txt  683.txt  765.txt  847.txt  929.txt
10.txt    192.txt  274.txt  356.txt  438.txt  51.txt   601.txt  684.txt  766.txt  848.txt  92.txt
110.txt   193.txt  275.txt  357.txt  439.txt  520.txt  602.txt  685.txt  767.txt  849.txt  930.txt

6.以a1,a2,..... a1000之类的方式创建1000个文件

[root@localhost theitroad]# touch a{1..1000}

7,使用echo命令创建文件

[root@localhost theitroad]# echo {1..10}
1 2 3 4 5 6 7 8 9 10

8,我们可以通过触摸命令一次创建数百万个文件

#touch {1..1000}{1..1000}

现在,我将向我们介绍如何使用“文件”时间戳记。

为此,我将创建两个文件“ theitroad.txt”和“ sarath.txt”

然后,我们将注意到上次访问时间,上次修改时间和上次更改时间的信息。

然后,我们将使用touch命令创建,更改和修改文件的时间戳。

首先在这里创建theitroad.txt和sarath.txt文件。

[root@localhost theitroad]# touch theitroad.txt sarath.txt

[root@localhost theitroad]# ls
sarath.txt  theitroad.txt

[root@localhost theitroad]# stat theitroad.txt
  File: `theitroad.txt'
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 803h/2051d      Inode: 11272254    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/   root)   Gid: (    0/   root)
Access: 2013-01-16 16:02:37.000000000 +0530
Modify: 2013-01-16 16:02:37.000000000 +0530
Change: 2013-01-16 16:02:37.000000000 +0530

因此,我们可以清楚地看到stat命令的响应,因此可以看到任何文件的访问时间,修改时间和更改时间。

所有这三个都在文件时间戳记下。

而且touch命令可以更改时间戳和误导。
因此,我想说的是,如果入侵者可以访问系统,那么他/她就可以使用文件时间戳了。

让我们借助几个示例来开始解释这些时间戳的更改。

示例1:如何更改文件theitroad.txt的文件访问和修改时间

[root@localhost theitroad]# stat theitroad.txt
  File: `theitroad.txt'
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 803h/2051d      Inode: 11272254    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/   root)   Gid: (    0/   root)
Access: 2013-01-16 16:02:37.000000000 +0530
Modify: 2013-01-16 16:02:37.000000000 +0530
Change: 2013-01-16 16:02:37.000000000 +0530

[root@localhost theitroad]# touch -a theitroad.txt

上面的命令将在此处更改访问时间os文件theitroad.txt。

现在,要查看所做更改的静态信息,我们需要在此处使用stat命令的帮助...

[root@localhost theitroad]# stat theitroad.txt
  File: `theitroad.txt'
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 803h/2051d      Inode: 11272254    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/   root)   Gid: (    0/   root)
Access: 2013-01-16 16:12:09.000000000 +0530
Modify: 2013-01-16 16:02:37.000000000 +0530
Change: 2013-01-16 16:12:09.000000000 +0530
[root@localhost theitroad]#

我们可以看到访问时间已更改。

注意:如果文件尚不存在,它将创建一个新文件。

但是,如果我们不希望创建一个新文件(如果theitroad.txt文件不存在),请使用以下命令。

#touch -c theitroad.txt

它将避免创建新文件。

示例2:如何更改文件修改时间?

[root@localhost theitroad]# touch -m theitroad.txt

这将更改文件theitroad.txt的修改时间

[root@localhost theitroad]# touch -m *.txt

这将更改所有.txt文件的修改时间

[root@localhost theitroad]# stat theitroad.txt
  File: `theitroad.txt'
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 803h/2051d      Inode: 11272254    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/   root)   Gid: (    0/   root)
Access: 2013-01-16 16:12:09.000000000 +0530
Modify: 2013-01-16 16:18:03.000000000 +0530
Change: 2013-01-16 16:18:03.000000000 +0530

示例3:如何设置访问和修改时间?

#touch -c -t YYDDHHMM filename

例如:

#touch -c -t 12101820 theitroad.txt

此命令将访问和修改日期和时间设置为文件theitroad.txt,时间为2013年12月10日下午18:20。

我们可以通过以下方式查看访问和修改日期和时间

#ls -l command

[root@localhost theitroad]# touch -c -t 12101820 theitroad.txt

[root@localhost theitroad]# ls -l
total 0
-rw-r--r-- 1 root root 0 Jan 16 16:02 sarath.txt
-rw-r--r-- 1 root root 0 Dec 10  2013 theitroad.txt
[root@localhost theitroad]#

示例4:如何使用其他文件的时间戳

其中我将使用文件sarath.txt的时间戳

[root@localhost theitroad]# ls -l
total 0
-rw-r--r-- 1 root root 0 Jan 16 16:02 sarath.txt
-rw-r--r-- 1 root root 0 Dec 10  2013 theitroad.txt

[root@localhost theitroad]# touch -r theitroad.txt sarath.txt

上面的命令将使用sarath.txt文件的时间戳更新theitroad.txt文件的时间戳。

[root@localhost theitroad]# ls -l
total 0
-rw-r--r-- 1 root root 0 Dec 10  2013 sarath.txt
-rw-r--r-- 1 root root 0 Dec 10  2013 theitroad.txt

其中我已经通过文件sarath.txt的时间戳更新了theitroad.txt文件的时间戳

例5:用当前时间以外的指定时间创建文件

# touch -t YYMMDDHHMM.SS linux.txt

下面说明上述格式:

  • CC-年份的前两位数字
  • YY –年份的后两位数字。如果YY的值在70到99之间,则假定CC位为19. 如果YY的值在00到37之间,则CC位为20。
  • 不能将日期设置为2038年1月18日以后。
  • MM –月
  • DD –日期
  • 时–小时
  • 毫米–分钟
  • SS –秒

示例6:允许用户使用选项“ -d”和“ -t”添加特定的上次访问时间。

首先,请参阅可用于测试目的的文件列表。

[root@theitroad test]# ls
decryptedfile.zip  linux.txt   theitroad.txt
level6zip.des3     sarath.txt  theitroad.txt

现在我们要对theitroad.txt文件进行实验,因此让我们首先检查它的完整时间戳。

[root@theitroad test]# stat theitroad.txt
  File: `theitroad.txt'
  Size: 115467          Blocks: 240        IO Block: 4096   regular file
Device: 805h/2053d      Inode: 10311845    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/   root)   Gid: (    0/   root)
Access: 2013-05-04 23:12:31.000000000 -0400
Modify: 2013-04-19 05:26:32.000000000 -0400
Change: 2013-04-23 02:09:14.000000000 -0400

现在使用选项-d,我们将添加特定的上次访问时间。 [root@theitroad test]# touch -d "24 may" theitroad.txt现在,在进行更改后,请再次查看时间戳以比较先前时间戳输出的结果。

[root@theitroad test]# stat theitroad.txt
  File: `theitroad.txt'
  Size: 115467          Blocks: 240        IO Block: 4096   regular file
Device: 805h/2053d      Inode: 10311845    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/   root)   Gid: (    0/   root)
Access: 2013-05-24 00:00:00.000000000 -0400
Modify: 2013-05-24 00:00:00.000000000 -0400
Change: 2013-05-23 03:53:19.000000000 -0400
[root@theitroad test]#

当我们将theitroad.txt文件访问和修改时间的先前值的输出与当前访问和修改时间值进行比较时,我们将很容易看到更改。

注意:如果我们仅使用-d选项提供时间,则它将自动将日期更改为当前日期。

例子:

[root @ theitroad test] touch -d“ 15:50” theitroad.txt
我们在/var/spool/mail/root中有新邮件

[root@theitroad test]# stat theitroad.txt
  File: `theitroad.txt'
  Size: 115467          Blocks: 240        IO Block: 4096   regular file
Device: 805h/2053d      Inode: 10311845    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/   root)   Gid: (    0/   root)
Access: 2013-05-23 15:50:00.000000000 -0400
Modify: 2013-05-23 15:50:00.000000000 -0400
Change: 2013-05-23 04:08:46.000000000 -0400

如何同时将访问时间和修改时间同时更改为当前时间?

[root@theitroad test]# stat theitroad.txt
  File: `theitroad.txt'
  Size: 115467          Blocks: 240        IO Block: 4096   regular file
Device: 805h/2053d      Inode: 10311845    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/   root)   Gid: (    0/   root)
Access: 2013-05-23 15:50:00.000000000 -0400
Modify: 2013-05-23 15:50:00.000000000 -0400
Change: 2013-05-23 04:08:46.000000000 -0400

现在,使用选项“ a”和“ m”运行touch命令

在此,选项“ a”将更改访问时间。

和选项“ m”将更改文件的修改时间。 [root@theitroad test]# touch -am theitroad.txt现在,进行更改后,我们可以使用stat命令查看静态信息以检查命令是否正常运行。

[root@theitroad test]# stat theitroad.txt
  File: `theitroad.txt'
  Size: 115467          Blocks: 240        IO Block: 4096   regular file
Device: 805h/2053d      Inode: 10311845    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/   root)   Gid: (    0/   root)
Access: 2013-05-23 04:23:03.000000000 -0400
Modify: 2013-05-23 04:23:03.000000000 -0400
Change: 2013-05-23 04:23:03.000000000 -0400

如何使用文件的时间戳而不是当前时间?

其中我们将使用theitroad.txt文件的时间作为sarath.txt文件的时间。 [root@theitroad test]# touch -r theitroad.txt sarath.txt

[root@theitroad test]# stat sarath.txt theitroad.txt
  File: `sarath.txt'
  Size: 11470           Blocks: 24         IO Block: 4096   regular file
Device: 805h/2053d      Inode: 10311844    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/   root)   Gid: (    0/   root)
Access: 2013-05-23 04:23:03.000000000 -0400
Modify: 2013-05-23 04:23:03.000000000 -0400
Change: 2013-05-23 04:40:55.000000000 -0400
  File: `theitroad.txt'
  Size: 115467          Blocks: 240        IO Block: 4096   regular file
Device: 805h/2053d      Inode: 10311845    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/   root)   Gid: (    0/   root)
Access: 2013-05-23 04:23:03.000000000 -0400
Modify: 2013-05-23 04:23:03.000000000 -0400
Change: 2013-05-23 04:23:03.000000000 -0400

如何通过前进特定的秒数来修改文件的时间戳。 [root@theitroad test]# touch -r theitroad.txt F 50 sarath.txt上面的命令将使文件theitroad.txt比sarath.txt早50秒。
我们可以通过运行stat命令进行确认。

[root@theitroad test]# stat sarath.txt theitroad.txt
  File: `sarath.txt'
  Size: 11470           Blocks: 24         IO Block: 4096   regular file
Device: 805h/2053d      Inode: 10311844    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/   root)   Gid: (    0/   root)
Access: 2013-05-23 04:23:03.000000000 -0400
Modify: 2013-05-23 04:23:03.000000000 -0400
Change: 2013-05-23 04:46:07.000000000 -0400
  File: `theitroad.txt'
  Size: 115467          Blocks: 240        IO Block: 4096   regular file
Device: 805h/2053d      Inode: 10311845    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/   root)   Gid: (    0/   root)
Access: 2013-05-23 04:23:03.000000000 -0400
Modify: 2013-05-23 04:23:03.000000000 -0400
Change: 2013-05-23 04:23:03.000000000 -0400
[root@theitroad test]#

同样:以下带有选项“ B”的命令会使sarath.txt文件比theitroad.txt早50秒。

[root@theitroad test]# touch -r theitroad.txt B 50 sarath.txt

[root@theitroad test]# stat sarath.txt theitroad.txt
  File: `sarath.txt'
  Size: 11470           Blocks: 24         IO Block: 4096   regular file
Device: 805h/2053d      Inode: 10311844    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/   root)   Gid: (    0/   root)
Access: 2013-05-23 04:23:03.000000000 -0400
Modify: 2013-05-23 04:23:03.000000000 -0400
Change: 2013-05-23 04:52:27.000000000 -0400
  File: `theitroad.txt'
  Size: 115467          Blocks: 240        IO Block: 4096   regular file
Device: 805h/2053d      Inode: 10311845    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/   root)   Gid: (    0/   root)
Access: 2013-05-23 04:23:03.000000000 -0400
Modify: 2013-05-23 04:23:03.000000000 -0400
Change: 2013-05-23 04:23:03.000000000 -0400