1163
Touch a file in Linux (programming.dev)
you are viewing a single comment's thread
view the rest of the comments
[-] bjoern_tantau@swg-empire.de 132 points 7 months ago

Does anyone actually use touch for its intended purpose? Must be up there with cat.

[-] funkajunk@lemm.ee 95 points 7 months ago
[-] TheBat@lemmy.world 76 points 7 months ago

Wtf. All these years I thought 'touch' was reference to Michelangelo's Creation of Adam.

[-] funkajunk@lemm.ee 44 points 7 months ago

That's beautiful, bro 🥲

[-] BestBouclettes@jlai.lu 90 points 7 months ago* (last edited 7 months ago)

The intended use of touch is to update the timestamp right?

[-] bjoern_tantau@swg-empire.de 100 points 7 months ago

Yeah. It could just as well have issued a file not found error when you try to touch a nonexistent file. And we would be none the wiser about what we're missing in the world.

[-] 4am@lemm.ee 18 points 7 months ago

“Do one thing and do it very well” is the UNIX philosophy after all; if you’re 99% likely to just create that missing file after you get a file not found error, why should touch waste your time?

[-] 0x0@lemmy.dbzer0.com 35 points 7 months ago

Because now touch does two things.

Without touch, we could "just" use the shell to create files.

: > foo.txt
[-] deegeese@sopuli.xyz 21 points 7 months ago

Touch does one thing from a “contract” perspective:

Ensure the timestamp of is

[-] dan@upvote.au 15 points 7 months ago

Systemd also does one thing from a contract perspective: run your system

[-] emptiestplace@lemmy.ml 4 points 7 months ago
load more comments (1 replies)
[-] stebo02@lemmy.dbzer0.com 20 points 7 months ago

with this logic, any command that moves, copies or opens a file should just create a new file if it doesn't exist

and now you're just creating new files without realising just because of a typo

[-] Kusimulkku@lemm.ee 9 points 7 months ago

But this directly goes against that philosophy, since now instead of changing timestamps it's also creating files

[-] kautau@lemmy.world 10 points 7 months ago* (last edited 7 months ago)

You can pass -c to not create a file, but it does go against the philosophy that it creates them by default instead of that being an option

EDIT: Looking closer into the code, it would appear to maybe be an efficiency thing based on underlying system calls

Without that check, touch just opens a file for writing, with no other filesystem check, and closes it

With that check, touch first checks if the file exists, and then if so opens the file for writing

[-] BestBouclettes@jlai.lu 9 points 7 months ago

If you touch -c it should work I guess

[-] zurchpet@lemmy.ml 33 points 7 months ago

We use it to trigger service restarts.

touch tmp/service-restart.txt

Using monit to detect the timestamp change and do the actual restart command.

[-] dan@upvote.au 6 points 7 months ago

This is an interesting idea to allow non-root users to restart a service. It looks like this is doable with systemd too. https://superuser.com/a/1531261

[-] zurchpet@lemmy.ml 2 points 7 months ago

Indeed. Replacing monit with systemd for this job is still on our todo list.

[-] magic_lobster_party@kbin.run 20 points 7 months ago

I sometimes use cat to concatenate files. For example, add a header to a csv file without manually copy and paste it. It’s rare, but at least more frequent than using touch.

[-] wewbull@feddit.uk 19 points 7 months ago
$ cat file1 > output_file
$ cat file2 >> output_file
$ cat file3 >> output_file

I'm sorry!

[-] dan@upvote.au 4 points 7 months ago* (last edited 7 months ago)

That's its intended purpose - combining files together (the opposite of split). See the first line of the man page: https://man7.org/linux/man-pages/man1/cat.1.html

[-] tubaruco@lemm.ee 20 points 7 months ago

what is cat's use if not seeing whats inside a file?

[-] Navigate@lemmings.world 62 points 7 months ago

It is short for concatenate, which is to join things together. You can give it multiple inputs and it will output each one directly following the previous. It so happens to also work with just one input.

[-] kautau@lemmy.world 10 points 7 months ago
[-] 0x0@lemmy.dbzer0.com 50 points 7 months ago
[-] kautau@lemmy.world 9 points 7 months ago
[-] DrWeevilJammer@lemmy.ml 3 points 7 months ago* (last edited 7 months ago)

Bat bat bo-at

Bonbaten-fana fo-fat

Fee-fi-fo-fat

Bat!

load more comments (1 replies)
[-] Mad_Punda@feddit.de 2 points 7 months ago

TIL

I never realized. Thanks!

[-] ulterno@lemmy.kde.social 28 points 7 months ago

