The touch command in Linux is a standard Unix program primarily used to modify a file’s access and modification timestamps. Additionally, it can create a new empty file if the specified file does not exist.
Syntax:
The syntax for the touch command in Linux is:
touch [OPTION]... FILE...
- touch: The command itself.
- [Option]: Optional flags that modify the behavior of the touch command.
- File: One or more file names or paths that you want to modify or create.
OPTIONS:
- -a: Change the access time of file. Do not change the modification time unless -m is also specified.
- -c: Do not create a specified file if it does not exist. Do not write any diagnostic messages concerning this condition.
- -m: Change the modification time of file. Do not change the access time unless -a is also specified.
- -r: ref_file Use the corresponding times of the file named by ref_file instead of the current time.
- -f: ref_file Use the corresponding times of the file named by ref_file instead of the current time.
- file: A path name of a file whose times are to be modified.
- -t: time Use the specified time instead of the current time. time will be a decimal number of the form:
[[CC]YY]MMDDhhmm [.SS]
- MM – The month of the year [01-12].
- DD – The day of the month [01-31].
- hh – The hour of the day [00-23].
- mm – The minute of the hour [00-59].
- CC – The first two digits of the year.
- YY – The second two digits of the year.
- SS – The second of the minute [00-61].
touch Command Examples:
1. The following touch command creates an empty (zero byte) new file called abc.
$ touch abc
2. By using touch command, you can also create more than one single file. For example the following command will create 3 files named, abc,def,ghi.
$ touch abc def ghi
3. To change or update the last access and modification times of a file called abc, use the -a option as follows. The following command sets the current time and date on a file. If the abc file does not exist, it will create the new empty file with the name.
$ touch -a abc
4. Using -c option with touch command avoids creating new files. For example the following command will not create a file called abc if it does not exists.
$ touch -c abc
5. If you like to change the only modification time of a file called abc, then use the -m option with touch command.
$ touch -m abc
6. You can explicitly set the time using -c and -t option with touch command. The following command sets the access and modification date and time to a file abc as 17:30 (17:30 p.m.) December 10 of the current year (2012).
$ touch -c -t 12101730 abc
7. The following touch command with -r option, will update the time-stamp of file abc with the time-stamp of def file. So, both the file holds the same time stamp.
$ touch -r abc def
8. If you would like to create a file with specified time other than the current time, then the below command touch command with -t option will gives the abc file a time stamp of 18:30:55 p.m. on December 10, 2012.
$ touch -t 201212101830.55 abc
Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.
- Apply for Programming Internship
- Check Linux Books
- Practice Programming MCQs
- Check Information Technology Books