sh.itjust.works
  • Communities
  • Create Post
  • Create Community
  • heart
    Support Lemmy
  • search
    Search
  • Login
  • Sign Up
chraebsli@programming.dev to Programmer Humor@programming.dev · 1 year ago

Touch a file in Linux

programming.dev

message-square
90
link
fedilink
1.14K

Touch a file in Linux

programming.dev

chraebsli@programming.dev to Programmer Humor@programming.dev · 1 year ago
message-square
90
link
fedilink
alert-triangle
You must log in or register to comment.
  • Björn Tantau@swg-empire.de
    link
    fedilink
    arrow-up
    133
    arrow-down
    2
    ·
    1 year ago

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

    • funkajunk@lemm.ee
      link
      fedilink
      English
      arrow-up
      95
      arrow-down
      1
      ·
      1 year ago

      TIL it’s actually for changing timestamps.

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

      • This is fine🔥🐶☕🔥@lemmy.world
        link
        fedilink
        arrow-up
        76
        ·
        1 year ago

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

        • funkajunk@lemm.ee
          link
          fedilink
          English
          arrow-up
          45
          arrow-down
          1
          ·
          1 year ago

          That’s beautiful, bro 🥲

        • gandalf_der_12te@feddit.de
          link
          fedilink
          arrow-up
          10
          arrow-down
          1
          ·
          1 year ago

          touché

          • orphiebaby@lemm.ee
            link
            fedilink
            English
            arrow-up
            1
            ·
            edit-2
            1 year ago

            That’s not how you use “touché”. Pet peeve of mine.

            • BitsOfBeard@programming.dev
              link
              fedilink
              English
              arrow-up
              3
              ·
              1 year ago

              Touché!

            • Rusty Shackleford@programming.dev
              link
              fedilink
              English
              arrow-up
              1
              ·
              1 year ago

              🇪​🇳​ 🇬​🇦​🇷​🇩​🇪​

              or…

              ʳᶦᵖᵒˢᵗᵉ…?

    • BestBouclettes@jlai.lu
      link
      fedilink
      arrow-up
      89
      ·
      edit-2
      1 year ago

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

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

        • 4am@lemm.ee
          link
          fedilink
          arrow-up
          19
          arrow-down
          3
          ·
          1 year 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?

          • Trailblazing Braille Taser@lemmy.dbzer0.com
            link
            fedilink
            arrow-up
            33
            ·
            1 year ago

            Because now touch does two things.

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

            : > foo.txt
            
            • deegeese@sopuli.xyz
              link
              fedilink
              arrow-up
              21
              ·
              1 year ago

              Touch does one thing from a “contract” perspective:

              Ensure the timestamp of <file> is <now>

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

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

                • emptiestplace@lemmy.ml
                  link
                  fedilink
                  arrow-up
                  4
                  ·
                  1 year ago

                  Oh no.

                  :(

                • dukk@programming.dev
                  link
                  fedilink
                  arrow-up
                  3
                  arrow-down
                  2
                  ·
                  1 year ago

                  Does it do it well, though?

          • stebo02@lemmy.dbzer0.com
            link
            fedilink
            arrow-up
            20
            ·
            edit-2
            12 days ago

            deleted by creator

          • Kusimulkku@lemm.ee
            link
            fedilink
            arrow-up
            8
            ·
            1 year ago

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

            • kautau@lemmy.world
              link
              fedilink
              arrow-up
              10
              ·
              edit-2
              1 year 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
          link
          fedilink
          arrow-up
          9
          ·
          1 year ago

          If you touch -c it should work I guess

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

      • dan@upvote.au
        link
        fedilink
        arrow-up
        6
        ·
        1 year 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@lemmy.ml
          link
          fedilink
          arrow-up
          2
          ·
          1 year ago

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

    • tubaruco@lemm.ee
      link
      fedilink
      arrow-up
      21
      arrow-down
      1
      ·
      1 year ago

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

      • Navigate@lemmings.world
        link
        fedilink
        arrow-up
        62
        ·
        1 year 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
          link
          fedilink
          arrow-up
          10
          ·
          1 year ago

          That’s why we have bat now

          https://github.com/sharkdp/bat

          • Trailblazing Braille Taser@lemmy.dbzer0.com
            link
            fedilink
            arrow-up
            50
            ·
            1 year ago

            To bonbatenate files?

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

              Exactly

            • DrWeevilJammer@lemmy.ml
              link
              fedilink
              arrow-up
              3
              ·
              edit-2
              1 year ago

              Bat bat bo-at

              Bonbaten-fana fo-fat

              Fee-fi-fo-fat

              Bat!

          • Hamartiogonic@sopuli.xyz
            link
            fedilink
            arrow-up
            1
            ·
            1 year ago

            Cat with wings? Isn’t a bat more like a rat with wings?

        • Mad_Punda.de@feddit.de
          link
          fedilink
          arrow-up
          2
          ·
          1 year ago

          TIL

          I never realized. Thanks!

      • ulterno@lemmy.kde.socialBanned
        link
        fedilink
        English
        arrow-up
        26
        ·
        1 year 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
          link
          fedilink
          arrow-up
          2
          ·
          1 year ago

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

          • Octopus1348@lemy.lol
            link
            fedilink
            arrow-up
            9
            ·
            1 year ago

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

    • magic_lobster_party@kbin.run
      link
      fedilink
      arrow-up
      20
      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.

      • wewbull@feddit.uk
        link
        fedilink
        English
        arrow-up
        19
        ·
        1 year ago
        $ cat file1 > output_file
        $ cat file2 >> output_file
        $ cat file3 >> output_file
        

        I’m sorry!

      • dan@upvote.au
        link
        fedilink
        arrow-up
        4
        ·
        edit-2
        1 year 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

    • marcos@lemmy.world
      link
      fedilink
      arrow-up
      14
      ·
      1 year 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
        link
        fedilink
        arrow-up
        7
        ·
        1 year ago

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

    • NotSteve_@lemmy.ca
      link
      fedilink
      arrow-up
      12
      ·
      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
      11
      ·
      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)?

      • Phoenix3875@lemmy.world
        link
        fedilink
        arrow-up
        2
        ·
        1 year 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
      link
      fedilink
      arrow-up
      10
      ·
      1 year ago

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

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

        You would love Linux cli.

    • 🇸‌🇵‌🇪‌🇨‌🇺‌🇱‌🇦‌🇹‌🇪‌🇷‌@lemmy.world
      link
      fedilink
      arrow-up
      8
      ·
      1 year ago

      Cat is actually super useful.

    • Trainguyrom@reddthat.com
      link
      fedilink
      English
      arrow-up
      9
      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

      • emptiestplace@lemmy.ml
        link
        fedilink
        arrow-up
        9
        arrow-down
        1
        ·
        1 year ago

        Sorry, what?

    • 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

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

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

      • NaiveBayesian@programming.dev
        link
        fedilink
        arrow-up
        2
        ·
        1 year ago

        Yup, stupid zip files and their directories from 1970

    • ik5pvx@lemmy.world
      link
      fedilink
      arrow-up
      2
      ·
      1 year 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.

      • Björn Tantau@swg-empire.de
        link
        fedilink
        arrow-up
        3
        ·
        1 year ago

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

    • makingStuffForFun@lemmy.ml
      link
      fedilink
      arrow-up
      2
      ·
      1 year ago

      I use it regularly

    • mox@lemmy.sdf.org
      link
      fedilink
      arrow-up
      1
      ·
      1 year 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

    • Anticorp@lemmy.world
      link
      fedilink
      English
      arrow-up
      1
      ·
      edit-2
      1 year ago

      I use it all the time, especially in ssh on a server.

    • sqw@lemmy.sdf.org
      link
      fedilink
      English
      arrow-up
      0
      ·
      1 year ago

      i use both frequently but im also a pretty dumb user

  • dubyakay@lemmy.ca
    link
    fedilink
    arrow-up
    54
    ·
    1 year ago

    These are some weird looking dolph— oh

  • xmunk
    link
    fedilink
    arrow-up
    49
    arrow-down
    1
    ·
    1 year ago

    Remember to confirm consent before touching.

    • IsoSpandy@lemm.ee
      link
      fedilink
      arrow-up
      65
      arrow-down
      1
      ·
      1 year ago

      You can only touch in places where you have permission to touch.

      • lseif@sopuli.xyz
        link
        fedilink
        arrow-up
        24
        arrow-down
        1
        ·
        1 year ago

        sudo touch woman

        • lad@programming.dev
          link
          fedilink
          arrow-up
          42
          ·
          1 year ago

          Iseif is not in the sudoers file. This incident will be reported.

          • lseif@sopuli.xyz
            link
            fedilink
            arrow-up
            14
            ·
            1 year ago

            to whom, perchance?

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

            • lud@lemm.ee
              link
              fedilink
              arrow-up
              23
              ·
              1 year ago

              Obligatory

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

                Omg I hadn’t seen this one before, laughing so hard

  • Zozano@lemy.lol
    link
    fedilink
    arrow-up
    47
    arrow-down
    3
    ·
    1 year ago

    images-2

    Same energy as Joan Cornella’s comics

    • Schorsch@feddit.de
      link
      fedilink
      arrow-up
      13
      arrow-down
      5
      ·
      1 year ago

      'Murica!

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

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

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

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

    • ezchili@iusearchlinux.fyi
      link
      fedilink
      arrow-up
      15
      arrow-down
      1
      ·
      1 year ago

      I guess printf “” > file

      • 48954246@lemmy.world
        link
        fedilink
        English
        arrow-up
        21
        ·
        edit-2
        1 year ago

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

        touch -a is probably better

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

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

          • owsei@programming.dev
            link
            fedilink
            arrow-up
            10
            ·
            1 year ago

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

            • 4am@lemm.ee
              link
              fedilink
              arrow-up
              6
              ·
              1 year ago

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

              • Trailblazing Braille Taser@lemmy.dbzer0.com
                link
                fedilink
                arrow-up
                14
                ·
                1 year 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.

              • al177@lemmy.sdf.org
                link
                fedilink
                arrow-up
                4
                ·
                edit-2
                1 year ago

                $ :|wc -c 0 $ touch /tmp/f; :>>/tmp/f; wc -c /tmp/f 0 /tmp/f

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

              that’s awesome, did not know about that handy operator!

              • owsei@programming.dev
                link
                fedilink
                arrow-up
                3
                ·
                1 year 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

    • 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.

        • schnurrito@discuss.tchncs.de
          link
          fedilink
          arrow-up
          3
          ·
          1 year ago

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

          • 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
              6
              ·
              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.

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

                That’s a good one!

  • RustyNova@lemmy.world
    link
    fedilink
    arrow-up
    29
    arrow-down
    1
    ·
    1 year 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

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

      If you need multiple files for testing a script or such: touch file{1..5}.txt

      • emptiestplace@lemmy.ml
        link
        fedilink
        arrow-up
        2
        ·
        1 year ago

        If you’re having a party or whatever: touch *

    • DannyBoy
      link
      fedilink
      English
      arrow-up
      5
      ·
      1 year ago

      I do the opposite, I forget I can just create a file with nano. I run touch then open it with nano after to edit.

      • emptiestplace@lemmy.ml
        link
        fedilink
        arrow-up
        9
        arrow-down
        1
        ·
        1 year ago

        That’s weird. Stop it.

    • Octopus1348@lemy.lol
      link
      fedilink
      arrow-up
      2
      ·
      1 year ago

      I usually do open filename because I prefer GUI text editors.

  • makingStuffForFun@lemmy.ml
    link
    fedilink
    arrow-up
    15
    ·
    1 year 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: [email protected]

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
Visibility: Public
globe

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

  • 2.01K users / day
  • 4.26K users / week
  • 8.02K users / month
  • 17.6K users / 6 months
  • 1.21K local subscribers
  • 23.3K subscribers
  • 1.39K Posts
  • 50.2K Comments
  • Modlog
  • mods:
  • Feyter@programming.dev
  • adr1an@programming.dev
  • BurningTurtle@programming.dev
  • Pierre-Yves Lapersonne@programming.dev
  • BE: 0.19.11
  • Modlog
  • Legal
  • Instances
  • Docs
  • Code
  • join-lemmy.org