• Björn Tantau
    link
    fedilink
    1312 months ago

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

      • Björn Tantau
        link
        fedilink
        992 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.

        • @[email protected]
          link
          fedilink
          182 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?

          • stebo
            link
            fedilink
            202 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

          • @[email protected]
            link
            fedilink
            92 months ago

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

            • @[email protected]
              link
              fedilink
              10
              edit-2
              2 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

    • Züri
      link
      fedilink
      332 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.

    • magic_lobster_party
      link
      fedilink
      202 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.

    • @[email protected]
      link
      fedilink
      142 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
        link
        72 months ago

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

    • @[email protected]
      link
      fedilink
      132 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
      link
      132 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)?

      • @[email protected]
        link
        fedilink
        7
        edit-2
        2 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.

      • @[email protected]
        link
        fedilink
        22 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.

    • @[email protected]
      link
      fedilink
      English
      6
      edit-2
      2 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

    • @[email protected]
      link
      fedilink
      6
      edit-2
      2 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

    • qaz
      link
      fedilink
      32 months ago

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

      • @NaiveBayesian
        link
        22 months ago

        Yup, stupid zip files and their directories from 1970

    • @[email protected]
      link
      fedilink
      22 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.

    • Gamma
      link
      English
      362 months ago

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

      • @[email protected]
        link
        fedilink
        English
        21
        edit-2
        2 months ago

        Feels dangerous to run. What happens if the file already exists and has something important in it?

        touch -a is probably better

        • @[email protected]
          link
          fedilink
          92 months ago

          The other command could just be printf '' >> file to not overwrite it. Or even simpler >>file and then interrupt

          • @owsei
            link
            102 months ago

            or :>>file then you don’t need to interrupt

            • @[email protected]
              link
              fedilink
              62 months ago

              .“:>>” is “append null” right? Do you get a file with a single ASCII NUL or is it truly empty?

              • Trailblazing Braille Taser
                link
                fedilink
                142 months ago

                Not really. I believe : is the “true” builtin. So it’s like running a program that exits with zero and writes nothing to stdout. The >> streams the empty stdout into the named file.

              • @owsei
                link
                32 months ago

                Yeah!

                it’s basically a noop, I use it as a placeholder when I’m writing a script, since bash doesn’t accept code blocks with no commands

    • @[email protected]
      link
      fedilink
      English
      82 months 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

    • @[email protected]
      link
      fedilink
      English
      52 months 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)

    • @[email protected]
      link
      fedilink
      42 months ago

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

      • @[email protected]
        link
        fedilink
        12 months 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.

        • @[email protected]
          link
          fedilink
          22 months ago

          You might if some other program checks whether that file exists and behaves differently depending on that.

          • @[email protected]
            link
            fedilink
            12 months ago

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

            • @[email protected]
              link
              fedilink
              52 months 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.

  • @[email protected]
    link
    fedilink
    282 months ago

    I’m way to used to doing nano file.txt that I always forget about touch.

    Although most times, if I create a file, it’s to put something in it