How can I work with the real file behind a symlink?Problem restoring from tar backup: why are there...

Can I cast a spell through the Invoke Duplicity clone while inside a Forcecage?

Was it really inappropriate to write a pull request for the company I interviewed with?

It doesn't matter the side you see it

When to use mean vs median

Four buttons on a table

Practical reasons to have both a large police force and bounty hunting network?

If there are any 3nion, 5nion, 7nion, 9nion, 10nion, etc.

Sometimes a banana is just a banana

How to merge row in the first column in LaTeX

Do AL rules let me pick different starting equipment?

Difference between 'stomach' and 'uterus'

PTIJ: Aharon, King of Egypt

How to fix my table, centering of columns

Formatting a table to look nice

Reason why dimensional travelling would be restricted

Why do phishing e-mails use faked e-mail addresses instead of the real one?

Is there a frame of reference in which I was born before I was conceived?

I can't die. Who am I?

Can an earth elemental drown/bury its opponent underground using earth glide?

Did Amazon pay $0 in taxes last year?

Where is the fallacy here?

Why are special aircraft used for the carriers in the United States Navy?

Meaning of word ягоза

What is the minimum amount of skill points per HD?



How can I work with the real file behind a symlink?


Problem restoring from tar backup: why are there /dev/disk/by-id/ symlinks and how can I avoid them?Find strings that appear consecutively in a fileFind a file in the CWD or any of its parentsShell script bash: Moving file iterate based on monthHow to create destination directory structure dynamically when using mv with wildcards in bash?Symlink two real directories with files in bothFile Operations Using Symlink Source/Destinationcan bash “source” a custom-interpreter shell?Strange Undeletable FolderCreating a Windows application that has to call bash scripts and Python scripts that use Linux-based modules?













0















I have a file and a symlink:



file: ~/${USER_HOME}/blabla/.cheatsheet
symlink: ~/.cheatsheet (is linked to file above)


Now in my script I do some operations with the file, e.g. add a line and sort the file alphabetically and move it:



addOneCommand() {
file"=~/.cheatsheet"
# add it to the file
echo "${cmd}" >> "${file}"
# sort file instantly
cat "${file}" | sort > "${file}".tmp
mv "${file}".tmp "${file}"
}


But the real file behind the symlink doesn't get affected by the script (e.g. not sorted alphabetically).
What can I do to work with "symlinked files" in bash scripts?



Thanks.










share|improve this question


















  • 1





    Note: file"=~/.cheatsheet" is not going to work.

    – Kamil Maciorowski
    yesterday











  • You may find stackoverflow.com/questions/7665/… or stackoverflow.com/questions/29789204/… or unix.stackexchange.com/questions/167631/… or serverfault.com/questions/76042/… of interest.

    – Jeff Zeitlin
    yesterday
















0















I have a file and a symlink:



file: ~/${USER_HOME}/blabla/.cheatsheet
symlink: ~/.cheatsheet (is linked to file above)


Now in my script I do some operations with the file, e.g. add a line and sort the file alphabetically and move it:



addOneCommand() {
file"=~/.cheatsheet"
# add it to the file
echo "${cmd}" >> "${file}"
# sort file instantly
cat "${file}" | sort > "${file}".tmp
mv "${file}".tmp "${file}"
}


But the real file behind the symlink doesn't get affected by the script (e.g. not sorted alphabetically).
What can I do to work with "symlinked files" in bash scripts?



Thanks.










share|improve this question


















  • 1





    Note: file"=~/.cheatsheet" is not going to work.

    – Kamil Maciorowski
    yesterday











  • You may find stackoverflow.com/questions/7665/… or stackoverflow.com/questions/29789204/… or unix.stackexchange.com/questions/167631/… or serverfault.com/questions/76042/… of interest.

    – Jeff Zeitlin
    yesterday














0












0








0








I have a file and a symlink:



file: ~/${USER_HOME}/blabla/.cheatsheet
symlink: ~/.cheatsheet (is linked to file above)


Now in my script I do some operations with the file, e.g. add a line and sort the file alphabetically and move it:



addOneCommand() {
file"=~/.cheatsheet"
# add it to the file
echo "${cmd}" >> "${file}"
# sort file instantly
cat "${file}" | sort > "${file}".tmp
mv "${file}".tmp "${file}"
}


But the real file behind the symlink doesn't get affected by the script (e.g. not sorted alphabetically).
What can I do to work with "symlinked files" in bash scripts?



Thanks.










share|improve this question














I have a file and a symlink:



file: ~/${USER_HOME}/blabla/.cheatsheet
symlink: ~/.cheatsheet (is linked to file above)


Now in my script I do some operations with the file, e.g. add a line and sort the file alphabetically and move it:



addOneCommand() {
file"=~/.cheatsheet"
# add it to the file
echo "${cmd}" >> "${file}"
# sort file instantly
cat "${file}" | sort > "${file}".tmp
mv "${file}".tmp "${file}"
}


