Home Learn Linux 10 Advanced Uses of the Touch Command in Linux

10 Advanced Uses of the Touch Command in Linux

The touch command in Linux is known for its simplicity in creating empty files, but its utility goes far beyond. This article delves into 10 advanced uses of the touch command, from setting file timestamps to batch file creation, offering insights to elevate your command-line proficiency.

by Divya Kiran Kumar
Published: Updated:
touch command examples

Today, we’re diving deep into one of the most straightforward yet powerful commands in the Linux universe: the touch command. While its primary function is to create an empty file or update the timestamps of a file without modifying its contents, there’s more to touch than meets the eye. Let’s explore some advanced and creative ways to utilize touch in your daily Linux adventures, sprinkling in my personal experiences, likes, and dislikes along the way.

10 Advanced Uses of the Touch Command in Linux

1. Creating multiple files at once

Did you know you can create multiple files in a single go? This functionality is a real time-saver. Here’s how:

touch file1.txt file2.txt file3.txt

Sample output: This command doesn’t produce output, but if you run ls, you’ll see file1.txt, file2.txt, and file3.txt in your directory.

Personally, I love this feature for quickly setting up project structures without the hassle of creating files one by one.

2. Using touch with wildcard characters

Wildcard characters with touch can be a powerful combination. Suppose you want to update the timestamps of all .txt files in a directory:

touch *.txt

This doesn’t produce visible output but updates the access and modification times of all .txt files in the current directory. It’s incredibly useful for scripting and automation, though I sometimes wish it offered a bit more feedback by default.

3. Creating a file with a specific timestamp

This is where touch shines for me. You can set a file’s access and modification times to a specific date and time:

touch -t 202302271230.00 newfile.txt

This sets the timestamp to 27th February 2023, 12:30 PM. For historical data management or testing, this feature is invaluable, though it can be a bit finicky with the format.

4. Using touch to create files in directories

Creating a file in a non-existent directory will fail. But combine touch with mkdir -p, and you’ve got magic:

mkdir -p newdir && touch newdir/newfile.txt

This creates newdir if it doesn’t exist and then creates newfile.txt inside it. It’s a neat trick that exemplifies Linux’s power and simplicity, though it involves remembering to pair it with mkdir -p.

5. Copying a file’s timestamps to another file

Ever needed one file to have the same timestamps as another? Here’s a cool way to do it:

touch -r originalfile.txt newfile.txt

This copies the timestamps from originalfile.txt to newfile.txt. It’s a niche feature but comes in handy for synchronization tasks, even though I find its use cases to be somewhat limited.

6. Creating files with future timestamps

You can set a file’s timestamp to a future date, which is great for testing time-dependent applications:

touch -d 'next Friday' futurefile.txt

This sets futurefile.txt‘s timestamp to the next Friday from the current date. It’s a quirky feature that’s fun to use, although it’s not something I need daily.

7. Touching files conditionally with -c

Preventing touch from creating a new file if it doesn’t exist can be useful:

touch -c nonexistingfile.txt

This won’t create nonexistingfile.txt if it doesn’t already exist. I appreciate this for scripts where I don’t want to accidentally create files, though I wish this behavior were the default sometimes.

8. Using touch in scripts to log events

Creating an empty file can serve as a marker or log for events in scripts:

touch ~/script_ran_$(date +%Y%m%d).log

This creates a log file with the current date, useful for tracking when scripts run. It’s a simple yet effective logging mechanism that I find elegant.

9. Changing only the access or modification time

You can update just the access time (-a) or modification time (-m):

touch -a -t 202302271230.00 file.txt

This updates only the access time. It’s great for when you need to change one without affecting the other, although remembering the flags can be a bit of a hassle.

10. Touching files with special characters

Handling files with names containing special characters (e.g., spaces) requires careful quoting:

touch "new file with spaces.txt"

This creates a file named new file with spaces.txt. It’s a basic tip but crucial for avoiding unexpected behavior, though dealing with special characters in filenames is always a bit tricky.

Conclusion

The touch command might seem mundane at first glance, but as we’ve seen, it packs a lot of power and flexibility. From managing file timestamps to scripting and beyond, touch provides a plethora of advanced capabilities for those willing to explore its nuances. Like any tool in the Linux toolkit, it has its quirks, but that’s part of what makes Linux so rewarding to use.

Remember, the best way to learn is by doing, so I encourage you to try these examples yourself and discover new ways to use touch in your workflows. Happy touching!

You may also like

1 comment

pobiddy January 19, 2021 - 8:40 PM

Great article, and well written. Thank you. I don’t understand an application for NOT creating a file. Why just not create one – why use the touch command.
I have a few “dummy” password databases that I keep as decoys. I use the touch -d command in Anacron to update the access and modify times on these each day so they appear to be legitimate, and so my authentic database doesn’t stand out.
Cheers,
pobiddy

Reply

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.