How to filter files when using scp to copy recursively?

I need to copy all the .class files from the server to the local one with all the reserved files. 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 problem is that there is a lot of other useless files like .svn-base files that I don't want. how can i filter them so i only scp .class files?

+73
file directory filter shell scp
Aug 04 '09 at 16:15
source share
7 answers

I would recommend using something like rsync for this because of its include and exclude flags, for example: -

 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 (basically all files)
  • -v for verbose output
  • -e to specify ssh instead of the default value (which should be ssh, in fact)
+112
Aug 04 '09 at 16:27
source share

To exclude dotfiles in the base directory:

 scp -r [!.]* server:/path/to/something 
+39
Feb 12 '15 at 14:10
source share

There are no filters in scp to filter files. For โ€œadvancedโ€ things like this, I recommend using rsync:

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

(this line copies rsync from the remote folder to the current one)

Recent rsync tunnels over ssh are automatically the default.

+30
Aug 04 '09 at 16:25
source share

Since you can scp , you must be in ssh order,
either script follow these steps or log in 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 (exit) to the 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 that $(find ...) too long for your tar to change,

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



Finally, since you keep it in /usr/project/backup/ ,
why do mining? Just save tar.bz2 , possibly with a date + timestamp.

+7
Aug 04 '09 at 17:07
source share

With ssh based authentication enabled, the script will 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 
+1
Jan 4 '16 at 11:23
source share

Below is the command for files.

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

  • IP will be the IP address of the destination server.
  • -name "* .log" for included files.
  • \! -name "hs_err_pid2801.log" for file exceptions.
  • . current working directory.
  • -type f for file type.

Below are the commands for the directory.

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

You can customize the command above according to your requirement.

0
Nov 29 '17 at 11:37
source share
  • Copy the source folder to somedir :

    cp -r srcdir somedir

  • Delete all unnecessary files:

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

  • run scp from somedir

-one
Aug 4 '09 at 16:21
source share



All Articles