linux touch命令_Linux touch命令教程

linux touch命令

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支持创建多个文件。 在此文件中,我们将创建多个名为file2file3file4

$ 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功能扩展给定的文件名。 在此示例中,我们将创建名为ABC ,…, Z

$ touch {A..Z}
Create Files Named A..Z
Create Files Named A..Z
创建名为A..Z的文件

创建名为1…100的文件(Create Files Named 1…100)

Another example to create multiple  files is file names 1 to 100 .

创建多个文件的另一个示例是文件名1100

$ touch {1..100}
Create Files Named 1...100
Create Files Named 1…100
创建名为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 .

我们可能还需要指定正在创建的文件的扩展名。 在此示例中,我们将创建1100文件扩展名为txt文件。

$ touch {1..100}.txt
Specify Multiple Files Extension
Specify Multiple Files Extension
指定多个文件扩展名

更改文件的文件访问权限(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
LEARN MORE  PHP Glob() Function To Match Path, Directory, File Names with Examples
了解更多PHP Glob()函数以示例匹配路径,目录,文件名

翻译自: https://www.poftut.com/linux-touch-command-tutorial/

linux touch命令

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值