Use scp to copy files of specific extension from directory





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







-1















I'm writing a bash script that needs to fetch all *_out.csv from a directory, on a remote server. All these files are several directories deep inside of another directory. So for instance, say the directory is called ox_20190404/. I can find all my files by going:



find ox_20190404/assessment/LWR/validation -type f -name "*_out.csv"



This question answers part of my question, but since I don't want to copy the directory in it's entirety I need to figure out how to implement the above code. Suppose I start with this:



$ dir="/projects/ox/git"
$ server="myusername@server"
$ scp $server:$dir/$(ssh $server 'ls -t $dir | head -1') .


How would I grab the files I need from there?



The last part of my question wonders if there is a way to then take all the copied files and place them in the same file path and directory they were in on the remote server.










share|improve this question









New contributor




dylanjm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • To clarify, you want the directory under $dir that has the most recent ... name? timestamp? You hard-coded ox_20190404 in the lead-up, so it's not clear how you selected it.

    – Jeff Schaller
    23 hours ago











  • @JeffSchaller Suppose I'm ssh'd into the server. If I type ls -t /projects/ox/git | head -1 then ox_20190404 is the directory that is returned. I then want to go inside that folder and get the files from there.

    – dylanjm
    23 hours ago













  • is zsh available on $server?

    – Jeff Schaller
    23 hours ago











  • @JeffSchaller It appears so, but it's not really setup (no .zshrc files).

    – dylanjm
    23 hours ago













  • and so the final scp command would explicitly list all of the *_out.csv files underneath the most recent directory under $dir in order to be copied locally?

    – Jeff Schaller
    23 hours ago


















-1















I'm writing a bash script that needs to fetch all *_out.csv from a directory, on a remote server. All these files are several directories deep inside of another directory. So for instance, say the directory is called ox_20190404/. I can find all my files by going:



find ox_20190404/assessment/LWR/validation -type f -name "*_out.csv"



This question answers part of my question, but since I don't want to copy the directory in it's entirety I need to figure out how to implement the above code. Suppose I start with this:



$ dir="/projects/ox/git"
$ server="myusername@server"
$ scp $server:$dir/$(ssh $server 'ls -t $dir | head -1') .


How would I grab the files I need from there?



The last part of my question wonders if there is a way to then take all the copied files and place them in the same file path and directory they were in on the remote server.










share|improve this question









New contributor




dylanjm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • To clarify, you want the directory under $dir that has the most recent ... name? timestamp? You hard-coded ox_20190404 in the lead-up, so it's not clear how you selected it.

    – Jeff Schaller
    23 hours ago











  • @JeffSchaller Suppose I'm ssh'd into the server. If I type ls -t /projects/ox/git | head -1 then ox_20190404 is the directory that is returned. I then want to go inside that folder and get the files from there.

    – dylanjm
    23 hours ago













  • is zsh available on $server?

    – Jeff Schaller
    23 hours ago











  • @JeffSchaller It appears so, but it's not really setup (no .zshrc files).

    – dylanjm
    23 hours ago













  • and so the final scp command would explicitly list all of the *_out.csv files underneath the most recent directory under $dir in order to be copied locally?

    – Jeff Schaller
    23 hours ago














-1












-1








-1








I'm writing a bash script that needs to fetch all *_out.csv from a directory, on a remote server. All these files are several directories deep inside of another directory. So for instance, say the directory is called ox_20190404/. I can find all my files by going:



find ox_20190404/assessment/LWR/validation -type f -name "*_out.csv"



This question answers part of my question, but since I don't want to copy the directory in it's entirety I need to figure out how to implement the above code. Suppose I start with this:



$ dir="/projects/ox/git"
$ server="myusername@server"
$ scp $server:$dir/$(ssh $server 'ls -t $dir | head -1') .


How would I grab the files I need from there?



The last part of my question wonders if there is a way to then take all the copied files and place them in the same file path and directory they were in on the remote server.










share|improve this question









New contributor




dylanjm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












I'm writing a bash script that needs to fetch all *_out.csv from a directory, on a remote server. All these files are several directories deep inside of another directory. So for instance, say the directory is called ox_20190404/. I can find all my files by going:



find ox_20190404/assessment/LWR/validation -type f -name "*_out.csv"



This question answers part of my question, but since I don't want to copy the directory in it's entirety I need to figure out how to implement the above code. Suppose I start with this:



