Touch Command Syntax
Before using the touch command, access your VPS server using SSH. Then, open your Linux terminal to run the touch command. The following is what the syntax of the touch command would be like.
touch <options> <file_name>
Create New File
Using the touch command without any options allows you to create a new file. If it is an existing file, then it will only update the access and modification time to the current time instead of overwriting the file with a new file.
touch file_name.txt
To create more files with a single touch command, just specify the names of the files with a space in between the names. Here is an example of how the command would look like creating multiple files.
touch file_name.txt file_name1.txt file_name2.txt
You can also generate file names for multiple files automatically with the curl braces such as the example below.
touch file_name{1..4}.txt
The command above will create four new files starting from “file_name1.txt” up to “file_name4.txt”.
Change Timestamp Information of File
To change the access time of a file to the current time, use the “-a” option such as shown in the following example.
touch -a file_name.txt
To change the modification time of a file to the current time, use the “-m” option such as shown in the following example.
touch -m file_name.txt
To change both access and modification time at once, you may use both options together in a single touch command such as the following example.
touch -am file_name.txt
There are situations where you would want to change the access and modification time to an existing file without creating a new file. Use the “-c” option in this situation such as shown in the following command.
touch -c file_name.txt
You may modify the access and modification time of the file using the “-t” option such as the following example.
touch -t 202201201020.15 file_name.txt
The date and time format have to be written in this way, CCYYMMDDhhmm.ss.
- CC – The first two digits of the year.
- YY – The last two digits of the year.
- MM – The month of the year.
- DD – The day of the month.
- hh – The hour of the day.
- mm – The minute of the hour.
- ss – The second of the minute.
You can also refer to the timestamp information of a file and set a similar timestamp on another file, such as the following command.
touch -r file_name.txt file_name1.txt
The command above will scan the timestamp information of “file_name.txt” and modify the timestamp information of “file_name1.txt” similar to the timestamp information scanned.
To specify the date and time using string, you may use the “-d” command such as the examples shown below.
touch -d '20 Jan' file_name.txt touch -d '10:37' file_name.txt
The first line will set the date of the file to the 20th of January, and the time will be at 00:00 by default. The second line will set the time of the file to 10:37, but the date will be set to the current date by default.
These are the basic usage of the touch command. Master it and improve your VPS file management with it.