• Björn Tantau@swg-empire.de
        link
        fedilink
        arrow-up
        75
        ·
        1 year 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.

    • Züri@lemmy.ml
      link
      fedilink
      arrow-up
      23
      ·
      1 year 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.

    • magic_lobster_party@kbin.run
      link
      fedilink
      arrow-up
      19
      arrow-down
      1
      ·
      1 year 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.

    • NotSteve_@lemmy.ca
      link
      fedilink
      arrow-up
      10
      ·
      1 year 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
      link
      fedilink
      arrow-up
      8
      ·
      1 year 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
        link
        fedilink
        arrow-up
        6
        ·
        edit-2
        1 year 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.

    • Crow@lemmy.blahaj.zone
      link
      fedilink
      arrow-up
      6
      arrow-down
      1
      ·
      edit-2
      1 year 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
      link
      fedilink
      English
      arrow-up
      6
      arrow-down
      3
      ·
      edit-2
      1 year 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

    • Gamma@programming.dev
      link
      fedilink
      English
      arrow-up
      20
      ·
      1 year ago

      Nope. If you open a nonexistent path and you have permissions to write to that directory, then that file is created.

    • Midnitte@beehaw.org
      link
      fedilink
      English
      arrow-up
      8
      ·
      1 year ago

      I mean, nano filename will work, but there’s no mkfile that I can find…

      $>filename would also work, but it’s not explicitly for creating a new file

    • tranzystorek_io@beehaw.org
      link
      fedilink
      English
      arrow-up
      6
      ·
      1 year ago

      most shells will accept outputting from a silent command to a file, e.g. :> foo.txt (where : is the posix synonym to the true command)

    • dan@upvote.au
      link
      fedilink
      arrow-up
      4
      ·
      1 year ago

      How often do you actually need a blank file though? Usually you’d be writing something in the file.

      • null@slrpnk.net
        link
        fedilink
        arrow-up
        1
        ·
        1 year ago

        I’m betting that’s why none ever materialized. Most tools that can manipulate a file, can also create that file first, so there’s just never been a usecase.

        Right-clicking the desktop to create a new txt file in Windows feels so natural, but I can’t really think of any time you’d want to create a new file and do nothing with it in a CLI.

          • null@slrpnk.net
            link
            fedilink
            arrow-up
            1
            ·
            1 year ago

            But even still, what’s a realistic usecase that would that involve needing a blank, unmodified file in that instance?

            • indepndnt@lemmy.world
              link
              fedilink
              arrow-up
              1
              ·
              1 year ago

              One use case is if you’re running a web server that is configured to return a “maintenance” page instead of the live site if a particular file exists. Which is actually pretty cool because then you don’t have to update the config when you need to do something or let your users get a bunch of 502 errors, you just touch maintenance and you’re good.