$ dir="/projects/ox/git"
$ server="myusername@server"
$ scp $server:$dir/$(ssh $server 'ls -t $dir | head -1') .


How would I grab the files I need from there?



The last part of my question wonders if there is a way to then take all the copied files and place them in the same file path and directory they were in on the remote server.







bash shell-script rsync scp






share|improve this question









New contributor




dylanjm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




dylanjm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 30 mins ago









ctrl-alt-delor

12.4k42661




12.4k42661






New contributor




dylanjm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









dylanjmdylanjm

992




992




New contributor




dylanjm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





dylanjm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






dylanjm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.













  • To clarify, you want the directory under $dir that has the most recent ... name? timestamp? You hard-coded ox_20190404 in the lead-up, so it's not clear how you selected it.

    – Jeff Schaller
    23 hours ago











  • @JeffSchaller Suppose I'm ssh'd into the server. If I type ls -t /projects/ox/git | head -1 then ox_20190404 is the directory that is returned. I then want to go inside that folder and get the files from there.

    – dylanjm
    23 hours ago













  • is zsh available on $server?

    – Jeff Schaller
    23 hours ago











  • @JeffSchaller It appears so, but it's not really setup (no .zshrc files).

    – dylanjm
    23 hours ago













  • and so the final scp command would explicitly list all of the *_out.csv files underneath the most recent directory under $dir in order to be copied locally?

    – Jeff Schaller
    23 hours ago



















  • To clarify, you want the directory under $dir that has the most recent ... name? timestamp? You hard-coded ox_20190404 in the lead-up, so it's not clear how you selected it.

    – Jeff Schaller
    23 hours ago











  • @JeffSchaller Suppose I'm ssh'd into the server. If I type ls -t /projects/ox/git | head -1 then ox_20190404 is the directory that is returned. I then want to go inside that folder and get the files from there.

    – dylanjm
    23 hours ago













  • is zsh available on $server?

    – Jeff Schaller
    23 hours ago











  • @JeffSchaller It appears so, but it's not really setup (no .zshrc files).

    – dylanjm
    23 hours ago













  • and so the final scp command would explicitly list all of the *_out.csv files underneath the most recent directory under $dir in order to be copied locally?

    – Jeff Schaller
    23 hours ago

















To clarify, you want the directory under $dir that has the most recent ... name? timestamp? You hard-coded ox_20190404 in the lead-up, so it's not clear how you selected it.

– Jeff Schaller
23 hours ago





To clarify, you want the directory under $dir that has the most recent ... name? timestamp? You hard-coded ox_20190404 in the lead-up, so it's not clear how you selected it.

– Jeff Schaller
23 hours ago













@JeffSchaller Suppose I'm ssh'd into the server. If I type ls -t /projects/ox/git | head -1 then ox_20190404 is the directory that is returned. I then want to go inside that folder and get the files from there.

– dylanjm
23 hours ago







@JeffSchaller Suppose I'm ssh'd into the server. If I type ls -t /projects/ox/git | head -1 then ox_20190404 is the directory that is returned. I then want to go inside that folder and get the files from there.

– dylanjm
23 hours ago















is zsh available on $server?

– Jeff Schaller
23 hours ago





is zsh available on $server?

– Jeff Schaller
23 hours ago













@JeffSchaller It appears so, but it's not really setup (no .zshrc files).

– dylanjm
23 hours ago







@JeffSchaller It appears so, but it's not really setup (no .zshrc files).

– dylanjm
23 hours ago















and so the final scp command would explicitly list all of the *_out.csv files underneath the most recent directory under $dir in order to be copied locally?

– Jeff Schaller
23 hours ago





and so the final scp command would explicitly list all of the *_out.csv files underneath the most recent directory under $dir in order to be copied locally?

– Jeff Schaller
23 hours ago










2 Answers
2






active

oldest

votes


















0














I've adjusted some of your variable names a bit.



Surely there are better ways to do this than something dangerous like parsing the output of ls, but see whether this works for you:



$ pth="/projects/ox/git"
$ server="myusername@server"
$ dir="$(ssh $server "ls -t "$pth" | head -1")"
$ mkdir -p "$pth/$dir"
$ scp -p $server:"$pth/$dir"/'*_out.csv' "$pth/$dir"/


Once dir has been set to the newest remote directory, mkdir -p is used to ensure that the same directory name exists locally. Then scp the files into a local directory with the same path and name as the remote directory. I was looking for an rsync solution, but couldn't think of one.






