Daily Linux Command: touch

Do we need to memorize Linux commands? Daily Linux Command: man

Everyone is very familiar with the method of creating files in Windows systems—right-clicking in a folder and selecting a file type from the “New” menu, where the created file’s default name is “New Text Document.txt”.

So how do we create files in a Linux system? Previously, we discussed creating folders using the mkdir(1) command; today we will talk about how to create files using the touch(1) command.

The touch(1) command has two functions: create files or update file timestamps.

✦•———-Basic Usage———-•✦

Usage:

touch <filename1> [[filename2]...]

For example, to create an empty file named <span>main.c</span>, simply execute:

>$ touch main.c

This file has been created, and let’s check its timestamp information:

>$ stat main.c
  File: main.c
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 0,40    Inode: 54671       Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/   dybai)   Gid: ( 1000/   dybai)
Context: unconfined_u:object_r:user_tmp_t:s0
Access: 2025-10-16 10:14:07.688030864 +0800
Modify: 2025-10-16 10:14:07.688030864 +0800
Change: 2025-10-16 10:14:07.688030864 +0800
 Birth: 2025-10-16 10:14:07.687030851 +0800

The file was created at Beijing time <span>2025-10-16 10:14:07</span>.

Now, when we use the touch(1) command followed by an existing filename, don’t worry, this file will not be cleared (it will not be truncated), and its content will not change; only its timestamp will be updated to the current system time:

>$ touch main.c
>$ stat main.c
  File: main.c
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 0,40    Inode: 54671       Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/   dybai)   Gid: ( 1000/   dybai)
Context: unconfined_u:object_r:user_tmp_t:s0
Access: 2025-10-16 10:16:03.267503736 +0800
Modify: 2025-10-16 10:16:03.267503736 +0800
Change: 2025-10-16 10:16:03.267503736 +0800
 Birth: 2025-10-16 10:14:07.687030851 +0800

The <span>Access</span>, <span>Modify</span>, and <span>Change</span> times of the file have changed to <span>2025-10-16 10:16:03</span>.

What is the use of updating file timestamps?

If you are a developer, you will use the <span>make</span> command to build code projects. To save compilation time, make(1) will only compile files when changes occur. However, in some cases, even if the source file’s timestamp has not changed, we may want to recompile (for example, to verify a recently modified Makefile without changing any source files).

So the question arises, how does make(1) know if a source file has been updated?

That’s right, it determines this by checking the file’s <span>Modify</span> timestamp.

Therefore, we can use the feature of touch(1) to update the file’s timestamp to achieve recompilation.

✦•———-Advanced Usage Tips———-•✦

Similar to the previously introduced <span>rm</span> and <span>mkdir</span> commands, we can also use shell wildcards to create files in bulk.

>$ touch files{1..6}.txt
>$ ls -lh
total 0
-rw-r--r--. 1 dybai dybai 0 Oct 16 10:49 files1.txt
-rw-r--r--. 1 dybai dybai 0 Oct 16 10:49 files2.txt
-rw-r--r--. 1 dybai dybai 0 Oct 16 10:49 files3.txt
-rw-r--r--. 1 dybai dybai 0 Oct 16 10:49 files4.txt
-rw-r--r--. 1 dybai dybai 0 Oct 16 10:49 files5.txt
-rw-r--r--. 1 dybai dybai 0 Oct 16 10:49 files6.txt

For other wildcard usages, you can refer to the previously introduced wildcard article and try it out yourself.

Congratulations, you have gained more useful knowledge✨

Leave a Comment