Linux Commands – touch
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed tail command in Linux which is used to print some lines from the bottom of a file or pipe.
https://cloudaffaire.com/linux-commands-tail/
In this blog post, we will discuss touch command in Linux. the touch command is used to update the access and modification times of a file to the current time. If the file does not exist, touch will create an empty file. Every file in Linux has some timestamp associated with it. There is mainly three types of timestamp available to track the last access time (atime), last modify time (mtime), and last change time (ctime). Using touch command, you can change the file atime and mtime.
- access time (atime): access time tells when the file’s content was last accessed.
- modify time (mtime): modify time tells when the file’s content was last changed.
- change time (ctime): change time tells when the file’s attribute (metadata) was last changed.
Linux Commands – touch:
You can use the touch command to change the file access time and modify time. If the file does not exist, the touch command will create an empty file with the current timestamp.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
############################ ## Linux Commands | touch ## ############################ ## Prerequisites: One Unix/Linux/POSIX-compliant operating system with bash shell ##------ ## touch ##------ ## touch [option]... file... touch myfile1 ## creates an empty file with current timestamp stat myfile1 ## get details for the file ## File: myfile1 ## Size: 0 Blocks: 0 IO Block: 65536 regular empty file ## Device: 3a06b594h/973518228d Inode: 7881299348005897 Links: 1 ## Access: (0644/-rw-r--r--) Uid: (197609/ user) Gid: (197121/ UNKNOWN) ## Access: 2020-05-23 23:07:23.112560000 +0530 ## Modify: 2020-05-23 23:07:23.112560000 +0530 ## Change: 2020-05-23 23:07:23.106258600 +0530 ## Birth: 2020-05-23 23:06:06.768060200 +0530 touch myfile1 ## updates the file timestamp since myfile1 exist stat myfile1 ## get details for the file ## File: myfile1 ## Size: 0 Blocks: 0 IO Block: 65536 regular empty file ## Device: 3a06b594h/973518228d Inode: 7881299348005897 Links: 1 ## Access: (0644/-rw-r--r--) Uid: (197609/ user) Gid: (197121/ UNKNOWN) ## Access: 2020-05-23 23:18:56.552289600 +0530 ## Modify: 2020-05-23 23:18:56.552289600 +0530 ## Change: 2020-05-23 23:18:56.551907300 +0530 ## Birth: 2020-05-23 23:06:06.768060200 +0530 touch myfile{1..5} ## can create multiple files at once ls -l |
You can use touch -a or –time=[atime|access|use] options to change only the access time of a file.
1 2 3 4 5 |
## touch -a option touch -a myfile1 ## changes file access time stat myfile1 |
You can use touch -m or –time=[mtime|modify] options to change only the modification time of a file.
1 2 3 4 5 |
## touch -m option touch -m myfile1 ## changes file modify time stat myfile1 |
You can use touch -c or –no-create options to not warn about or create files that do not exist. By default, touch command creates an empty file with the current timestamp if the file already not exist.
1 2 3 4 5 |
## touch -c option touch -c mynewfile ## do not create the file if not exist ls |
You can use touch -d TIME or –date=TIME options to use TIME instead of the current time. Time can contain month names, time zones, ‘am’ and ‘pm’, ‘yesterday’, etc.
1 2 3 4 5 6 7 |
## touch -d option ls -l ## -rw-r--r-- 1 user user 0 May 23 23:27 myfile1 touch -d 'May 22 22:30' myfile1 ## provide a custom timestamp ls -l ## -rw-r--r-- 1 user user 0 May 22 22:30 myfile1 |
You can use touch -r FILE or –reference=FILE option to use the times of the reference FILE instead of the current time. If this option is combined with the ‘–date=TIME’ (‘-d TIME’) option, the reference FILE’s time is the origin for any relative TIMEs given, but is otherwise ignored.
1 2 3 4 5 6 7 8 |
## touch -r option touch -r myfile1 myfile2 ## use reference file (myfile1) timestamp ls -l ## -rw-r--r-- 1 user user 0 May 22 22:30 myfile1 ## -rw-r--r-- 1 user user 0 May 22 22:30 myfile2 |
You can use touch -t [[CC]YY]MMDDHHMM[.SS] option to use the argument (optional four-digit or two-digit years, months, days, hours, minutes, optional seconds) instead of the current time. If the year is specified with only two digits, then CC is 20 for years in the range 0 … 68, and 19 for years in 69 … 99. If no digits of the year are specified, the argument is interpreted as a date in the current year.
1 2 3 4 5 |
## touch -t option touch -t '1903032020' myfile1 ## provide a custom timestamp in [[CC]YY]MMDDHHMM[.SS] format ls -l myfile1 ## -rw-r--r-- 1 user user 0 Mar 3 2019 myfile1 |
You can use touch -h or –no-dereference options to change the timestamps of a symbolic link, rather than what the link refers to. When using this option, empty files are not created, but option ‘-c’ must also be used to avoid warning about files that do not exist.
1 2 3 4 5 6 7 8 9 10 11 |
## touch -h option ln -s myfile1 mylink1 ## creates a symbolic link touch -h mylink1 ## changes the timestamp of the symbolic link ## not the file it refers to ls -l touch mylink1 ## changes the timestamp of the actual file ## the symbolic link refers to ls -l |
Hope you have enjoyed this article. In the next blog post, we will discuss the watch command in Linux.