13 Useful Examples of touch Command in Linux

In this article, I will take you through the usages of touch command in Linux. touch command allows you to update File timestamps. It is very much helpful in creating scripts and creating empty files. You might have seen some instances where you have been told to change the timestamp of a file due to various reasons in your Company. Here Linux comes for our rescue by providing a very easy but a very powerful tool called touch. We will see the various ways through which File date and timestamp can be changed through touch command as we go through.

13 Useful Examples of touch Command in Linux 1

touch command in Linux

Also Read: An Introduction to Javascript

1. Create an Empty file using touch Command

You can use simple touch file.txt command to create a file file.txt in your System.

root@localhost:~# touch file.txt

Now check if the file is created or not.

root@localhost:~# ls -ltr file.txt
-rw-r--r-- 1 root root 0 Jan 24 15:09 file.txt

2. Create Multiple files using touch command in Linux

If you want to create multiple files simultaneously, you can do that by using touch file.txt file1.txt file2.txt command.

root@localhost:~# touch file.txt file1.txt file2.txt

Check if the file is created or not.

root@localhost:~# ls -lrt file.txt file1.txt file2.txt
-rw-r--r-- 1 root root 0 Jan 24 15:11 file.txt
-rw-r--r-- 1 root root 0 Jan 24 15:11 file2.txt
-rw-r--r-- 1 root root 0 Jan 24 15:11 file1.txt

3. How to create a file using touch command in Linux

You can use touch command in linux to create files with contents in it. Here I am writing sequence 1 to 100 in file.txt using seq 1000 > file.txt command.

Advertisements
root@localhost:~# seq 1000 > file.txt

Verify the file creation by ls -ltr file.txt command.

root@localhost:~# ls -ltr file.txt
-rw-r--r-- 1 root root 3893 Jan 24 14:51 file.txt

4. Replicate file access time using touch command in Linux

You can replicate one file access to another using -r option. Here we will replicate file.txt access to file1.txt using touch -r file.txt file1.txt command.

root@localhost:~# touch -r file.txt file1.txt

Verify file access replication by using ls -lrt file.txt file1.txt command. You can notice from the output that file1.txt has exact same permission of file.txt.

root@localhost:~# ls -lrt file.txt file1.txt
-rw-r--r-- 1 root root 0 Jan 24 15:11 file.txt
-rw-r--r-- 1 root root 0 Jan 24 15:11 file1.txt

5. Modify File date and timestamp to the Latest

You can modify the file access date and timestamp to the latest date and timestamp using -a option. To modify Comma.txt date and timestamp to the latest use touch -a Comma.txt command.

Check Comma.txt access time before running touch Command.

root@localhost:~# ls -l Comma.txt --time=atime
-rw-r--r-- 1 root root 901 Jan 21 12:30 Comma.txt

Modify date and timestamp.

root@localhost:~# touch -a Comma.txt

Check file access time again after running touch command as shown below.

root@localhost:~# ls -l Comma.txt --time=atime
-rw-r--r-- 1 root root 901 Jan 24 15:26 Comma.txt

 

6. Modify File timestamp to the Latest timestamp

If you want to provide the current timestamp on your file, you need to use -m option with find command in Linux.

Check timestamp of Comma.java file before running touch command.

root@localhost:~# ls -ltr Comma.java
-rw-r--r-- 1 root root 162 Jan 21 12:30 Comma.java

Modify the timestamp using below command.

root@localhost:~# touch -m Comma.java

Check Comma.java file after running touch command in Linux.

root@localhost:~# ls -ltr Comma.java
-rw-r--r-- 1 root root 162 Jan 24 15:17 Comma.java

7. Change both date and time to the Latest

First you need to check file date and timestamp before running touch command. You can check that by using stat file command. This command will show you the file access, modify and change time.

[root@localhost ~]# stat file
  File: ‘file’
  Size: 3893            Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 8504603     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-01-25 14:39:31.079518741 -0500
