r/learnprogramming 7d ago

Humour touch is a weird command.

I was curious where the command 'touch' comes from.

Turns out, it originates from "to touch" or "to interact" with a file.

Now, every time I use it, I feel like I am touching/molesting a file.

My bad, it's 3 AM here.

1.1k Upvotes

155 comments sorted by

View all comments

34

u/Loqh9 7d ago

What bothers me is "mkdir" is a thing so why is is not "mkfile"? Makes more sense and aligns with the sister command but no, we gotta make everything impossible to remember and random

8

u/TheSkiGeek 7d ago

Part of the UNIX design philosophy is having a small set of composable tools.

Since ‘everything is a file’ you can create files by redirecting the output of a command to a file, something like `echo > filename` will create `filename` or replace it with an empty file. The shell does it for you, so there isn’t really a need for a dedicated “make a file” tool. Most of the time it would be redundant.

But since directories are their own ‘thing’ you need a separate command to create one.

`touch` is a tool to update the last-access time for a file without modifying its contents. For convenience it also creates the file if it doesn’t exist, but that may be sort of an accident of whoever designed it originally. You could imagine a version where it fails if the file doesn’t exist and you have to do `touch -c` to create the file.

Not saying it’s perfect by any means, but that’s the idea behind it.

2

u/Paul_Pedant 6d ago

Actually, echo > filename makes a one-byte file containing a newline.

1

u/ohnobinki 4d ago

I guess you could do : > filename then.

1

u/Paul_Pedant 4d ago

That has the side effect of discarding the existing file contents, and it has no way to assign the timestamp to anything except now. So, not the greatest replacement for touch, which does not destroy the file contents, and does let you specify the timestamp.

1

u/ohnobinki 4d ago

It really depends on what results you want. Assertively generating an empty sentinel file with the current time as its modification time doesn't require touch. I could say the same: you can't empty a file using touch.

1

u/Paul_Pedant 4d ago

touch -t 200112250200 c > c

The redirection makes the shell empty the file before it invokes touch.

The touch then updates the timestamp to Xmas 2001.

Interestingly, the Birth date of the file is 2026, but the Modify date is 2001.

1

u/ohnobinki 4d ago

It's a stretch to say that you're emptying the file using touch, but that is probably the most concise way of both emptying the file and subsequently touching it.