But the real file behind the symlink doesn't get affected by the script (e.g. not sorted alphabetically).
What can I do to work with "symlinked files" in bash scripts?



Thanks.







linux bash symbolic-link






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked yesterday









m1wellm1well

155




155








  • 1





    Note: file"=~/.cheatsheet" is not going to work.

    – Kamil Maciorowski
    yesterday











  • You may find stackoverflow.com/questions/7665/… or stackoverflow.com/questions/29789204/… or unix.stackexchange.com/questions/167631/… or serverfault.com/questions/76042/… of interest.

    – Jeff Zeitlin
    yesterday














  • 1





    Note: file"=~/.cheatsheet" is not going to work.

    – Kamil Maciorowski
    yesterday











  • You may find stackoverflow.com/questions/7665/… or stackoverflow.com/questions/29789204/… or unix.stackexchange.com/questions/167631/… or serverfault.com/questions/76042/… of interest.

    – Jeff Zeitlin
    yesterday








1




1





Note: file"=~/.cheatsheet" is not going to work.

– Kamil Maciorowski
yesterday





Note: file"=~/.cheatsheet" is not going to work.

– Kamil Maciorowski
yesterday













You may find stackoverflow.com/questions/7665/… or stackoverflow.com/questions/29789204/… or unix.stackexchange.com/questions/167631/… or serverfault.com/questions/76042/… of interest.

– Jeff Zeitlin
yesterday





You may find stackoverflow.com/questions/7665/… or stackoverflow.com/questions/29789204/… or unix.stackexchange.com/questions/167631/… or serverfault.com/questions/76042/… of interest.

– Jeff Zeitlin
yesterday










3 Answers
3






active

oldest

votes


















0














The point is, that you create a new file ${file}.tmp, that is now not a symlink, then rename.



You might try



cat "${file}".tmp >"${file}"
rm "${file}".tmp


instead of



mv "${file}".tmp "${file}"


If you don't mind the race condition.



P.S.: If I understand your intention correctly, you might want sort -u instead of plain sort






share|improve this answer
























  • ah ok - that's it - thanks :)

    – m1well
    yesterday



















0














Your issue is that you're using mv, which will replace the symlink.



