touch command in Linux is used to update timestamps on files, namely, the access and modification time for any file(s). This is basically ‘touching’ the file, and hence the name of the command.
This command has different options through which we could modify certain timestamps for the files provided as arguments.
1. Using touch to create an empty file
touch command can be used to create a new file, if it does not exist, by simply passing the name of the file to be created to it.
touch file1.txt file2.txt ...
root@ubuntu:~/test# touch file1.txt
2. Avoid creating a new file with touch -c command
touch creates a new file if it doesn’t exist. We can use the
-c command option to suppress this behavior. This could be useful if you only want to modify the read or access time of the required file.
touch -c file.txt
The below example does not create
file2.txt, since it does not exist.
3. Update Timestamp on File
We can update the timestamp of the file by passing the required file name to
As you can see, the timestamp of
file1.txt has been updated to the current time. The access and modification times have been updated successfully as a result.
4. Update the access time only
We can update only the access time by using the
touch -a file.txt
The below example modifies only the access time of
file1.txt. The modification time remains unchanged.
5. Update the modification time only
We can update only the modification time by passing the
touch -m file.txt
The below example updates only the modification time of
file1.txt. The access time remains unchanged.
6. Use another file timestamp as reference
We can pass another file’s timestamps as a reference for updating timestamps of the file to be changed, by providing the
-r option (reference)
touch -r src.txt dst.txt
src.txt as the source file (reference) and creates a new file
dst.txt if it does not exists and replicates the timestamps from
src.txt. Refer the below example for more clarity.
7. Use another date as timestamp
We can manipulate the actual date itself for the file timestamp using the
-d option along with a string which is parsed as the ‘datetime’ string.
touch -d 'STRING' file.txt
The string can be in the form of
-3 week, etc.
We learnt how to use Linux
touch command to modify the timestamps of the file in different ways, using appropriate options.
Linux manual page: http://man7.org/linux/man-pages/man1/touch.1.html