touch command in Linux with examples

Let’s learn about the touch command in Linux with examples which is mainly used to make the empty file (zero byte file).

We can also use this command to change the access time, we set our own timestamp.

So, let’s start with our first example and also look into the option in the touch command.

Create a file without any content “0 byte File”

Command:
# touch tastethelinux

So, we have created a single empty file named as tastethelinux.

How to create multiple files without any content “0 byte File”

So, let’s create multiple files with name file1, file2, and file3 using the touch command.

Command:
# touch file1 file2 file3
How to create multiple files without any content using an expression

So, if we have to create multiple files with the same suffix or prefix, then we will use expressions, let’s see the example

Command:
# touch test{1..10}

So, we have used the expression test{1..10}, this will create test1 to test10 file in that current directory.

Same if you have to create a file like atest, btest, ctest, and dtest.

So we will use the expression like {a..d}test this will create our above files.

Command:
# touch {a..d}test

How to change Access time of a File using touch command in Linux

So, guys let’s change the Access time of a tastethelinux file with -a option.

Command:
# touch -a tastethelinux

Here first we have used a stat command to check the Access time, and after using touch command with -a option, it has taken the access time of your System.

How to change the Modification time of a File using touch command in Linux

So, we have changed the access time, let’s change modification time with option -m.

Command:
# touch -m file1

So, we have to set the current time for file1 and used -m option to change the modification time.

How to change the timestamp of a File using touch command in Linux

But what if we have to set the time with our needs, so let’s use -t option for the timestamp.

Command:
# touch -t 202005301200 timestamp_file

So, we can see that our file timestamp got changed successfully, let’s bifurcate the timestamp 202005301200.

202005301200, 2020 is the year, 05 is the month, 30 is the day, 12 is the hour, and 00 is the minutes.

How to set the file time instead of the current time of a File.

So, what if we have to set the timestamp of some other file, then we will use -r option.

Command:
# touch -r file1 tastethelinux

So, we will use the reference timestamp of the file1 and set it to the tastethelinux file.

How to change only DATE of a File using touch command.

Now we will only change the date by -d option, so let’s move forward and look into it.

Command:
# touch -d '05 May' file1

So, we can see that the file1 has a date of 31 May and we have set that to 05 May.

How to change only the TIME of a File using the touch command.

So, what if we have to change the time of the particular file by using -d option.

Command:
# touch -d '10:00' file2

So, we have used -d option with the hours and minutes with the file2.

Cheatsheet for a touch command
create a file without any content "0 byte File"

Syntax:
#touch [file_name]

Example:
# touch tastethelinux
How to create multiple files without any content "0 byte File"

Syntax:
#touch [file1] [file2] [file3].....[filen] 

Example:
# touch file1 file2 file3
How to create multiple files without any content using an expression

Syntax:
# touch prefix{expresion}
# touch {expression}suffix

Example:
# touch test{1..10}
#touch {1..10}test
How to change Access time of a File using touch command in Linux

Syntax:
# touch -a [file]

Example:
# touch -a tastethelinux
How to change the Modification time of a File using touch command in Linux

Syntax:
# touch -m [file]

Example:
# touch -m tastethelinux
How to change the timestamp of a File using touch command in Linux

Syntax:
# touch -t [[CC]YY]MMDDhhmm[.ss] [file]

Example:
# touch -t 202005301200 timestamp_file
How to use the file time instead of the current time of a File.

Syntax:
# touch -r [file_timestamp_that_we_want_in_another_file] [file_that_will_be_changed]

Example:
# touch -r file1 tastethelinux
How to change only DATE of a File using touch command.
Syntax:
# touch -d 'string' [file]

Example:
# touch -d '05 May' file1
How to change only TIME of a File using touch command.

Syntax:
# touch -d 'string' [file]

Example:
# touch -d '10:00' file2

[wp_quiz id=”2560″]

Want to learn about the Hardware information commands refer the link.

Thanks to read the POST keep sharing and keep Supporting us!

Give your valuable time