Alina
Alina

Reputation: 2261

scp: how to transfer files from different directories preserving the whole path

I need to transfer all *.png from different directories from a remote server BUT preserving the full path of each .png file because all .png files have the same name.

scp -r -e server:coverages/K4me3/*/pos/output/*/*.png Desktop/

While coping it rewrites already existing .png files because the names od them are the same in different directories. I want to preserve the full directory path,s o that when copying, the .png files are copied within their own directories.

Upvotes: 0

Views: 6430

Answers (2)

Balan
Balan

Reputation: 23

Here is an answer to preserve directory structure and copy just the png files from a server to a local system based on ssh.

ssh user@server 'find /server/path -name "*.png" -print0 | xargs -0 tar -cO' | tar -xfv - -C .

Source: Link

Upvotes: 0

SCP doesn't preserve the paths of files on its own, as you have discovered.

You'll probably want to use rsync to do this, since rsync does preserve paths

I think the command would be:

rsync -a -r -v -z server_config:/path/to/root/directory/on/server [destination_folder]

This is the reverse of this question: scp a folder to a remote system keeping the directory layout


Alternatively, and as the comments suggest, you can write a script to get all of the files or lower level directories (with absolute path) and call an scp transfer on each of them. Here is a script that I at one point used to copy files in this way:

#!/usr/bin/env python
from multiprocessing.dummy import Pool
from subprocess import call
from functools import partial

root = #  Root Directory
files = [
    root + # Sub 1,
    root + # Sub 2,
    root + # Sub 3,
    root + # Sub etc,
]
command_s = "scp -r -v -c arcfour -F /path/to/.ssh/config Server:"
command_e = " Output_Dir/"
max_processes = 4  
# Transfer the files 4 at a time because my computer is busy with other stuff
cmds = []

for filename in files:
    cmds.append(command_s + filename + command_e)

pool = Pool(max_processes)
for i, returncode in enumerate(pool.imap(partial(call, shell=True), cmds)):
    if returncode != 0:
        print ("%d command failed: %d" % (i, returncode))

Upvotes: 1

Related Questions