The ‘touch’ command in Linux

The 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

Linux 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.

Format: touch file1.txt file2.txt ...

root@ubuntu:~/test# ls
root@ubuntu:~/test# touch file1.txt
root@ubuntu:~/test# ls
file1.txt
root@ubuntu:~/test# 
Touch Default
Touch – Create a new file

2. Avoid creating a new file with touch -c command

By default, 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.

Format: touch -c file.txt

The below example does not create file2.txt, since it does not exist.

Touch C Option
Touch -c Option

3. Update Timestamp on File

We can update the timestamp of the file by passing the required file name to touch command.

Format: touch file.txt

Touch Change Timestamp
Touch – Change Timestamp

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 -a option.

Format: touch -a file.txt

The below example modifies only the access time of file1.txt. The modification time remains unchanged.

Touch Change Access Time
Touch – Change Access Time

5. Update the modification time only

We can update only the modification time by passing the -m option

Format: touch -m file.txt

The below example updates only the modification time of file1.txt. The access time remains unchanged.

Touch Change Modification Time
Touch – Change Modification Time

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)

Format: touch -r src.txt dst.txt

This uses 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.

Touch Reference Option
Touch – reference Option

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.

Format: touch -d 'STRING' file.txt

The string can be in the form of +3 day, -2 day, -3 week, etc.

Touch D Option
Touch -d Option

Conclusion

We learnt how to use Linux touch command to modify the timestamps of the file in different ways, using appropriate options.

Reference

Linux manual page: http://man7.org/linux/man-pages/man1/touch.1.html