How can you write to a file from the command line without using shell output redirection? [duplicate]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












0
















This question already has an answer here:



  • Write to a file without redirection?

    3 answers



I'm trying to:



sudo echo "$USER: my message" > /dev/kmsg


to write a message to the dmesg log. This causes a 'permission denied' error, because it is redirecting the output of 'sudo' (while no longer privileged) to /dev/kmsg. So the 'echo' command is running as root and the writing to /dev/kmsg is running as me. Quoting 'echo' command and ther the redirect ('>/dev/kmsg') doesn't work because the redirection is a shell service that sudo doesn't understand. (The userid in question is in sudoers with NOPASSWD.)



Is there some Linux command that takes the file name as an option and writes its arguments out? Like: 'tee /dev/kmsg -- My message to be logged' I know about the 'logger' command, but that doesn't write to whatever log file 'dmesg' sees. (I want to intersperse some of my messages in with the boot process to know when certain things happen.)



This works, but has the problem of needing to have some place to write a file to:



echo "$USER: My msg" >/tmp/foo
sudo cp /tmp/foo /dev/kmsg
rm /tmp/foo


Solution from ctrl-alt-delor's comment below:



echo "my message" | sudo tee /dev/kmsg >/dev/null


Although it does use piping and redirection, it occurs in the unprivileged shell. Privileged access to '/dev/kmsg' occurs in the 'tee' command in 'sudo'. And it doesn't need write to a disk file (which could have problems).



Also @stolenmoments solution:



echo something | sudo dd of=/dev/kmsg









share|improve this question















marked as duplicate by Jeff Schaller, Isaac, Wieland, Michael Homer, Christopher Jan 1 at 21:16


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Your "this works" example actually is using redirection which you say at the outset you do not wish to use. Also, are you sure you don't want to append to the existing log file with >>?

    – DopeGhoti
    Dec 31 '18 at 21:43







  • 2





    echo "message" | sudo tee «file-name» >/dev/null should do it.

    – ctrl-alt-delor
    Dec 31 '18 at 21:57











  • @DopeGhoti Yes, that is not an ideal solution. Also, that works because the redirection is not to the R/W only by root /dev/kmsg file, it is to an ordinary file.

    – Ribo
    Jan 1 at 0:05











  • @JeffSchaller Not a duplicate, the other question deals with writing a new program.

    – Ribo
    Jan 1 at 0:27















0
















This question already has an answer here:



  • Write to a file without redirection?

    3 answers



I'm trying to:



sudo echo "$USER: my message" > /dev/kmsg


to write a message to the dmesg log. This causes a 'permission denied' error, because it is redirecting the output of 'sudo' (while no longer privileged) to /dev/kmsg. So the 'echo' command is running as root and the writing to /dev/kmsg is running as me. Quoting 'echo' command and ther the redirect ('>/dev/kmsg') doesn't work because the redirection is a shell service that sudo doesn't understand. (The userid in question is in sudoers with NOPASSWD.)



Is there some Linux command that takes the file name as an option and writes its arguments out? Like: 'tee /dev/kmsg -- My message to be logged' I know about the 'logger' command, but that doesn't write to whatever log file 'dmesg' sees. (I want to intersperse some of my messages in with the boot process to know when certain things happen.)



This works, but has the problem of needing to have some place to write a file to:



echo "$USER: My msg" >/tmp/foo
sudo cp /tmp/foo /dev/kmsg
rm /tmp/foo


Solution from ctrl-alt-delor's comment below:



echo "my message" | sudo tee /dev/kmsg >/dev/null


Although it does use piping and redirection, it occurs in the unprivileged shell. Privileged access to '/dev/kmsg' occurs in the 'tee' command in 'sudo'. And it doesn't need write to a disk file (which could have problems).



Also @stolenmoments solution:



echo something | sudo dd of=/dev/kmsg









share|improve this question















marked as duplicate by Jeff Schaller, Isaac, Wieland, Michael Homer, Christopher Jan 1 at 21:16


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Your "this works" example actually is using redirection which you say at the outset you do not wish to use. Also, are you sure you don't want to append to the existing log file with >>?

    – DopeGhoti
    Dec 31 '18 at 21:43







  • 2





    echo "message" | sudo tee «file-name» >/dev/null should do it.

    – ctrl-alt-delor
    Dec 31 '18 at 21:57











  • @DopeGhoti Yes, that is not an ideal solution. Also, that works because the redirection is not to the R/W only by root /dev/kmsg file, it is to an ordinary file.

    – Ribo
    Jan 1 at 0:05











  • @JeffSchaller Not a duplicate, the other question deals with writing a new program.

    – Ribo
    Jan 1 at 0:27













0












0








0









This question already has an answer here:



  • Write to a file without redirection?

    3 answers



I'm trying to:



sudo echo "$USER: my message" > /dev/kmsg


to write a message to the dmesg log. This causes a 'permission denied' error, because it is redirecting the output of 'sudo' (while no longer privileged) to /dev/kmsg. So the 'echo' command is running as root and the writing to /dev/kmsg is running as me. Quoting 'echo' command and ther the redirect ('>/dev/kmsg') doesn't work because the redirection is a shell service that sudo doesn't understand. (The userid in question is in sudoers with NOPASSWD.)



Is there some Linux command that takes the file name as an option and writes its arguments out? Like: 'tee /dev/kmsg -- My message to be logged' I know about the 'logger' command, but that doesn't write to whatever log file 'dmesg' sees. (I want to intersperse some of my messages in with the boot process to know when certain things happen.)



This works, but has the problem of needing to have some place to write a file to:



echo "$USER: My msg" >/tmp/foo
sudo cp /tmp/foo /dev/kmsg
rm /tmp/foo


Solution from ctrl-alt-delor's comment below:



echo "my message" | sudo tee /dev/kmsg >/dev/null


Although it does use piping and redirection, it occurs in the unprivileged shell. Privileged access to '/dev/kmsg' occurs in the 'tee' command in 'sudo'. And it doesn't need write to a disk file (which could have problems).



Also @stolenmoments solution:



echo something | sudo dd of=/dev/kmsg









share|improve this question

















This question already has an answer here:



  • Write to a file without redirection?

    3 answers



I'm trying to:



sudo echo "$USER: my message" > /dev/kmsg


to write a message to the dmesg log. This causes a 'permission denied' error, because it is redirecting the output of 'sudo' (while no longer privileged) to /dev/kmsg. So the 'echo' command is running as root and the writing to /dev/kmsg is running as me. Quoting 'echo' command and ther the redirect ('>/dev/kmsg') doesn't work because the redirection is a shell service that sudo doesn't understand. (The userid in question is in sudoers with NOPASSWD.)



Is there some Linux command that takes the file name as an option and writes its arguments out? Like: 'tee /dev/kmsg -- My message to be logged' I know about the 'logger' command, but that doesn't write to whatever log file 'dmesg' sees. (I want to intersperse some of my messages in with the boot process to know when certain things happen.)



This works, but has the problem of needing to have some place to write a file to:



echo "$USER: My msg" >/tmp/foo
sudo cp /tmp/foo /dev/kmsg
rm /tmp/foo


Solution from ctrl-alt-delor's comment below:



echo "my message" | sudo tee /dev/kmsg >/dev/null


Although it does use piping and redirection, it occurs in the unprivileged shell. Privileged access to '/dev/kmsg' occurs in the 'tee' command in 'sudo'. And it doesn't need write to a disk file (which could have problems).



Also @stolenmoments solution:



echo something | sudo dd of=/dev/kmsg




This question already has an answer here:



  • Write to a file without redirection?

    3 answers







sudo logs io-redirection






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 1 at 13:37







Ribo

















asked Dec 31 '18 at 21:37









RiboRibo

1197




1197




marked as duplicate by Jeff Schaller, Isaac, Wieland, Michael Homer, Christopher Jan 1 at 21:16


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Jeff Schaller, Isaac, Wieland, Michael Homer, Christopher Jan 1 at 21:16


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • Your "this works" example actually is using redirection which you say at the outset you do not wish to use. Also, are you sure you don't want to append to the existing log file with >>?

    – DopeGhoti
    Dec 31 '18 at 21:43







  • 2





    echo "message" | sudo tee «file-name» >/dev/null should do it.

    – ctrl-alt-delor
    Dec 31 '18 at 21:57











  • @DopeGhoti Yes, that is not an ideal solution. Also, that works because the redirection is not to the R/W only by root /dev/kmsg file, it is to an ordinary file.

    – Ribo
    Jan 1 at 0:05











  • @JeffSchaller Not a duplicate, the other question deals with writing a new program.

    – Ribo
    Jan 1 at 0:27

















  • Your "this works" example actually is using redirection which you say at the outset you do not wish to use. Also, are you sure you don't want to append to the existing log file with >>?

    – DopeGhoti
    Dec 31 '18 at 21:43







  • 2





    echo "message" | sudo tee «file-name» >/dev/null should do it.

    – ctrl-alt-delor
    Dec 31 '18 at 21:57











  • @DopeGhoti Yes, that is not an ideal solution. Also, that works because the redirection is not to the R/W only by root /dev/kmsg file, it is to an ordinary file.

    – Ribo
    Jan 1 at 0:05











  • @JeffSchaller Not a duplicate, the other question deals with writing a new program.

    – Ribo
    Jan 1 at 0:27
















Your "this works" example actually is using redirection which you say at the outset you do not wish to use. Also, are you sure you don't want to append to the existing log file with >>?

– DopeGhoti
Dec 31 '18 at 21:43






Your "this works" example actually is using redirection which you say at the outset you do not wish to use. Also, are you sure you don't want to append to the existing log file with >>?

– DopeGhoti
Dec 31 '18 at 21:43





2




2





echo "message" | sudo tee «file-name» >/dev/null should do it.

– ctrl-alt-delor
Dec 31 '18 at 21:57





echo "message" | sudo tee «file-name» >/dev/null should do it.

– ctrl-alt-delor
Dec 31 '18 at 21:57













@DopeGhoti Yes, that is not an ideal solution. Also, that works because the redirection is not to the R/W only by root /dev/kmsg file, it is to an ordinary file.

– Ribo
Jan 1 at 0:05





@DopeGhoti Yes, that is not an ideal solution. Also, that works because the redirection is not to the R/W only by root /dev/kmsg file, it is to an ordinary file.

– Ribo
Jan 1 at 0:05













@JeffSchaller Not a duplicate, the other question deals with writing a new program.

– Ribo
Jan 1 at 0:27





@JeffSchaller Not a duplicate, the other question deals with writing a new program.

– Ribo
Jan 1 at 0:27










3 Answers
3






active

oldest

votes


















1














Putting aside the "why", any command that reads stdin and writes to a named output file will do. My favorite is "dd", but I'm old enough to have used what "dd" refers to.




echo something | sudo dd of=/dev/kmsg




should work just fine.






