touch command

touch command 
Use this command to change the access and modification time of the file. Creates an empty file if the file does not already exist.In this, what is the access and modification time and how do we change this time. Along with this, how to create empty files, you will know in detail about all these. 

*Access and modification time is also called time stamps.

1.what is the access time?

The last time the file was access or read .
We read the file through some commands like cat, vi and nano. Keep in mind that only you have to open it and not make any kind of change to the content inside it.   

2. What is modification time:-

The time when the content inside the file was last modified.
In this, we open the file and modify its content. 

3. Why are we use touch command ? 

We access and modify the file by opening it, the access and modification time also changes in its reflection. But without accessing and modifying the file, its access and modification time can be changed and this work is done with the help of touch command.

Now we will learn how to use touch command  
 
Syntax of touch command:

$touch  [option]  FILE_NAME 

We have a file named file.txt whose status is as shown in the picture. Taking this as the basis, we will learn about other operations:-





You may not be able to see the diagram clearly so I write it below 

Access: 2021-09-20  14:53:40
Modify: 2021-09-20. 15:00:59

0.touch  FILE_NAME 

When we use touch command without any option then access and modification time changes according to current time.

$touch new_filename

1.change access time:- 
You can change the access time of one or more files by using the -a option with the touch command.
 
$touch  -a  FILE_NAME 
previous access time :2021-09-20   14:53:40 
Previous modification time: 2021-09-20  15:00:59

current access time :2021-09-20 17:14:05 
Current modification time: 2021-09-20 15:00:09
Here we have only changed the access time.If you want to change the access time of more than one file, you can do as follows-

$touch  -a file1 file2 file3 .. .. ... .... 

2. change modification time:- 
This is an easy way to change the modification time without modifying any file. 

$touch -m FILE_NAME









previous access time: 2021-09-20  17:14:05
Previous modification time: 2021-09-20 15:00:09

Current access time : 2021-09-20 17:14:05
Current modification time: 2021-09-20  19:43:58

We have changed the modification time without modifying the file.If you want to keep the same modification time of more than one file, then you can do as follows :-

$touch -m file1 file2 file3 .. .. ... ....

3. touch --reference="file" FILE_NAME

where, file= The file whose time stamp is to be used for another file.
 We will not set the timestamps of the FILE_NAME equal to the current timestamp and use the timestamps of the file_name for the FILE_NAME. 

We are taking file.txt as a reference file. Its timestamp is as follows:- 








Now we will use the timestamp of file.txt for the timestamp of my.txt. 

$touch --reference="file.txt" my.txt











Wow ,As you can see in the diagram above, the access and modification time of my.txt is equal to the access and modification time of file.txt respectively.

4. touch  --date="DATE_STRING"  FILE_NAME 

Where DATE_STRING="yyyymmdd"
Or "yyyy/mm/dd" Or "yyyy-mm-dd"
Using --date option changes both the last access and the modification date depending on DATE_STRING.
If you want to change either of the two then you can use -m or  -a. something as follows:- 
For changing last modification date:-

$touch -m --date="DATE_STRING"  FILE_NAME

For changing last access date:-
$touch -a --date="DATE_STRING" FILE_NAME

5.how to create empty file 
All the operations we performed with all the above options, if the file does not already exist in it, then all of them create one or more empty files according to the number of input files.let us show you
 


As you can see in the above picture the river.txt file was not present earlier but the file was created after using the above mentioned random option with touch command.

6.touch -c FILE_NAME
If you want to change the timestamps of a file and if the file does not exist then an empty file is created but if you want that the file is not created then you can use the -c option.

Ex:-

$touch -c -m file_name
$touch -c -a file_name

I have tried to make it easy to understand by taking the Linux manual page as a reference.

If you liked this article, please help others by sharing.If there is anything incorrect in this or it needs more improvement then you can comment me in the comment box below or contact at  query.me

 .  

Comments

Popular posts from this blog

ls command

wc Command

ifconfig command