How to use the Linux touch command
The Linux command touch is designed to manually amend and access timestamps, however, it’s also often used to create new files. By using the options, you can choose whether the system time is used or if another timestamp is to be used.
What is the Linux touch command?¶
The touch command is used in Linux to change timestamps and access stamps in individual files or directories. Since this recreates a file if it doesn’t already exist, the command is also often used to create new, empty files. For most users, this secondary use is far more important in their daily work. Linux touch works in all command Linux distributions such as Debian or Ubuntu and comes as standard.
How does the touch command work?¶
The primary function of Linux touch is to change timestamps. To do this, the corresponding file is opened by the command and the entry time is changed. The system then changes the timestamp. If there is no time given, the command will use the current system time. If the file entered still doesn’t exist, it will be created by the command if there is no option set not to do this. For this reason, the touch command is one of the simplest ways to create new, empty files.
What does the touch syntax look like?¶
The syntax for the Linux touch command looks as follows:
$ touch [Option] [File]
If the command is used without any options, it will change the timestamp of the file to the current system time or create a new file. If you want to set the timestamp yourself, you have different options.
What are the options in Linux touch?¶
These are the options you have available with the touch command:
- -a: With this option you can change the access time.
- -c or –no-create: Stops a new file from being created.
- -d or –date=STRING: Uses the entered time rather than the system time.
- -m: With this option you can change only the change time.
- -r or –reference=FILE: Uses the timestamp of the reference file.
- -t TIMESTAMP: This uses a manual time stamp. This is entered as [YY]MMDhhmm[.ss].
Examples of a touch command¶
If you want to use Linux touch for yourself, you can use these practical examples:
$ touch example.txt
This will either give the file example.txt a new timestamp or create a new file.
$ touch -t 2301011536 example.txt
You can change the time stamp of the file example.txt to January 1st, 2023, 15:36.