The touch command changes the time of access or modification time of a file to the current time, or to the time specified in an argument. It is also used to create an empty file with the specified file name, assuming the file does not exist. This is often useful in testing permissions or in simply creating files that will later be processed by some application.
The original purpose of the touch command is to update a file’s timestamp to the current date and time without modifying it. The touch command can also be used to create an empty file of size 0 bytes. We cannot enter any text in the file with the touch command, but we can create multiple new files with a single command.
Syntax
The syntax of the touch command is:
$ touch {file names}
If you encounter the below error while running the touch command:
touch: command not found
you may try installing the below package as per your choice of distribution:
Distribution | Command |
---|---|
OS X | brew install coreutils |
Debian | apt-get install coreutils |
Ubuntu | apt-get install coreutils |
Alpine | apk add coreutils |
Arch Linux | pacman -S coreutils |
Kali Linux | apt-get install coreutils |
CentOS | yum install coreutils |
Fedora | dnf install coreutils |
Raspbian | apt-get install coreutils |
touch Command Example
1. Create a new empty file(s) or change the times for existing file(s) to current time:
# touch path/to/file
2. Set the times on a file to a specific date and time:
# touch -t YYYYMMDDHHMM.SS path/to/file
3. Set the time on a file to one hour in the past:
# touch -d "-1 hour" path/to/file
4. Use the times from a file to set the times on a second file:
# touch -r path/to/file1 path/to/file2
5. Create multiple files:
# touch path/to/file{1,2,3}.txt
Conclusion
touch command updates the access time and modification time (and dates) to the current time and date for one or more files. touch is useful in forcing other commands to handle files a certain way; for example, the operation of make, and sometimes find, relies on a file’s access and modification time. If a file doesn’t exist, touch creates it with a file size of 0.