Linux touch Command Tutorial with – POFTUT

Linux touch Command Tutorial with


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.

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

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 file2 file3

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 {A..Z}
Create Files Named A..Z
Create Files Named A..Z

Create Files Named 1…100

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

$ touch {1..100}
Create Files Named 1...100
Create Files Named 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 .

$ 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 file1

Change Modification Time of File

We can also update last modification time to the current time with the -m option like below.

$ 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 .

 $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.

$ 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.

$ 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 .

$ touch -r file2 file1

 

LEARN MORE  How To Remove Files Older Than 1 Day/1 Week/1 Month Recursively

Leave a Comment