Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter files when using scp to copy dir recursively?

I need to copy all the .class files from server to local with all dir reserved. e.g. server:/usr/some/unknown/number/of/sub/folders/me.class will be /usr/project/backup/some/unknown/number/of/sub/folders/me.class the problem is, there are many other useless files such as .svn-base files that i don't want. how can i filter them so I only scp .class files?

like image 809
derrdji Avatar asked Aug 04 '09 16:08

derrdji


People also ask

How do you Recursive a SCP?

To copy a directory (and all the files it contains), use scp with the -r option. This tells scp to recursively copy the source directory and its contents. You'll be prompted for your password on the source system ( deathstar.com ). The command won't work unless you enter the correct password.

What is difference between SCP and rsync?

Copying files and directories with SCP or Rsync Secure Copy (SCP) uses SSH to copy only the files or directories that you select. On first use, Rsync copies all files and directories and then it copies only the files and directories that you have changed. It does not copy all the files and directories again.

Does SCP work on folders?

The Unix command scp (which stands for "secure copy protocol") is a simple tool for uploading or downloading files (or directories) to/from a remote machine.

How do you securely transfer files between servers with SCP?

The scp tool relies on SSH (Secure Shell) to transfer files, so all you need is the username and password for the source and target systems. Another advantage is that with SCP you can move files between two remote servers, from your local machine in addition to transferring data between local and remote machines.


1 Answers

I'd probably recommend using something like rsync for this due to its include and exclude flags, e.g:-

rsync -rav -e ssh --include '*/' --include='*.class' --exclude='*' \ server:/usr/some/unknown/number/of/sub/folders/ \  /usr/project/backup/some/unknown/number/of/sub/folders/ 

Some other useful flags:

  • -r for recursive
  • -a for archive (mostly all files)
  • -v for verbose output
  • -e to specify ssh instead of the default (which should be ssh, actually)
like image 85
Gavin Gilmour Avatar answered Sep 20 '22 18:09

Gavin Gilmour