115

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?

derrdji
  • 12,661
  • 21
  • 68
  • 78
  • I like the rsync option mentioned. You didn't mention if this is a one-off operation, or if you'll be automating this repeatedly. For a one-off operation, the judicious use of find, grep -v, xargs and temporary files should make short work of this. – user47559 Aug 04 '09 at 16:40

9 Answers9

174

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)
Daniel Imms
  • 47,944
  • 19
  • 150
  • 166
Gavin Gilmour
  • 6,833
  • 4
  • 40
  • 44
  • 4
    Anyway to make this ignore subfolders that don't have *class files in them? (i.e. I don't want a bunch of empty dirs) – Grant Birchmeier Oct 25 '13 at 15:09
  • 2
    Can you explain --include, not --include= In the MAN pages, I could find explanation on --include= but not --include – bazz Feb 10 '14 at 04:15
  • This amazing. Stumbled upon this thread looking for an easy way to scp 40 settings.php files in this multi-installation of Drupal without having to do each one at at time. Thanks! – Josh Oct 31 '14 at 19:54
  • 2
    The option `-a` already includes `-r` according to the rsync man pages. – Georg Schölly Oct 26 '17 at 10:57
  • 5
    @GrantBirchmeier `--prune-empty-dirs` will remove the empty dirs. – Elrond1337 Jun 15 '18 at 07:40
  • Or if you want to just remove them after the fact you can do that with ```find . -type d -print | xargs rmdir``` – John Apr 10 '20 at 19:41
90

To exclude dotfiles in base directory:

scp -r [!.]* server:/path/to/something

[!.]* is a shell glob that expands to all files in working directory not starting with a dot.

rgtk
  • 3,240
  • 1
  • 30
  • 36
47

There is no feature in scp to filter files. For "advanced" stuff like this, I recommend using rsync:

rsync -av --exclude '*.svn' user@server:/my/dir .

(this line copy rsync from distant folder to current one)

Recent versions of rsync tunnel over an ssh connection automatically by default.

Guillaume Gendre
  • 2,504
  • 28
  • 17
Ville Laurikari
  • 28,380
  • 7
  • 60
  • 55
11

Since you can scp you should be ok to ssh,
either script the following or login and execute...

# After reaching the server of interest
cd /usr/some/unknown/number/of/sub/folders
tar cfj pack.tar.bz2 $(find . -type f -name *.class)

return back (logout) to local server and scp,

# from the local machine
cd /usr/project/backup/some/unknown/number/of/sub/folders
scp you@server:/usr/some/unknown/number/of/sub/folders/pack.tar.bz2 .
tar xfj pack.tar.bz2

If you find the $(find ...) is too long for your tar change to,

find . -type f -name *.class | xargs tar cfj pack.tar.bz2

Finally, since you are keeping it in /usr/project/backup/,
why bother extraction? Just keep the tar.bz2, with maybe a date+time stamp.

nik
  • 13,254
  • 3
  • 41
  • 57
  • This helped me. I'm in Windows and I'm fighting trying get `rsync` and `ssh` installed on the OS. Instead, this was a clever workaround. Thanks! – rayryeng Feb 05 '18 at 13:24
2

Below command for files.

scp `find . -maxdepth 1 -name "*.log" \! -name "hs_err_pid2801.log" -type f` root@IP:/tmp/test/

  1. IP will be destination server IP address.
  2. -name "*.log" for include files.
  3. \! -name "hs_err_pid2801.log" for exclude files.
  4. . is current working dir.
  5. -type f for file type.

Below command for directory.

scp -r `find . -maxdepth 1 -name "lo*" \! -name "localhost" -type d` root@IP:/tmp/test/

you can customize above command as per your requirement.

Suneet Khurana
  • 431
  • 5
  • 10
1

With ssh key based authentication enabled, the following script would work.

for x in `ssh user@remotehost 'find /usr/some -type f -name *.class'`; do y=$(echo $x|sed 's/.[^/]*$//'|sed "s/^\/usr//"); mkdir -p /usr/project/backup$y; scp $(echo 'user@remotehost:'$x) /usr/project/backup$y/; done
varun
  • 341
  • 2
  • 17
1

If you indeed wanna use scp, there's a indirect way.Say we want to copy all .jpg file under local folder '/src' to folder '/dst' in remote server 10.1.1.2:

#make a clean temp folder
mkdir /tmp/ttt
#copy all .jpg file and retain folder structure as-is
find /src -type f -name *.jpg -exec cp --parents \{\} /tmp/ttt \;
#copy to remote target folder as-is and retain original time attributes
scp -rp /tmp/ttt/* 10.1.1.2:/dst
#if copy ok, remove temp folder
rm -rf /tmp/ttt
Scott Chu
  • 972
  • 14
  • 26
0
scp -i /home/<user>/.ssh/id_rsa -o "StrictHostKeyChecking=no" -rp /source/directory/path/[!.]* <target_user>@<target_system:/destination/directory/path
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
-3
  1. Copy your source folder to somedir:

    cp -r srcdir somedir

  2. Remove all unneeded files:

    find somedir -name '.svn' -exec rm -rf {} \+

  3. launch scp from somedir

dfa
  • 114,442
  • 31
  • 189
  • 228
  • why all the downvotes on this answer? This sounds like a via solution to the question. – bcarroll Aug 28 '14 at 13:30
  • 8
    Because this would copy a bunch of unnecessary files then delete them, wasting potentially a lot of time. – Oded Aug 14 '15 at 06:33
  • For a program that goes through several compilation steps, this actually makes a lot of sense. It could go right before the step that packs the compiled code into a `tar` or `zip`-file. Instead, there's no compression (scp does the compression) and the 'packaged' code gets copied over. – samvv May 24 '16 at 18:08
  • true that @samvv (all depends on the setup, environment) – San Jay Falcon Jan 14 '19 at 16:51
  • if files are being ignored because of space constraints, this wastes space as well as time – jake May 11 '20 at 13:33