It is to use along with split. e.g.

  1. You take a single large file, say 16GB
  2. Use split to break it into multiple files of 4GB
  3. Now you can transfer it to a FAT32 Removable Flash Drive and transfer it to whatever other computer that doesn't have Ethernet.
  4. Here, you can use cat to combine all files into the original file. (preferably accompanied by a checksum)
[-] Tangent5280@lemmy.world 2 points 7 months ago

Doesnt computers do this automatically if you try to copy over a file larger than its per file size limit?

[-] Octopus1348@lemy.lol 9 points 7 months ago

No. It just gives an error that it's too big.

[-] marcos@lemmy.world 14 points 7 months ago

When you updated a Django server, you were supposed to touch the settings.py file so the server would know to reload your code. (I haven't used any for a long time, so I don't know if it's still the procedure.)

There are many small things that use it.

[-] alexdeathway@programming.dev 7 points 7 months ago

it now has a hot reload, How long ago were you using Django?

[-] NotSteve_@lemmy.ca 13 points 7 months ago

I used it recently to update the creation date of a bunch of notes. Just wanted them to display in the correct order in Obsidian. Besides that though, always just used it for file creation lol

[-] noproblemmy@programming.dev 13 points 7 months ago

cat

Ahhhhh, fuck. I'm quite noob with linux. I got into some rabbit hole trying to read the docs. I found 2 man pages, one is cat(1) and the other cat(1p). Apparently the 1p is for POSIX.

If someone could help me understand... As far as I could understand I would normally be concerned with (1), but what would I need to be doing to be affected by (1p)?

[-] survivalmachine@beehaw.org 7 points 7 months ago* (last edited 7 months ago)

The POSIX standard is more portable. If you are writing scripts for your system, you can use the full features in the main man pages. If you are writing code that you want to run on other Linux systems, maybe with reduced feature sets like a tiny embedded computer or alternates to gnu tools like alpine linux, or even other unixes like the BSDs, you will have a better time if you limit yourself to POSIX-compatible features and options -- any POSIX-compatible Unix-like implementation should be able to run POSIX-compliant code.

This is also why many shell scripts will call #!/bin/sh instead of #!/bin/bash -- sh is more likely to be available on tinier systems than bash.

If you are just writing scripts and commands for your own purposes, or you know they will only be used on full-feature distributions, it's often simpler and more comfortable to use all of the advanced features available on your system.

[-] Phoenix3875@lemmy.world 2 points 7 months ago

If you execute a binary without specifying the path to it, it will be searched from the $PATH environment variable, which is a list of places to look for the binary. From left to right, the first found one is returned.

You can use which cat to see what it resolves to and whereis cat to get all possible results.

If you intentionally wants to use a different binary with the same name, you can either directly use its path, or prepend its path to $PATH.

[-] starman2112@sh.itjust.works 10 points 7 months ago

I don't know anything about Linux but I do love touching cats

[-] YoorWeb@lemmy.world 5 points 7 months ago

You would love Linux cli.

[-] Speculater@lemmy.world 8 points 7 months ago

Cat is actually super useful.

[-] Crow@lemmy.blahaj.zone 6 points 7 months ago* (last edited 7 months ago)

I mean, timestamps aren't really all that useful. Really just if you do some stuff with makefiles but even then it's a stretch. I did once use cat for its intended purpose tho, for a report. We split up the individual chapters into their own files so we have an easier time with git stuff, made a script that had an array with the files in the order we wanted, gave it to cat and piped that into pandoc

[-] Trainguyrom@reddthat.com 6 points 7 months ago* (last edited 7 months ago)

Touch is super useful for commands that interact with a file but don't create the file by default. For example, yesterday I needed to copy a file to a remote machine accessible over ssh so I used scp (often known as "secure copy") but needed to touch the file in order to create it before scp would copy into it

[-] emptiestplace@lemmy.ml 8 points 7 months ago
[-] qaz@lemmy.world 3 points 7 months ago

Yes, Nextcloud can't sync files with a timestamp of 0

[-] NaiveBayesian@programming.dev 2 points 7 months ago

Yup, stupid zip files and their directories from 1970

[-] makingStuffForFun@lemmy.ml 2 points 7 months ago

I use it regularly

[-] ik5pvx@lemmy.world 2 points 7 months ago

Yes, when you are for example checking if the permissions in the directory are correct, or if you want to check if your nfs export is working. It's one of those commands that once you know it exists, you WILL find a way to use it.

[-] bjoern_tantau@swg-empire.de 3 points 7 months ago

Well, those aren't really the intended use either.

load more comments (3 replies)
this post was submitted on 17 Mar 2024
1163 points (97.5% liked)

Programmer Humor

19480 readers
736 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 1 year ago
MODERATORS