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
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.
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:
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.
touch to create files in directories
Creating a file in a non-existent directory will fail. But combine
mkdir -p, and you’ve got magic:
mkdir -p newdir && touch newdir/newfile.txt
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
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
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
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
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.
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 (
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.
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!