
Introduction

Difficulty:π₯
Practicality:π₯π₯π₯

Usage

touch [OPTION]... FILE...

Common Parameters

-
-a: only update the file’s access time.
-
-c: do not create a new file.
-
-d, –date=STRING: set the file time to the specified date and time instead of the current time.
-
-h, –no-dereference : when processing symbolic links (symlink), do not dereference the symbolic link but operate directly on the link itself. This option is only effective on systems that can modify the timestamp of symbolic links.
-
-m: only update the file’s modification time.
-
-r, –reference=FILE: set the date and time of the specified file or directory to that of the given reference file or directory.
-
-t STAMP: STAMP is a specific format for time: [[CC]YY]MMDDhhmm[.ss], which updates the file time to the specified date and time instead of the current time.
-
–time=WORD: update the specified time, WORD can be: access, atime, use, which are equivalent to the -a option. Or modify, mtime, which are equivalent to the -m option.
-
–help: display help information.
-
–version: display version information.

Date Strings

The touch command’s -d or –date option can follow a string to represent a specific time, and this string is quite flexible and understandable, such as the following two:
-
“Sun, 29 Feb 2004 16:21:42 -0800” -
“2004-02-29 16:21:42” -
“next Thursday”
-
Calendar Time: specific year, month, and day information. -
Time of Day: specific hour, minute, and second information. -
Time Zone: indicates the offset of the time relative to UTC, for example, “-0800” means 8 hours earlier than UTC.
-
Day of the Week: a specific day of the week.
-
Relative Time: such as a few hours ago, a few minutes later, etc.
-
Relative Date: for example, “next Thursday” indicates the next Thursday.
-
Numbers: can be any numbers representing dates or times.
https://www.gnu.org/software/coreutils/manual/html_node/Date-input-formats.html

Reference Examples

1. Create an empty file with a specified name
touch test.txt
jay@jaylinuxlenovo:~/test/touch$ touch File{1..5}.txt
jay@jaylinuxlenovo:~/test/touch$ ls
File1.txt File2.txt File3.txt File4.txt File5.txt
jay@jaylinuxlenovo:~/test/touch$ ls -l File1.txt
-rw-rw-r-- 1 jay jay 0 Dec 31 14:38 File1.txt
jay@jaylinuxlenovo:~/test/touch$ touch File1.txt
jay@jaylinuxlenovo:~/test/touch$ ls -l File1.txt
-rw-rw-r-- 1 jay jay 0 Dec 31 14:44 File1.txt
Time before update was 14:38, after update was 14:44.
4. Use a specific timestamp format to update the file’s time
jay@jaylinuxlenovo:~/test/touch$ ls -l File1.txt
-rw-rw-r-- 1 jay jay 0 Dec 31 14:44 File1.txt
jay@jaylinuxlenovo:~/test/touch$ touch -t 202201010130 File1.txt
jay@jaylinuxlenovo:~/test/touch$ ls -l File1.txt
-rw-rw-r-- 1 jay jay 0 Jan 1 2022 File1.txt
jay@jaylinuxlenovo:~/test/touch$ ls -l File1.txt
-rw-rw-r-- 1 jay jay 0 Dec 31 14:48 File1.txt
jay@jaylinuxlenovo:~/test/touch$ touch -d '2023-12-31 12:12' File1.txt
jay@jaylinuxlenovo:~/test/touch$ ls -l File1.txt
-rw-rw-r-- 1 jay jay 0 Dec 31 12:12 File1.txt
6. Update the time of a specified file to that of a reference file
jay@jaylinuxlenovo:~/test/touch$ ls -l File1.txt File2.txt
-rw-rw-r-- 1 jay jay 0 Dec 31 12:12 File1.txt
-rw-rw-r-- 1 jay jay 0 Dec 31 14:38 File2.txt
jay@jaylinuxlenovo:~/test/touch$ touch -r File2.txt File1.txt
jay@jaylinuxlenovo:~/test/touch$ ls -l File1.txt File2.txt
-rw-rw-r-- 1 jay jay 0 Dec 31 14:38 File1.txt
-rw-rw-r-- 1 jay jay 0 Dec 31 14:38 File2.txt

Precautions

-
When using touch, if the specified file does not exist, the system will create a new empty file. This may lead to files being created in unexpected locations. -
Running the touch command requires sufficient permissions to create or modify files in the specified directory. If there are no corresponding permissions, the command will fail. -
When using the -d parameter to set a specific date, the date format needs to meet the system’s date format requirements. -
Using the -c parameter, if the file does not exist, touch will not create a new file, which is very useful for scenarios where you only want to update the timestamp of existing files.

Learn a new skill in the new year, teaching you how to elegantly draw sequence diagrams

Common Linux Commands – su γSwitch Userγ

Do you know how to use soft links or hard links?

[ESP32S3] Setting Up VSCode Development Environment

C drive suddenly full, the culprit is …… !

No more worries about lawyer’s letters, the ultimate open-source solution for MCU development!