8 Touch Command Examples
Check this 8 Touch Command Examples so you can easily manage files and directories. touch is a standard Unix command-line interface program which is used to create empty files, update the access date or modification date of a file or directory. Is the equivalent of creating or opening a file and saving it without any change to the file contents. Touch eliminates the unnecessary steps of opening the file, saving the file, and closing the file again.
Touch Command Options
- -a, change only the access time
- -c, do not create any files
- -d, parse STRING and use it instead of current time
- -m, change only the modification time
- -r, use this file’s times instead of current time
- -t, use [[CC]YY]MMDDhhmm[.ss] instead of current time
Touch Command Examples
1. Create an Empty File
This touch command creates an empty (zero byte) new file called file-a.
# touch file-a
2. Create Multiple Files
You can also create more than one single file. this touch command will create 3 files named, file-a, file-b and file-c.
# touch file-a file-b file-c
3. Change File Access and Modification Time
if you want to change or update the last access and modification times of a file called file-c, use the -a option as follows. The following command sets the current time and date on a file. If the file-c file does not exist, it will created
# touch -a file-c
4. Avoid Creating New File
With -c option with touch command avoids creating new files. following touch command will not create a file called file-c if it does not exists.
# touch -c file-c
5. Change File Modification Time
To change the only modification time of a file called file-c, then use the -m option with touch command.
# touch -m file-c
6. Manually Set the Access and Modification times
You can manually set the time using -c and -t option with touch command.
# touch -c -t YYDDHHMM file-c
For example the following command sets the access and modification date and time to a file file-c as 13:00 p.m May 19 of 2015.
# touch -c -t 05191300 file-c
7. Use the time stamp of another File
Touch command with -r option, will update the time-stamp of file file-a with the time-stamp of file-c file.
# touch -r file-c file-a
8. Create a File using a specified time
If you would like to create a file with specified time other than the current time.
# touch -t YYMMDDHHMM.SS file-b
For example the below touch command with -t option will gives the file-b file a time stamp of 13:00:11 p.m. on may 11, 2015.
# touch -t 201505111300.11 file-b