1

I am trying to use scp to copy large log files from a remote server. However I want only the lines in remote log file that has a string "Fail". This is how I am doing it currently

scp user@ip:remote_folder/logfile* /localfolder

This copies all the files starting with logfile in remote server to my local folder. The files are pretty large and I need to copy only the lines in those log file, containing the string "Fail" from remote server. Can any body tell me how to do this? Can I use cat or grep command?

1
  • A little tip: if you are transferring text files, then using the -C flag to ssh/scp to enable compression can make things much faster over a slow connection...
    – thkala
    Commented Jan 14, 2016 at 23:16

2 Answers 2

2

Use grep on the remote machine and filter the output into file name and content:

#!/usr/bin/env bash

BASEDIR=~/temp/log

IFS=$'\n'
for match in `ssh user@ip grep -r Fail "remote_folder/logfile*"`
do
    IFS=: read file line <<< $match
    mkdir -p `dirname $BASEDIR/$file`
    echo $line >> $BASEDIR/$file
done

You might want to look at an explanation to IFS in combination with read.

2

ssh user@ip grep Fail remote_folder/logfile*

1
  • 2
    You might want to quote the file name argument, because your shell will expand it locally if you have globbing enabled, and it matches a local list of files (or you have nullglob enabled in your shell).
    – tripleee
    Commented Jan 14, 2016 at 12:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.