0

first time I am using linux for work. I am trying to figure out how I can download multiple files with the same extension in multiple directories using scp.

For example:

/server/directoryA/directoryA1/nameA.txt
/server/directoryA/directoryA2/contactA.txt
/server/directoryA/directoryB1/nameB.txt
/server/directoryB/directoryB2/contactB.txt

I want to download all the *.txt file in one scp command. I can't seem to get it working.

I tried something like:

scp user@server:/server/*/*/*.txt .

I tried with -r too but doesn't seem to be working. Anyone can point me to the right command syntax? Thank you!

WonderSteve
  • 837
  • 2
  • 9
  • 15

1 Answers1

0

You can do this using ssh instead of scp:

ssh user@server 'find /server/ -name "*.txt" -print0 | xargs -0 tar -cO' | tar -xivf - -C .

this will copy all the *.txt into the current dir ".", but it will copy the directory structure too, so if you want just the txt files without the directory structure you will need to move all the downloaded txt files into the current directory by:

find -name "*.txt" -print0 | xargs -0 -I {} cp {} .

and then remove the empty directory structure:

rm ./server -r
Miguel A. Carrasco
  • 1,379
  • 1
  • 15
  • 26