Beehaw
  • Communities
  • Create Post
  • search
    Search
  • Login
  • Sign Up
chraebsli ( chraebsli@programming.dev )  to Programmer Humor@programming.dev · 3 years ago

Touch a file in Linux

programming.dev

message-square
44
link
fedilink
672

Touch a file in Linux

programming.dev

chraebsli ( chraebsli@programming.dev )  to Programmer Humor@programming.dev · 3 years ago
message-square
44
link
fedilink
alert-triangle
You must log in or # to comment.
  • Björn ( bjoern_tantau@swg-empire.de ) 
    link
    fedilink
    arrow-up
    93
    ·
    3 years ago

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

    • funkajunk ( funkajunk@lemm.ee ) 
      link
      fedilink
      English
      arrow-up
      64
      ·
      3 years ago

      TIL it’s actually for changing timestamps.

      https://www.man7.org/linux/man-pages/man1/touch.1.html

    • BestBouclettes ( BestBouclettes@jlai.lu ) 
      link
      fedilink
      arrow-up
      60
      ·
      3 years ago

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

      • Björn ( bjoern_tantau@swg-empire.de ) 
        link
        fedilink
        arrow-up
        76
        ·
        3 years 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 ( 4am@lemm.ee ) 
          link
          fedilink
          arrow-up
          13
          ·
          3 years 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?

          • T. Hex ( 0x0@lemmy.dbzer0.com ) 
            link
            fedilink
            arrow-up
            26
            ·
            3 years ago

            Because now touch does two things.

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

            : > foo.txt
            
            • deegeese ( deegeese@sopuli.xyz ) 
              link
              fedilink
              arrow-up
              16
              ·
              3 years ago

              Touch does one thing from a “contract” perspective:

              Ensure the timestamp of is

              • dan ( dan@upvote.au ) 
                link
                fedilink
                arrow-up
                12
                ·
                3 years ago

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

                • emptiestplace ( emptiestplace@lemmy.ml ) 
                  link
                  fedilink
                  arrow-up
                  4
                  ·
                  3 years ago

                  Oh no.

                  :(

                • dukk ( dukk@programming.dev ) 
                  link
                  fedilink
                  arrow-up
                  2
                  ·
                  3 years ago

                  Does it do it well, though?

          • stebo02 ( stebo02@lemmy.dbzer0.com ) 
            link
            fedilink
            arrow-up
            13
            ·
            edit-2
            1 year ago

            deleted by creator

          • Kusimulkku ( Kusimulkku@lemm.ee ) 
            link
            fedilink
            arrow-up
            7
            ·
            3 years ago

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

        • BestBouclettes ( BestBouclettes@jlai.lu ) 
          link
          fedilink
          arrow-up
          7
          ·
          3 years ago

          If you touch -c it should work I guess

    • Züri ( zurchpet@lemmy.ml ) 
      link
      fedilink
      arrow-up
      23
      ·
      3 years 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 ( dan@upvote.au ) 
        link
        fedilink
        arrow-up
        6
        ·
        3 years 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

        • Züri ( zurchpet@lemmy.ml ) 
          link
          fedilink
          arrow-up
          2
          ·
          3 years ago

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

    • magic_lobster_party ( magic_lobster_party@kbin.run ) 
      link
      fedilink
      arrow-up
      19
      ·
      3 years 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 ( wewbull@feddit.uk ) 
        link
        fedilink
        English
        arrow-up
        14
        ·
        3 years ago
        $ cat file1 > output_file
        $ cat file2 >> output_file
        $ cat file3 >> output_file
        

        I’m sorry!

      • dan ( dan@upvote.au ) 
        link
        fedilink
        arrow-up
        4
        ·
        3 years 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 ( tubaruco@lemm.ee ) 
      link
      fedilink
      arrow-up
      15
      ·
      3 years ago

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

      • Navigate ( Navigate@lemmings.world ) 
        link
        fedilink
        arrow-up
        38
        ·
        3 years 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.

        • Mad_Punda.de ( Mad_Punda@feddit.de ) 
          link
          fedilink
          arrow-up
          2
          ·
          3 years ago

          TIL

          I never realized. Thanks!

      • ulterno ( ulterno@lemmy.kde.social ) Banned
        link
        fedilink
        English
        arrow-up
        16
        ·
        3 years 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)
    • NotSteve_ ( NotSteve_@lemmy.ca ) 
      link
      fedilink
      arrow-up
      10
      ·
      3 years 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 ( noproblemmy@programming.dev ) 
      link
      fedilink
      arrow-up
      8
      ·
      3 years 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 ( survivalmachine@beehaw.org ) 
        link
        fedilink
        arrow-up
        7
        ·
        edit-2
        3 years 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 ( Crow@lemmy.blahaj.zone ) 
      link
      fedilink
      arrow-up
      6
      ·
      3 years 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 ( Trainguyrom@reddthat.com ) 
      link
      fedilink
      English
      arrow-up
      6
      ·
      3 years 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 ( emptiestplace@lemmy.ml ) 
        link
        fedilink
        arrow-up
        7
        ·
        3 years ago

        Sorry, what?

    • makingStuffForFun ( makingStuffForFun@lemmy.ml ) 
      link
      fedilink
      arrow-up
      2
      ·
      3 years ago

      I use it regularly

    • mox ( mox@lemmy.sdf.org ) 
      link
      fedilink
      arrow-up
      1
      ·
      3 years ago

      Creating an empty file is one of its intended purposes. Unix commands were designed as multi-purpose primitives, so they could be reused and composed to handle many different tasks. The touch command is no exception.

      https://www.youtube.com/watch?v=tc4ROCJYbm0&t=287s

    • sqw ( sqw@lemmy.sdf.org ) 
      link
      fedilink
      English
      arrow-up
      0
      ·
      3 years ago

      i use both frequently but im also a pretty dumb user

  • dubyakay ( dubyakay@lemmy.ca ) 
    link
    fedilink
    arrow-up
    32
    ·
    3 years ago

    These are some weird looking dolph— oh

  • null ( null@slrpnk.net ) 
    link
    fedilink
    arrow-up
    27
    ·
    3 years ago

    Is there a command that’s actually just for creating a new file?

    • Gamma ( gamma@programming.dev ) 
      link
      fedilink
      English
      arrow-up
      20
      ·
      3 years ago

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

    • ezchili ( ezchili@iusearchlinux.fyi ) 
      link
      fedilink
      arrow-up
      9
      ·
      3 years ago

      I guess printf “” > file

    • Midnitte ( Midnitte@beehaw.org ) 
      link
      fedilink
      English
      arrow-up
      8
      ·
      3 years 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

    • tranzystorekk ( tranzystorek_io@beehaw.org ) 
      link
      fedilink
      English
      arrow-up
      6
      ·
      3 years 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 ( dan@upvote.au ) 
      link
      fedilink
      arrow-up
      4
      ·
      3 years ago

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

      • null ( null@slrpnk.net ) 
        link
        fedilink
        arrow-up
        1
        ·
        3 years 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.

        • schnurrito ( schnurrito@discuss.tchncs.de ) 
          link
          fedilink
          arrow-up
          3
          ·
          3 years ago

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

          • null ( null@slrpnk.net ) 
            link
            fedilink
            arrow-up
            1
            ·
            3 years ago

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

  • Zozano ( Zozano@lemy.lol ) 
    link
    fedilink
    arrow-up
    26
    ·
    3 years ago

    images-2

    Same energy as Joan Cornella’s comics

    • Schorsch ( Schorsch@feddit.de ) 
      link
      fedilink
      arrow-up
      7
      ·
      3 years ago

      'Murica!

  • makingStuffForFun ( makingStuffForFun@lemmy.ml ) 
    link
    fedilink
    arrow-up
    12
    ·
    3 years ago

    As a Linux user, that is truly magical, and beautiful.

Programmer Humor@programming.dev

programmer_humor@programming.dev

Subscribe from Remote Instance

Create a post
You are not logged in. However you can subscribe from another Fediverse account, for example Lemmy or Mastodon. To do this, paste the following into the search field of your instance: !programmer_humor@programming.dev

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

  • Keep content in english
  • No advertisements
  • Posts must be related to programming or programmer topics
    • If the mod doesn’t find it funny, you’re banned. Ha-ha!.. For real: do not use the community for “statements”. There are other places for such content. Keep it chill and funny.
Visibility: Public
globe

This community can be federated to other instances and be posted/commented in by their users.

  • 339 users / day
  • 2.92K users / week
  • 4.07K users / month
  • 7.69K users / 6 months
  • 463 local subscribers
  • 33.7K subscribers
  • 1.56K Posts
  • 25.6K Comments
  • Modlog
  • mods:
  • Feyter ( Feyter@programming.dev ) 
  • adr1an ( anzo@programming.dev ) 
  • BurningTurtle ( BurningTurtle@programming.dev ) 
  • Pierre-Yves Lapersonne ( pylapp@programming.dev ) 
  • BE: 0.19.15
  • Modlog
  • Legal
  • Instances
  • Docs
  • Code