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?

- 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 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)

- 47,944
- 19
- 150
- 166

- 6,833
- 4
- 40
- 44
-
4Anyway 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
-
2Can 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
-
2The option `-a` already includes `-r` according to the rsync man pages. – Georg Schölly Oct 26 '17 at 10:57
-
5
-
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
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.

- 3,240
- 1
- 30
- 36
-
12This is probably the best answer; using globs to filter files is the way to go. – Yoshua Wuyts Dec 03 '15 at 00:33
-
1If your option are restricted to scp like I had , this definitely helped. – user28095 Dec 12 '15 at 18:23
-
3This indicates to exclude files like being asked, but how can this be achieved for a whole directory? – Pille May 30 '17 at 16:14
-
7
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.

- 2,504
- 28
- 17

- 28,380
- 7
- 60
- 55
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.

- 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
Below command for files.
scp `find . -maxdepth 1 -name "*.log" \! -name "hs_err_pid2801.log" -type f` root@IP:/tmp/test/
- IP will be destination server IP address.
- -name "*.log" for include files.
- \! -name "hs_err_pid2801.log" for exclude files.
- . is current working dir.
- -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.

- 431
- 5
- 10
-
This will copy files from local to remote, the question was about remote to local – user216112 Apr 04 '21 at 12:36
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

- 341
- 2
- 17
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

- 972
- 14
- 26
scp -i /home/<user>/.ssh/id_rsa -o "StrictHostKeyChecking=no" -rp /source/directory/path/[!.]* <target_user>@<target_system:/destination/directory/path

- 5,753
- 72
- 57
- 129
Copy your source folder to
somedir
:cp -r
srcdir
somedir
Remove all unneeded files:
find somedir -name '.svn' -exec rm -rf {} \+
launch scp from
somedir

- 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
-
8Because 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
-
-
if files are being ignored because of space constraints, this wastes space as well as time – jake May 11 '20 at 13:33