linux touch命令
Linux touch
command is used to create files and change files date time related attributes like access time etc. Creating empty files is most popular use case by the way. In this tutorial we will look how to use different features of touch
command.
Linux touch
命令用于创建文件和更改文件日期时间相关的属性,例如访问时间等。顺便说一下,创建空文件是最流行的用例。 在本教程中,我们将研究如何使用touch
命令的不同功能。
创建空文件 (Create Empty File)
The most popular usage of touch
command is creating empty files. For this option we do not need to provide any option. We will just put file name we want to create. In this example we will create an empty file named file1
.
touch
命令最流行的用法是创建空文件。 对于此选项,我们不需要提供任何选项。 我们将只放入我们要创建的文件名。 在此示例中,我们将创建一个名为file1
的空文件。
$ touch file1
创建多个空文件 (Create Multiple Empty Files)
In previous example we have created a single empty file. What if we need multiple files to be created in a single command shot. touch
supports multiple file creation. In this file we will create multiple files named file2
, file3
, file4
.
在前面的示例中,我们创建了一个空文件。 如果我们需要在单个命令镜头中创建多个文件怎么办。 touch
支持创建多个文件。 在此文件中,我们将创建多个名为file2
, file3
, file4
。
$ touch file2 file2 file3
创建名为A..Z的文件 (Create Files Named A..Z)
We can provide multiple file names to the touch
to create them in a empty state. But providing them one by one if they are structures is trivial task. We can use bash feature to expand given ile names. In this example we will create files named A
, B
, C
, … , Z
.
我们可以向提供多个文件名touch
的空状态来创建它们。 但是,如果它们是结构,则一一提供它们是微不足道的任务。 我们可以使用bash功能扩展给定的文件名。 在此示例中,我们将创建名为A
, B
, C
,…, Z
。
$ touch {A..Z}
创建名为1…100的文件(Create Files Named 1…100)
Another example to create multiple files is file names 1
to 100
.
创建多个文件的另一个示例是文件名1
到100
。
$ touch {1..100}
指定多个文件扩展名(Specify Multiple Files Extension)
We may need too specify the extension of files we are creating. In this example we will create files from 1
to 100
those file extension is txt
.
我们可能还需要指定正在创建的文件的扩展名。 在此示例中,我们将创建1
到100
文件扩展名为txt
文件。
$ touch {1..100}.txt
更改文件的文件访问权限(Change File Access of File)
File system hold information like last access a about files. touch
command can change these values easily. We will use -a
option to update last access time to the current time.
文件系统保留信息,例如关于文件的最后访问权限。 touch
命令可以轻松更改这些值。 我们将使用-a
选项将上次访问时间更新为当前时间。
$touch -a file1
更改文件的修改时间 (Change Modification Time of File)
We can also update last modification time to the current time with the -m
option like below.
我们还可以使用-m
选项将上次修改时间更新为当前时间,如下所示。
$ touch -m file1
不要覆盖 (Do Not Overwrite)
By default whether the given file exist newfile is created and if a file with the same name exist it is overwritten. We can prevent this behavior with -c
option like below. If file exist it will not created by touch
.
默认情况下,将创建给定文件是否存在newfile,并且如果存在同名文件,则将其覆盖。 我们可以使用-c
选项防止此行为,如下所示。 如果文件存在,则不会通过touch
创建。
$touch -c file1
设置文件访问时间 (Set Access Time of File)
In previous examples we have updated last access time of the file to the current time. We also have the ability to set different times for the last acess time. We will use -a
option and provide the date and time information. In this example we will set the last access time of file1
to January 02 15:45 of current year. It’s format is MMDDHHMM.
在前面的示例中,我们将文件的上次访问时间更新为当前时间。 我们还可以为上次访问时间设置不同的时间。 我们将使用-a
选项并提供日期和时间信息。 在此示例中,我们将file1
1的最后访问时间设置为当年的1月2日15:45。 格式为MMDDHHMM。
$ touch -c -a 01021545 file1
设置文件修改时间 (Set Modification Time of File)
Like previous example we can set last modification date of a file with -m
option.
与前面的示例一样,我们可以使用-m
选项设置文件的最后修改日期。
$ touch -c -m 01021545 file1
使用其他文件时间戳 (Use Other File Timestamp)
We can use other files date , time and timestamp information while setting newly created file. We will -r
option and the file we want to inherit its access and modification timestamp. In this example we will use file2
last access and modification timestamp for newly created file1
.
在设置新创建的文件时,我们可以使用其他文件的日期,时间和时间戳信息。 我们将使用-r
选项和要继承其访问和修改时间戳记的文件。 在此示例中,我们将对新创建的file1
使用file2
最后访问和修改时间戳。
$ touch -r file2 file1
linux touch命令