1) I run SSH session on the remote client to get files from the server.

2) There is a server and it keeps a very broad directory structure.

3) I've got a list of thousands path-names to the files on the server. Yet they are only a small fracture of the whole content of the server. So, the files are to be fetched one-by-one, not by directories, no wild cards.

Task: get all files by the list and place them on the client machine with all the relative paths created on the client.

The problem I've encountered is that the sftp cannot write a file in the non-existed dir:

get -p /q/w/e/r/t/y/file /base/q/w/e/r/t/y/file

does not create all the sequence of q/w/e/r/t/y/ in the /base/ (not even the 1-st subdirectory)

Note: the solution may be for a single file too. I'll try to make batch after it.

Not important note: actually, I don't need those paths - but there may be files with the same names and they should not conflict on the new place. So the idea of draining all files in one plain directory is not acceptable.

  • 2
    If you have rsync on both sides you can use that with a file list. (It runs over ssh.) Sep 16, 2019 at 7:44

1 Answer 1


Use a combination of mkdir and dirname before you do a sftp

mkdir -p $(dirname /base/q/w/e/r/t/y/file)
  • dirname will extract the full directory path to the file

  • mkdir -p will ensure that the entire directory tree is created
    (even if it is partially available)

  • thanks! The problem is, sftp command lmdkir has no flag -p. So, I cannot enter sftp and perform creation of the dir and getting the file. As I see it, the separated script should be launched to create the directory tree and then the sftp session - to get files. BTW, lftp does have the mkdir -p - but I have no rights to install lftp on the client machine... Sep 16, 2019 at 7:13
  • 2
    You can use !mkdir. See ! "command"` Sep 16, 2019 at 8:00
  • @MartinPrikryl, thanks for this new hint! So far I've made the 2-phase procedure: one script processes all the list and creates the dir tree, the next script is sftp get. But it's just because of the time limit. I'll return to your suggestion later. Thank you! Sep 16, 2019 at 16:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .