Using pipe and ssh to connect commands between different unix hosts. Output local cat, less, etc into remote files. Grep or watch remote logs

By neokrates, written on November 26, 2010


  • Join date: 11-30-99
  • Posts: 224
View Counter:
Rate it
  • Your favorite browser?

    View Results

    Loading ... Loading ...
  • bodytext bodytext bodytext

Ever wanted to grep through the remote file like you do on your local host? Log in and logout just to check the server config may be avoided. Many Linux / unix commands let you to connect local STDOUT to remote STDIN or vice versa. Here are some use cases which may make your life simpler.

Works for:

✔ most Linux distros

✔ unix systems

✔ CygWin

✔ openSsh


Your user has public / private key pair without pass phrase.
So, you can execute remote commands without any interaction.


Write local output into remote file

You just installed the apache on your $myhost. Now you want to test it.

You can echo the test string into file, only this time the file is in remote htdocs root:

echo "It works" | ssh 'cat >> /usr/local/apache/htdocs/it_works.html'

Then you open the browser and should see http://$myhost/it_works.html


Open remote file with less or cat

With cat:

ssh "cat /usr/local/apache/htdocs/it_works.html"

With less:

ssh "less /usr/local/apache/htdocs/it_works.html"


Grep remote files

With pipe:

ssh "cat /usr/local/apache/htdocs/it_works.html" |  grep "works"

Just remote grep:

ssh "grep works /usr/local/apache/htdocs/it_works.html"


Watching remote logs with tail

This way you can watch the access to apache server in real-time:

ssh "tail -f /etc/httpd/logfiles/access_log"


Have fun! ;)

Be Sociable, Share!
Does that help to solve your problem?
VN:F [1.8.5_1061]
Rating: +1 (from 3 votes)
2 votes 'YES'  1 votes 'NO'


Be Sociable, Share!


4 Responses to “Using pipe and ssh to connect commands between different unix hosts. Output local cat, less, etc into remote files. Grep or watch remote logs”

  1. Gimple says:

    Have you tried using watch with ssh i.e.
    ssh user@system “watch cat /var/log/auth.log”
    I know it can be done otherway round i.e.
    watch ssh user@system “cat /var/log/auth.log”
    In first case it doesnt work. I dont know why
    In second case it works, but it reconnects the ssh connection everytime, that can take a while EVERYTIME. I know theres a workaround to this as well but using connection sharing but it wont be nice if we could just use the first one. Any ideas on why the first one doesnt work ?

    • neokrates says:

      Hi Gimple,
      seems strange that your first case doesn’t work. Similar “watch” commands definitely worked for me.  Like :

      ssh "less /usr/local/apache/htdocs/it_works.html"
      Does "less" command work for you, or not?
      BR // Neo


      • Sergani says:

        Hi neo,
        I have a server running special software, and my monitoring tool is to run the following:
        watch -n 1 “monitoring command”
        I’m trying to do:
        ssh user@IP ‘watch -n 1 “monitoring command”‘
        but it’s giving:

        Error opening terminal: unknown.
        Any thoughts guys?

        • Without knowing much about your system, it is hard to say. Here is one common cause for that problem. It may be failing to ID you term type… Try to set it before watch.

          ssh user@IP ‘export TERM=vt100 && watch -n 1 “monitoring command”‘


          ssh user@IP ‘export TERM=ansi && watch -n 1 “monitoring command”‘

          Hope did no mistake in syntax. That should work if remote shell type is bash.


Leave a Reply