share|improve this answer































    0














    This will find the most recently modified (created) directory, assuming that the directory name does not contain a newline (n)



    newest=$(
    ssh -qn REMOTE 'find ./* -mindepth 0 -maxdepth 0 -type d -printf "%T@t%fn"' |
    sort -t$'t' -r -nk1,2 |
    head -n1 |
    cut -f2-
    )


    If you can guarantee that the target contains only directories of interest you can simplify it considerably (again, bearing in mind the newline issue)



    newest=$(ssh -qn REMOTE ls -t | head -n1)


    You can copy an entire tree of files using scp, but if you want to filter it you'll probably be better off using rsync



    rsync -av --include '*/' --include '*_out.csv' --exclude '*' --prune-empty-dirs REMOTE:"$newest" "$newest"


    If you're keeping the previous set of files locally and you really just wanted to add the latest set without copying the previous ones, rsync can do that too



    rsync -av --include '*/' --include '*_out.csv' --exclude '*' --prune-empty-dirs REMOTE: .





    share|improve this answer


























      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "106"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });






      dylanjm is a new contributor. Be nice, and check out our Code of Conduct.










      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f510795%2fuse-scp-to-copy-files-of-specific-extension-from-directory%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      I've adjusted some of your variable names a bit.



      Surely there are better ways to do this than something dangerous like parsing the output of ls, but see whether this works for you:



      $ pth="/projects/ox/git"
      $ server="myusername@server"
      $ dir="$(ssh $server "ls -t "$pth" | head -1")"
      $ mkdir -p "$pth/$dir"
      $ scp -p $server:"$pth/$dir"/'*_out.csv' "$pth/$dir"/


      Once dir has been set to the newest remote directory, mkdir -p is used to ensure that the same directory name exists locally. Then scp the files into a local directory with the same path and name as the remote directory. I was looking for an rsync solution, but couldn't think of one.






      share|improve this answer




























        0














        I've adjusted some of your variable names a bit.



        Surely there are better ways to do this than something dangerous like parsing the output of ls, but see whether this works for you:



        $ pth="/projects/ox/git"
        $ server="myusername@server"
        $ dir="$(ssh $server "ls -t "$pth" | head -1")"
        $ mkdir -p "$pth/$dir"
        $ scp -p $server:"$pth/$dir"/'*_out.csv' "$pth/$dir"/


        Once dir has been set to the newest remote directory, mkdir -p is used to ensure that the same directory name exists locally. Then scp the files into a local directory with the same path and name as the remote directory. I was looking for an rsync solution, but couldn't think of one.






        share|improve this answer


























          0












          0








          0







          I've adjusted some of your variable names a bit.



          Surely there are better ways to do this than something dangerous like parsing the output of ls, but see whether this works for you:



          $ pth="/projects/ox/git"
          $ server="myusername@server"
          $ dir="$(ssh $server "ls -t "$pth" | head -1")"
          $ mkdir -p "$pth/$dir"
          $ scp -p $server:"$pth/$dir"/'*_out.csv' "$pth/$dir"/


          Once dir has been set to the newest remote directory, mkdir -p is used to ensure that the same directory name exists locally. Then scp the files into a local directory with the same path and name as the remote directory. I was looking for an rsync solution, but couldn't think of one.






          share|improve this answer













          I've adjusted some of your variable names a bit.



          Surely there are better ways to do this than something dangerous like parsing the output of ls, but see whether this works for you:



          $ pth="/projects/ox/git"
          $ server="myusername@server"
          $ dir="$(ssh $server "ls -t "$pth" | head -1")"
          $ mkdir -p "$pth/$dir"
          $ scp -p $server:"$pth/$dir"/'*_out.csv' "$pth/$dir"/


          Once dir has been set to the newest remote directory, mkdir -p is used to ensure that the same directory name exists locally. Then scp the files into a local directory with the same path and name as the remote directory. I was looking for an rsync solution, but couldn't think of one.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 22 hours ago









          Jim L.Jim L.

          1413




          1413

























              0














              This will find the most recently modified (created) directory, assuming that the directory name does not contain a newline (n)



              newest=$(
              ssh -qn REMOTE 'find ./* -mindepth 0 -maxdepth 0 -type d -printf "%T@t%fn"' |
              sort -t$'t' -r -nk1,2 |
              head -n1 |
              cut -f2-
              )


              If you can guarantee that the target contains only directories of interest you can simplify it considerably (again, bearing in mind the newline issue)



              newest=$(ssh -qn REMOTE ls -t | head -n1)


              You can copy an entire tree of files using scp, but if you want to filter it you'll probably be better off using rsync



              rsync -av --include '*/' --include '*_out.csv' --exclude '*' --prune-empty-dirs REMOTE:"$newest" "$newest"


              If you're keeping the previous set of files locally and you really just wanted to add the latest set without copying the previous ones, rsync can do that too



              rsync -av --include '*/' --include '*_out.csv' --exclude '*' --prune-empty-dirs REMOTE: .





              share|improve this answer






























                0














                This will find the most recently modified (created) directory, assuming that the directory name does not contain a newline (n)



                newest=$(
                ssh -qn REMOTE 'find ./* -mindepth 0 -maxdepth 0 -type d -printf "%T@t%fn"' |
                sort -t$'t' -r -nk1,2 |
                head -n1 |
                cut -f2-
                )


                If you can guarantee that the target contains only directories of interest you can simplify it considerably (again, bearing in mind the newline issue)



                newest=$(ssh -qn REMOTE ls -t | head -n1)


                You can copy an entire tree of files using scp, but if you want to filter it you'll probably be better off using rsync



                rsync -av --include '*/' --include '*_out.csv' --exclude '*' --prune-empty-dirs REMOTE:"$newest" "$newest"


                If you're keeping the previous set of files locally and you really just wanted to add the latest set without copying the previous ones, rsync can do that too



                rsync -av --include '*/' --include '*_out.csv' --exclude '*' --prune-empty-dirs REMOTE: .





                share|improve this answer




























                  0












                  0








                  0







                  This will find the most recently modified (created) directory, assuming that the directory name does not contain a newline (n)



                  newest=$(
                  ssh -qn REMOTE 'find ./* -mindepth 0 -maxdepth 0 -type d -printf "%T@t%fn"' |
                  sort -t$'t' -r -nk1,2 |
                  head -n1 |
                  cut -f2-
                  )


                  If you can guarantee that the target contains only directories of interest you can simplify it considerably (again, bearing in mind the newline issue)



                  newest=$(ssh -qn REMOTE ls -t | head -n1)


                  You can copy an entire tree of files using scp, but if you want to filter it you'll probably be better off using rsync



                  rsync -av --include '*/' --include '*_out.csv' --exclude '*' --prune-empty-dirs REMOTE:"$newest" "$newest"


                  If you're keeping the previous set of files locally and you really just wanted to add the latest set without copying the previous ones, rsync can do that too



                  rsync -av --include '*/' --include '*_out.csv' --exclude '*' --prune-empty-dirs REMOTE: .





                  share|improve this answer















                  This will find the most recently modified (created) directory, assuming that the directory name does not contain a newline (n)



                  newest=$(
                  ssh -qn REMOTE 'find ./* -mindepth 0 -maxdepth 0 -type d -printf "%T@t%fn"' |
                  sort -t$'t' -r -nk1,2 |
                  head -n1 |
                  cut -f2-
                  )


                  If you can guarantee that the target contains only directories of interest you can simplify it considerably (again, bearing in mind the newline issue)



                  newest=$(ssh -qn REMOTE ls -t | head -n1)


                  You can copy an entire tree of files using scp, but if you want to filter it you'll probably be better off using rsync



                  rsync -av --include '*/' --include '*_out.csv' --exclude '*' --prune-empty-dirs REMOTE:"$newest" "$newest"


                  If you're keeping the previous set of files locally and you really just wanted to add the latest set without copying the previous ones, rsync can do that too



                  rsync -av --include '*/' --include '*_out.csv' --exclude '*' --prune-empty-dirs REMOTE: .






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 21 hours ago

























                  answered 22 hours ago









                  roaimaroaima

                  46k758124




                  46k758124






















                      dylanjm is a new contributor. Be nice, and check out our Code of Conduct.










                      draft saved

                      draft discarded


















                      dylanjm is a new contributor. Be nice, and check out our Code of Conduct.













                      dylanjm is a new contributor. Be nice, and check out our Code of Conduct.












                      dylanjm is a new contributor. Be nice, and check out our Code of Conduct.
















                      Thanks for contributing an answer to Unix & Linux Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f510795%2fuse-scp-to-copy-files-of-specific-extension-from-directory%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      濃尾地震

                      How to rewrite equation of hyperbola in standard form

                      No ethernet ip address in my vocore2