0

I am trying to copy the directory but only with the specific files.

sudo scp -r user@ip:'ls /nas1/logs/tomcat/dev-main/dev-main*|grep localhost_access_log.2023-09-21.txt' /home/test

If the files are located like this at remote server,

/nas1/logs/tomcat/dev-main/dev-main-1111/localhost_access_log.2023-09-21.txt    
/nas1/logs/tomcat/dev-main/dev-main-1234/localhost_access_log.2023-09-21.txt

then I want to copy the files like this to the local server.

/home/test/dev-main-1111/localhost_access_log.2023-09-21.txt
/home/test/dev-main-1234/localhost_access_log.2023-09-21.txt

Can I use grep with scp command?

2
  • You can't do that with scp. Do you have rsync or even tar installed? Commented Oct 25, 2023 at 7:17
  • 'rsync' is not installed. I have to check it is possible to install it.
    – Juyeon
    Commented Oct 25, 2023 at 7:25

2 Answers 2

1

Since rsync is not installed per the comment, you can use tar instead.

ssh user@ip 'cd /nas1/logs/tomcat/ && tar cf - dev-main-*/localhost_access_log.2023-09-22.txt' | tar xf -

Explanation:

  • ssh user@ip '...' executes the command ... on the remote host
  • In this case, the command to be executed is cd /nas1/logs/tomcat/ && tar cv dev-main-*/localhost_access_log.2023-09-22.txt, which first changes the remote directory to /nas1/logs/tomcat/, then executes tar with a glob that matches the files you want to transfer
  • The tar command, with the c option with f - for the archive to be dumped on standard output (without the f option, it would send it to the default tape device or stdout depending on the tar implementation and/or environment) which here is a pipe to the local tar command.
  • The tar xf - part reads the archive from its standard input (the other end of the pipe) and extracts it, preserving the directory structure and some other metadata (add the p option to preserve even more metadata).

You should execute the above command in /home/test/.

0

You can't put commands into an scp path, but it looks like you want a set of files that can be matched with a standard glob (wildcard pattern) and there are other tools that will retain the source path.

Here's one:

rsync -avR 'user@ip:/nas1/logs/tomcat/./dev-main/dev-main-*/localhost_access_log.2023-09-21.txt' /home/test/
3
  • rsync is not installed. I have to check the installing.
    – Juyeon
    Commented Oct 25, 2023 at 7:26
  • Well, actually you can put commands into an scp (remote) path, if the protocol is SCP, not SFTP. See "History, SCP and SFTP" in this answer. The point is in case of SCP the path is processed by a shell on the remote side, this is how your (locally single-quoted) * works on the remote side in case of SCP; but with SCP $() would also work. Not that I recommend using it. Commented Oct 25, 2023 at 8:03
  • @KamilMaciorowski well yes. I've done that in the past with rsync for a very specific situation, but like you I wouldn't recommend it for code that has to be supported by someone else Commented Oct 25, 2023 at 12:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .