Common Linux Commands – touch to Create Empty Files and Modify Timestamps

Follow the card below to get the latest shares by marking the public account.
Reply in the background with EmbeddedLearning Materials to get a learning packageπŸ‘‡πŸ‘‡πŸ‘‡
Common Linux Commands - touch to Create Empty Files and Modify Timestamps

Introduction

Common Linux Commands - touch to Create Empty Files and Modify Timestamps
The touch command comes from the English word “touch”, which means “to touch or contact”. In the Linux system, this command is mainly used to create empty files or update the access and modification timestamps of files. The touch command does not open the file, it only changes the file’s timestamp. If the specified file does not exist, it creates a new empty file.

Difficulty:πŸ”₯

Practicality:πŸ”₯πŸ”₯πŸ”₯

Common Linux Commands - touch to Create Empty Files and Modify Timestamps

Usage

Common Linux Commands - touch to Create Empty Files and Modify Timestamps
touch [OPTION]... FILE...
Common Linux Commands - touch to Create Empty Files and Modify Timestamps

Common Parameters

Common Linux Commands - touch to Create Empty Files and Modify Timestamps
  • -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.

Common Linux Commands - touch to Create Empty Files and Modify Timestamps

Date Strings

Common Linux Commands - touch to Create Empty Files and Modify Timestamps

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:

  1. “Sun, 29 Feb 2004 16:21:42 -0800”
  2. “2004-02-29 16:21:42”
  3. “next Thursday”
Generally, the date string can include the following elements:
  • 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.

If the date string is empty, it represents the beginning of that day. A complete explanation of date strings can be found at the following URL:

https://www.gnu.org/software/coreutils/manual/html_node/Date-input-formats.html

Common Linux Commands - touch to Create Empty Files and Modify Timestamps

Reference Examples

Common Linux Commands - touch to Create Empty Files and Modify Timestamps

1. Create an empty file with a specified name

touch test.txt
2. Use wildcards to create multiple files
jay@jaylinuxlenovo:~/test/touch$ touch File{1..5}.txt
jay@jaylinuxlenovo:~/test/touch$ ls
File1.txt  File2.txt  File3.txt  File4.txt  File5.txt
3. Update the access and modification time of a specified file
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
5. Use a more understandable time string to update the file’s time
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
Common Linux Commands - touch to Create Empty Files and Modify Timestamps

Precautions

Common Linux Commands - touch to Create Empty Files and Modify Timestamps
  • 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.

Common Linux Commands - touch to Create Empty Files and Modify Timestamps

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

Common Linux Commands - touch to Create Empty Files and Modify Timestamps

Common Linux Commands – su 【Switch User】

Common Linux Commands - touch to Create Empty Files and Modify Timestamps

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

Common Linux Commands - touch to Create Empty Files and Modify Timestamps

[ESP32S3] Setting Up VSCode Development Environment

Common Linux Commands - touch to Create Empty Files and Modify Timestamps

C drive suddenly full, the culprit is …… !

Common Linux Commands - touch to Create Empty Files and Modify Timestamps

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

Leave a Comment