share|improve this answer






























    2














    Use the logger command which is the shell's command-line interface to the syslog system log module. You can use the -p switch to alter which priority the log message is raised with, which in turn is used to determine into which log file the message is written. From the manual:



     -p, --priority priority
    Enter the message into the log with the specified priority. The priority may be specified numerically or as a facility.level pair. For example, -p local3.info logs the message as infor‐
    mational in the local3 facility. The default is user.notice.





    share|improve this answer























    • I tried 'logger -p kern.info ...' and it didn't make it into the output of dmesg (I don't know where dmesg gets its stuff from, I think from the kernel's internal log). It did get written to syslog, messages, and user.log.

      – Ribo
      Jan 1 at 0:15


















    0














    I know you asked for something without shell redirection, but you could make your sudo command work by changing it to the following:



    sudo su -c 'echo "$USER: my message" > /dev/kmsg'






    share|improve this answer























    • True, but there's still something unseemly about blowing out the kernel ring buffer.

      – DopeGhoti
      Dec 31 '18 at 21:50











    • @DopeGhoti: I agree. I don't know if it's considered "correct", but figured I would throw it out there if someone was wondering how to get redirection to work within sudo.

      – Peschke
      Dec 31 '18 at 21:53











    • Agreed, not 'correct'. This is just for debug/research -- I want to be able to intersperse some messages into the dmesg log at various points between other kern activity so I can see the order in which various events occur. Logging it elsewhere won't easily give me the chronology of the the events.

      – Ribo
      Jan 1 at 0:18

















    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Putting aside the "why", any command that reads stdin and writes to a named output file will do. My favorite is "dd", but I'm old enough to have used what "dd" refers to.




    echo something | sudo dd of=/dev/kmsg




    should work just fine.






    share|improve this answer



























      1














      Putting aside the "why", any command that reads stdin and writes to a named output file will do. My favorite is "dd", but I'm old enough to have used what "dd" refers to.




      echo something | sudo dd of=/dev/kmsg




      should work just fine.






      share|improve this answer

























        1












        1








        1







        Putting aside the "why", any command that reads stdin and writes to a named output file will do. My favorite is "dd", but I'm old enough to have used what "dd" refers to.




        echo something | sudo dd of=/dev/kmsg




        should work just fine.






        share|improve this answer













        Putting aside the "why", any command that reads stdin and writes to a named output file will do. My favorite is "dd", but I'm old enough to have used what "dd" refers to.




        echo something | sudo dd of=/dev/kmsg




        should work just fine.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 1 at 1:40









        stolenmomentstolenmoment

        1223




        1223























            2














            Use the logger command which is the shell's command-line interface to the syslog system log module. You can use the -p switch to alter which priority the log message is raised with, which in turn is used to determine into which log file the message is written. From the manual:



             -p, --priority priority
            Enter the message into the log with the specified priority. The priority may be specified numerically or as a facility.level pair. For example, -p local3.info logs the message as infor‐
            mational in the local3 facility. The default is user.notice.





            share|improve this answer























            • I tried 'logger -p kern.info ...' and it didn't make it into the output of dmesg (I don't know where dmesg gets its stuff from, I think from the kernel's internal log). It did get written to syslog, messages, and user.log.

              – Ribo
              Jan 1 at 0:15















            2














            Use the logger command which is the shell's command-line interface to the syslog system log module. You can use the -p switch to alter which priority the log message is raised with, which in turn is used to determine into which log file the message is written. From the manual:



             -p, --priority priority
            Enter the message into the log with the specified priority. The priority may be specified numerically or as a facility.level pair. For example, -p local3.info logs the message as infor‐
            mational in the local3 facility. The default is user.notice.





            share|improve this answer























            • I tried 'logger -p kern.info ...' and it didn't make it into the output of dmesg (I don't know where dmesg gets its stuff from, I think from the kernel's internal log). It did get written to syslog, messages, and user.log.

              – Ribo
              Jan 1 at 0:15













            2












            2








            2







            Use the logger command which is the shell's command-line interface to the syslog system log module. You can use the -p switch to alter which priority the log message is raised with, which in turn is used to determine into which log file the message is written. From the manual:



             -p, --priority priority
            Enter the message into the log with the specified priority. The priority may be specified numerically or as a facility.level pair. For example, -p local3.info logs the message as infor‐
            mational in the local3 facility. The default is user.notice.





            share|improve this answer













            Use the logger command which is the shell's command-line interface to the syslog system log module. You can use the -p switch to alter which priority the log message is raised with, which in turn is used to determine into which log file the message is written. From the manual:



             -p, --priority priority
            Enter the message into the log with the specified priority. The priority may be specified numerically or as a facility.level pair. For example, -p local3.info logs the message as infor‐
            mational in the local3 facility. The default is user.notice.






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Dec 31 '18 at 21:40









            DopeGhotiDopeGhoti

            43.8k55382




            43.8k55382












            • I tried 'logger -p kern.info ...' and it didn't make it into the output of dmesg (I don't know where dmesg gets its stuff from, I think from the kernel's internal log). It did get written to syslog, messages, and user.log.

              – Ribo
              Jan 1 at 0:15

















            • I tried 'logger -p kern.info ...' and it didn't make it into the output of dmesg (I don't know where dmesg gets its stuff from, I think from the kernel's internal log). It did get written to syslog, messages, and user.log.

              – Ribo
              Jan 1 at 0:15
















            I tried 'logger -p kern.info ...' and it didn't make it into the output of dmesg (I don't know where dmesg gets its stuff from, I think from the kernel's internal log). It did get written to syslog, messages, and user.log.

            – Ribo
            Jan 1 at 0:15





            I tried 'logger -p kern.info ...' and it didn't make it into the output of dmesg (I don't know where dmesg gets its stuff from, I think from the kernel's internal log). It did get written to syslog, messages, and user.log.

            – Ribo
            Jan 1 at 0:15











            0














            I know you asked for something without shell redirection, but you could make your sudo command work by changing it to the following:



            sudo su -c 'echo "$USER: my message" > /dev/kmsg'






            share|improve this answer























            • True, but there's still something unseemly about blowing out the kernel ring buffer.

              – DopeGhoti
              Dec 31 '18 at 21:50











            • @DopeGhoti: I agree. I don't know if it's considered "correct", but figured I would throw it out there if someone was wondering how to get redirection to work within sudo.

              – Peschke
              Dec 31 '18 at 21:53











            • Agreed, not 'correct'. This is just for debug/research -- I want to be able to intersperse some messages into the dmesg log at various points between other kern activity so I can see the order in which various events occur. Logging it elsewhere won't easily give me the chronology of the the events.

              – Ribo
              Jan 1 at 0:18















            0














            I know you asked for something without shell redirection, but you could make your sudo command work by changing it to the following:



            sudo su -c 'echo "$USER: my message" > /dev/kmsg'






            share|improve this answer























            • True, but there's still something unseemly about blowing out the kernel ring buffer.

              – DopeGhoti
              Dec 31 '18 at 21:50











            • @DopeGhoti: I agree. I don't know if it's considered "correct", but figured I would throw it out there if someone was wondering how to get redirection to work within sudo.

              – Peschke
              Dec 31 '18 at 21:53











            • Agreed, not 'correct'. This is just for debug/research -- I want to be able to intersperse some messages into the dmesg log at various points between other kern activity so I can see the order in which various events occur. Logging it elsewhere won't easily give me the chronology of the the events.

              – Ribo
              Jan 1 at 0:18













            0












            0








            0







            I know you asked for something without shell redirection, but you could make your sudo command work by changing it to the following:



            sudo su -c 'echo "$USER: my message" > /dev/kmsg'






            share|improve this answer













            I know you asked for something without shell redirection, but you could make your sudo command work by changing it to the following:



            sudo su -c 'echo "$USER: my message" > /dev/kmsg'







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Dec 31 '18 at 21:47









            PeschkePeschke

            2,513924




            2,513924












            • True, but there's still something unseemly about blowing out the kernel ring buffer.

              – DopeGhoti
              Dec 31 '18 at 21:50











            • @DopeGhoti: I agree. I don't know if it's considered "correct", but figured I would throw it out there if someone was wondering how to get redirection to work within sudo.

              – Peschke
              Dec 31 '18 at 21:53











            • Agreed, not 'correct'. This is just for debug/research -- I want to be able to intersperse some messages into the dmesg log at various points between other kern activity so I can see the order in which various events occur. Logging it elsewhere won't easily give me the chronology of the the events.

              – Ribo
              Jan 1 at 0:18

















            • True, but there's still something unseemly about blowing out the kernel ring buffer.

              – DopeGhoti
              Dec 31 '18 at 21:50











            • @DopeGhoti: I agree. I don't know if it's considered "correct", but figured I would throw it out there if someone was wondering how to get redirection to work within sudo.

              – Peschke
              Dec 31 '18 at 21:53











            • Agreed, not 'correct'. This is just for debug/research -- I want to be able to intersperse some messages into the dmesg log at various points between other kern activity so I can see the order in which various events occur. Logging it elsewhere won't easily give me the chronology of the the events.

              – Ribo
              Jan 1 at 0:18
















            True, but there's still something unseemly about blowing out the kernel ring buffer.

            – DopeGhoti
            Dec 31 '18 at 21:50





            True, but there's still something unseemly about blowing out the kernel ring buffer.

            – DopeGhoti
            Dec 31 '18 at 21:50













            @DopeGhoti: I agree. I don't know if it's considered "correct", but figured I would throw it out there if someone was wondering how to get redirection to work within sudo.

            – Peschke
            Dec 31 '18 at 21:53





            @DopeGhoti: I agree. I don't know if it's considered "correct", but figured I would throw it out there if someone was wondering how to get redirection to work within sudo.

            – Peschke
            Dec 31 '18 at 21:53













            Agreed, not 'correct'. This is just for debug/research -- I want to be able to intersperse some messages into the dmesg log at various points between other kern activity so I can see the order in which various events occur. Logging it elsewhere won't easily give me the chronology of the the events.

            – Ribo
            Jan 1 at 0:18





            Agreed, not 'correct'. This is just for debug/research -- I want to be able to intersperse some messages into the dmesg log at various points between other kern activity so I can see the order in which various events occur. Logging it elsewhere won't easily give me the chronology of the the events.

            – Ribo
            Jan 1 at 0:18


            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?