You can use a number of other approaches (see Eugen Rieck's answer for one), but fundamentally, you need to reuse the symlink, not replace it.



sponge is a good tool that will also allow you to remove handling of the *.tmp file as well:



With a file a, that is symlinked from b:



$ echo -e "3n1n2" > a
$ ln -s a b
$ ls -l
total 1
-rw-r--r-- 1 attie attie 6 Mar 5 15:44 a
lrwxrwxrwx 1 attie attie 1 Mar 5 15:44 b -> a


You can sort and rewrite a via b, without affecting the symlink:



$ cat b | sort | sponge b
$ ls -l
total 1
-rw-r--r-- 1 attie attie 6 Mar 5 15:44 a
lrwxrwxrwx 1 attie attie 1 Mar 5 15:44 b -> a
$ cat b
1
2
3





share|improve this answer































    0














    You can find the real file behind the symlink using the readlink or realpath commands.






    share|improve this answer























      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "3"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1411491%2fhow-can-i-work-with-the-real-file-behind-a-symlink%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      The point is, that you create a new file ${file}.tmp, that is now not a symlink, then rename.



      You might try



      cat "${file}".tmp >"${file}"
      rm "${file}".tmp


      instead of



      mv "${file}".tmp "${file}"


      If you don't mind the race condition.



      P.S.: If I understand your intention correctly, you might want sort -u instead of plain sort






      share|improve this answer
























      • ah ok - that's it - thanks :)

        – m1well
        yesterday
















      0














      The point is, that you create a new file ${file}.tmp, that is now not a symlink, then rename.



      You might try



      cat "${file}".tmp >"${file}"
      rm "${file}".tmp


      instead of



      mv "${file}".tmp "${file}"


      If you don't mind the race condition.



      P.S.: If I understand your intention correctly, you might want sort -u instead of plain sort






      share|improve this answer
























      • ah ok - that's it - thanks :)

        – m1well
        yesterday














      0












      0








      0







      The point is, that you create a new file ${file}.tmp, that is now not a symlink, then rename.



      You might try



      cat "${file}".tmp >"${file}"
      rm "${file}".tmp


      instead of



      mv "${file}".tmp "${file}"


      If you don't mind the race condition.



      P.S.: If I understand your intention correctly, you might want sort -u instead of plain sort






      share|improve this answer













      The point is, that you create a new file ${file}.tmp, that is now not a symlink, then rename.



      You might try



      cat "${file}".tmp >"${file}"
      rm "${file}".tmp


      instead of



      mv "${file}".tmp "${file}"


      If you don't mind the race condition.



      P.S.: If I understand your intention correctly, you might want sort -u instead of plain sort







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered yesterday









      Eugen RieckEugen Rieck

      11k22429




      11k22429













      • ah ok - that's it - thanks :)

        – m1well
        yesterday



















      • ah ok - that's it - thanks :)

        – m1well
        yesterday

















      ah ok - that's it - thanks :)

      – m1well
      yesterday





      ah ok - that's it - thanks :)

      – m1well
      yesterday













      0














      Your issue is that you're using mv, which will replace the symlink.



      You can use a number of other approaches (see Eugen Rieck's answer for one), but fundamentally, you need to reuse the symlink, not replace it.



      sponge is a good tool that will also allow you to remove handling of the *.tmp file as well:



      With a file a, that is symlinked from b:



      $ echo -e "3n1n2" > a
      $ ln -s a b
      $ ls -l
      total 1
      -rw-r--r-- 1 attie attie 6 Mar 5 15:44 a
      lrwxrwxrwx 1 attie attie 1 Mar 5 15:44 b -> a


      You can sort and rewrite a via b, without affecting the symlink:



      $ cat b | sort | sponge b
      $ ls -l
      total 1
      -rw-r--r-- 1 attie attie 6 Mar 5 15:44 a
      lrwxrwxrwx 1 attie attie 1 Mar 5 15:44 b -> a
      $ cat b
      1
      2
      3





      share|improve this answer




























        0














        Your issue is that you're using mv, which will replace the symlink.



        You can use a number of other approaches (see Eugen Rieck's answer for one), but fundamentally, you need to reuse the symlink, not replace it.



        sponge is a good tool that will also allow you to remove handling of the *.tmp file as well:



        With a file a, that is symlinked from b:



        $ echo -e "3n1n2" > a
        $ ln -s a b
        $ ls -l
        total 1
        -rw-r--r-- 1 attie attie 6 Mar 5 15:44 a
        lrwxrwxrwx 1 attie attie 1 Mar 5 15:44 b -> a


        You can sort and rewrite a via b, without affecting the symlink:



        $ cat b | sort | sponge b
        $ ls -l
        total 1
        -rw-r--r-- 1 attie attie 6 Mar 5 15:44 a
        lrwxrwxrwx 1 attie attie 1 Mar 5 15:44 b -> a
        $ cat b
        1
        2
        3





        share|improve this answer


























          0












          0








          0







          Your issue is that you're using mv, which will replace the symlink.



          You can use a number of other approaches (see Eugen Rieck's answer for one), but fundamentally, you need to reuse the symlink, not replace it.



          sponge is a good tool that will also allow you to remove handling of the *.tmp file as well:



          With a file a, that is symlinked from b:



          $ echo -e "3n1n2" > a
          $ ln -s a b
          $ ls -l
          total 1
          -rw-r--r-- 1 attie attie 6 Mar 5 15:44 a
          lrwxrwxrwx 1 attie attie 1 Mar 5 15:44 b -> a


          You can sort and rewrite a via b, without affecting the symlink:



          $ cat b | sort | sponge b
          $ ls -l
          total 1
          -rw-r--r-- 1 attie attie 6 Mar 5 15:44 a
          lrwxrwxrwx 1 attie attie 1 Mar 5 15:44 b -> a
          $ cat b
          1
          2
          3





          share|improve this answer













          Your issue is that you're using mv, which will replace the symlink.



          You can use a number of other approaches (see Eugen Rieck's answer for one), but fundamentally, you need to reuse the symlink, not replace it.



          sponge is a good tool that will also allow you to remove handling of the *.tmp file as well:



          With a file a, that is symlinked from b:



          $ echo -e "3n1n2" > a
          $ ln -s a b
          $ ls -l
          total 1
          -rw-r--r-- 1 attie attie 6 Mar 5 15:44 a
          lrwxrwxrwx 1 attie attie 1 Mar 5 15:44 b -> a


          You can sort and rewrite a via b, without affecting the symlink:



          $ cat b | sort | sponge b
          $ ls -l
          total 1
          -rw-r--r-- 1 attie attie 6 Mar 5 15:44 a
          lrwxrwxrwx 1 attie attie 1 Mar 5 15:44 b -> a
          $ cat b
          1
          2
          3






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered yesterday









          AttieAttie

          11.7k32845




          11.7k32845























              0














              You can find the real file behind the symlink using the readlink or realpath commands.






              share|improve this answer




























                0














                You can find the real file behind the symlink using the readlink or realpath commands.






                share|improve this answer


























                  0












                  0








                  0







                  You can find the real file behind the symlink using the readlink or realpath commands.






                  share|improve this answer













                  You can find the real file behind the symlink using the readlink or realpath commands.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered yesterday









                  xenoidxenoid

                  3,7683719




                  3,7683719






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Super User!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1411491%2fhow-can-i-work-with-the-real-file-behind-a-symlink%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Cannot install PyQt5 The Next CEO of Stack OverflowCannot install tcpreplay 3.4.4cannot...

                      Kapp-Putsch Acontecimentos | Outros artigos | Menu de navegação

                      Why did early computer designers eschew integers? The Next CEO of Stack OverflowWhat register...