Modify: 2020-01-25 14:39:31.079518741 -0500
Change: 2020-01-25 14:39:31.079518741 -0500
 Birth: -

Run touch command with -am option to modify date and timestamp of file.

[root@localhost ~]# touch -am file

Verify date and timestamp of file after running above touch command.

[root@localhost ~]# stat file
  File: ‘file’
  Size: 3893            Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 8504603     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-01-25 14:41:29.105393148 -0500
Modify: 2020-01-25 14:41:29.105393148 -0500
Change: 2020-01-25 14:41:29.105393148 -0500
 Birth: -

8. Check if the file Exists or not

Here we are checking if the file.txt exists or not before using -c option with touch command.

[root@localhost ~]# ls
1908 23 Aug anaconda-ks.cfg

Now you can run touch -c file.txt command to not create file.txt if the file does not exist.

root@localhost:~# touch -c file.txt

Verify after running touch command. You can notice from the output that file.txt is not created.

[root@localhost ~]# ls
1908 23 Aug anaconda-ks.cfg

9. Change date and time of a file

You can also manually change date and time of a file by using -c and -t option with touch command.

Let’s check the date and timestamp of file.txt before running touch command.

root@localhost:~# ls -ltr file.txt
-rw-r--r-- 1 root root 0 Jan 24 15:31 file.txt

Now let’s change the date to 12th August and time to 10:22 which is actually a future time.

[root@localhost ~]# touch -c -t 202008121022 file.txt

Notice in the output, you can see the date but not the time. That is because touch command ignores any future given time and does not show any timestamp.

[root@localhost ~]# ls -ltr file.txt
-rw-r--r-- 1 root root 46 Aug 12 2020 file.txt

10. Change date of a file

If you only want to change the date of file.txt, then you can do that by using -d option with touch command.

Here you can check the date of file.txt before running touch command.

root@localhost:~# ls -ltr file.txt
-rw-r--r-- 1 root root 0 Aug 12 2020 file.txt

Now you change the date of file.txt to 20 Aug from 12 August.

root@localhost:~# touch -d '20 Aug' file.txt

Check if you are able to see the modified date or not.

root@localhost:~# ls -ltr file.txt
-rw-r--r-- 1 root root 0 Aug 20 2020 file.txt

11. Change timestamp of a file

If you want to change the timestamp of a file file.txt, you can do that by running touch -d '12:30' file.txt command. This command will change the timestamp of file.txt to 12:30.

root@localhost:~# ls -ltr file.txt
-rw-r--r-- 1 root root 0 Aug 20 2020 file.txt

Here also you can use the same -d option to change your time like you used this above to change the date of a file.

root@localhost:~# touch -d '12:30' file.txt

You can check in the output, it is showing 12:30 timestamp on file.txt.

root@localhost:~# ls -ltr file.txt
-rw-r--r-- 1 root root 0 Jan 24 12:30 file.txt

12. Change date of a file using –date Option

There is another way you can often see in use to change the date of a file by using --date option. Here you can pass the arguments like yesterday to change your date instead of specifically assigning yesterday’s date.

[root@localhost ~]# touch --date="yesterday" file.txt

Now check if the date is changed to yesterday’s date or not.

[root@localhost ~]# ls -lrt file.txt
-rw-r--r-- 1 46 Jan 24 13:16 file.txt

13. Check Other touch Command Options 

If you want to check what other options are available with touch command, you can always run touch --help command and check that.

[root@localhost ~]# touch --help
Usage: touch [OPTION]... FILE...
Update the access and modification times of each FILE to the current time.

A FILE argument that does not exist is created empty, unless -c or -h
is supplied.

A FILE argument string of - is handled specially and causes touch to
change the times of the file associated with standard output.

................................................................................................................

 

Reference: Linux Boot Process

 

Popular Searches

  • How to create a file in Linux
  • linux touch
  • linux create file
Advertisements

Leave a Reply