loading...
Cover image for Creating files with "touch"

Creating files with "touch"

moopet profile image Ben Sinclair Updated on ・2 min read

Command-line myths (3 Part Series)

1) Sudo like you want to 2) Creating files with "touch" 3) What sourcery is this?

The act of creation

touch is often put forward as the command to create a file. What's wrong with that? Let's start with the manual page1.

touch -- change file access and modification times

It's a command that lets you update a file's timestamps without loading the file. So it's quick - you don't need to open and save a document manually. And it's useful in a few cases that new users probably don't care about. If your system is set to delete logs over a certain age, you can touch one you want to keep.

touch has the side-effect of creating an empty file if it doesn't already exist; that's not its purpose. It's one way of doing it, but you can accomplish the same thing many other ways. You can redirect nothing to a file, for example:

touch this
> that # then hit ctrl-c if you're using zsh
echo > the-other
cp /dev/null yet-another

And these will all do mostly the same thing2

But you have to figure touch is more straight-forward, right? It's one command, with no redirection or funny special characters to remember!

Sure. The problem with touch being taught as a way to create files isn't because it's a bad way of doing it, it's because you don't need to create files.

Say what?

When was the last time you were using a text editor and said to yourself, "Damn it, I forgot to create this file first, how can I save my work?" That's right, never.

There are a very, very limited number of times you - as a learning user - might want to create an empty file. Maybe you use .gitkeep to save empty directories to git. You should probably stop doing that. Maybe you want to add a .hushlogin to your home directory? You probably didn't know that was an option. Maybe you want to check whether you really have write-permission to this mounted directory? Ok, sure.

But no, people rarely create empty files. If anything, you'll be creating files from templates, which is probably handled by your editor or IDE anyway.

Why am I bringing this up? Because touch appears near the top of lists of commands suggested to command-line newbies all the time.

Here's the lesson: you don't need to waste time learning something that seems so oddly-named. YAGNI.

Spare your brain for something else.

--
If you have any practical use cases for creating empty files, or another reason you can think of for this to be a useful command for beginners, let me know in the comments!


  1. There's even a tl;dr

  2. echo > the-other will result in a file containing a newline, whereas the others will result in an empty (0-byte) file. 

Command-line myths (3 Part Series)

1) Sudo like you want to 2) Creating files with "touch" 3) What sourcery is this?

Posted on Nov 11 '19 by:

moopet profile

Ben Sinclair

@moopet

I've been a professional C, Perl, PHP and Python developer. I'm an ex-sysadmin. Back in the day, I had a geekcode which I'm not going to share with you. 418 I'm a teapot.

Discussion

markdown guide
 

touch is great when you want to find a file with a higher degree of time-fidelity than what the various -Xtime flags will get you. E.g., With find's mtime flag, I'm limited to "modified less than 24 hours ago". However, if I use touch in concert with one of find's Xnewer flags, I can reduce my search window to something much smaller. E.g., I could do something like:

touch -t $(date -d "2 mins ago" +%Y%m%d%H%M.%S) /tmp/.stamp
find . -newer /tmp/.stamp

Haven't had much need of this lately, but, was previously useful compiling something from source and didn't intimately know where it would have put all the files I needed to care about.

 

I find touch to be helpful when I am already in the command line, hands to the keyboard, and need to create a new scratch or readme file. Then, I start editing right away. Sometimes I did not handle generating files correctly and need to fill in the gaps.

 

Why is this important for new users? To cover-up hastily-reverted changes to files. ;)

 

Yeah, there're a lot of ways of doing it. I didn't know about that one :)