In this article we are going to show how to use touch Command in Linux terminal. If you want to learn about touch command in Linux then this post is ideal for you.
The touch is a command-line utility that is basically used to create a new empty files and update the timestamps of existing files and directories.
Linux is an operating system, like Windows OS, iOS, and Mac OS. Linux is one of the most popular operating system on the planet, Android is also powered by Linux OS(operating system). An operating system is software that manages all hardware resources associated with desktop or laptop. In simple words, the OS(operating system) manages communication between software and hardware.
Linux OS(operating system) is also distributed under an open source license. Open source following these points :
- Freely to run the program, for any purpose.
- Free to study how the program works.
- Freely to re-distribute copies so you can help your friends.
- Free to distribute copies of your modified versions to others.
Linux Files Timestamps
In the Linux system a file has three timestamps :
atime
– access time : atime Shows that when a file last time accessed by some command or application such as cat , vim or grep.mtime
– modify time : mtime shows that when the content of a file was modified last time.ctime -
change time : ctime shows that when the last time the file’s attribute or content was changed.
You can use the stat command to show a file status with timestamps.
$ stat filename
How to Use Touch Command
If you run the touch command without any options and if the given file name does not exist then touch command will create a new file. And if the given file name is already exists then touch command will change the file last access and modification times with the current time.
For an example :
$ touch filename
In the above command, if the given file filename
doesn’t exist then the above command will create it as a new file otherwise it will change its timestamps.
You can also run the touch command with multiple filenames to to create or modify them:
$ touch filename1 filename2 filename3
If you don’t want that touch command create a new file during command execution then you can use the -c
(--no-create
) option to prevent from creating a new file.
$ touch -c filename
If the given file filename is exists then the above command change its timestamps otherwise it will do anything.
Changing only access or modification times
When you run the touch command without any option then touch command will change the file last access and modification times with the current time. But if you want to change only one of them you can use -a and -m option with touch command.
To Change only the access time
Run the touch command with the -a
option to change only the access time of the given file:
$ touch -a filename
To Change only the modify time
Run the touch command with the -
m option to change only the modify time of the given file:
$ touch -m filename
Setting a specific timestamps
With the help of touch command you can also update or create a file with a specific time instead of the current time. simply run the touch command with the -d
(--date=
) option to specify a date string.
For an example :
$ touch -d '22 January 2021 09:02' filename
The above command change the last access and modification time of the given file filename
to 22 January 2021 09:02
.
You can also run the touch command with the only date and it will changes the year automatically with the current one:
$ touch -d '22 January' filename
To using the timestamp of another file
Run the touch command with the -r
(--reference=
) option to specify a reference file and use its timestamps instead of the current time.
For an example :
$ touch -r filename1 filename2
The above command will use the times of filename1
for filename2
.
That’s all
If you face any error and issue in above steps , please use comment box below to report.
If our tutorials helped you, please consider buying us a coffee. We appreciate your support!
Thank you for your support.