using scp renaming all files











up vote
0
down vote

favorite












I am sending files from one server to another using scp . I need to rename files after sending that .So I use following command for each file



scp original-hc.db user@host:/dir/original-hc_1.db


I want to send all files using a single command with renaming files . Like



scp *.db user@host:/dir/(actual file name before extension)_1.db









share|improve this question




















  • 2




    I don't believe what you're asking for is possible in a single command. As I understand it scp or any copy command for that matter will allow multiple files to copy but requires a single destination and does not allow for renaming unless you are only copying a single file. There are commands to rename lots of files at once, e.g. rename but not one command that combines both.
    – Zachary Brady
    Aug 31 '16 at 17:41















up vote
0
down vote

favorite












I am sending files from one server to another using scp . I need to rename files after sending that .So I use following command for each file



scp original-hc.db user@host:/dir/original-hc_1.db


I want to send all files using a single command with renaming files . Like



scp *.db user@host:/dir/(actual file name before extension)_1.db









share|improve this question




















  • 2




    I don't believe what you're asking for is possible in a single command. As I understand it scp or any copy command for that matter will allow multiple files to copy but requires a single destination and does not allow for renaming unless you are only copying a single file. There are commands to rename lots of files at once, e.g. rename but not one command that combines both.
    – Zachary Brady
    Aug 31 '16 at 17:41













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am sending files from one server to another using scp . I need to rename files after sending that .So I use following command for each file



scp original-hc.db user@host:/dir/original-hc_1.db


I want to send all files using a single command with renaming files . Like



scp *.db user@host:/dir/(actual file name before extension)_1.db









share|improve this question















I am sending files from one server to another using scp . I need to rename files after sending that .So I use following command for each file



scp original-hc.db user@host:/dir/original-hc_1.db


I want to send all files using a single command with renaming files . Like



scp *.db user@host:/dir/(actual file name before extension)_1.db






shell-script rename scp






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago









Rui F Ribeiro

38.2k1475125




38.2k1475125










asked Aug 31 '16 at 17:23









Arya Ray

2614




2614








  • 2




    I don't believe what you're asking for is possible in a single command. As I understand it scp or any copy command for that matter will allow multiple files to copy but requires a single destination and does not allow for renaming unless you are only copying a single file. There are commands to rename lots of files at once, e.g. rename but not one command that combines both.
    – Zachary Brady
    Aug 31 '16 at 17:41














  • 2




    I don't believe what you're asking for is possible in a single command. As I understand it scp or any copy command for that matter will allow multiple files to copy but requires a single destination and does not allow for renaming unless you are only copying a single file. There are commands to rename lots of files at once, e.g. rename but not one command that combines both.
    – Zachary Brady
    Aug 31 '16 at 17:41








2




2




I don't believe what you're asking for is possible in a single command. As I understand it scp or any copy command for that matter will allow multiple files to copy but requires a single destination and does not allow for renaming unless you are only copying a single file. There are commands to rename lots of files at once, e.g. rename but not one command that combines both.
– Zachary Brady
Aug 31 '16 at 17:41




I don't believe what you're asking for is possible in a single command. As I understand it scp or any copy command for that matter will allow multiple files to copy but requires a single destination and does not allow for renaming unless you are only copying a single file. There are commands to rename lots of files at once, e.g. rename but not one command that combines both.
– Zachary Brady
Aug 31 '16 at 17:41










2 Answers
2






active

oldest

votes

















up vote
5
down vote



accepted










This can easily be achieved with a loop



for f in *.db
do
scp "$f" user@host:/dir/"${f%.db}"_1.db
done


The ${f%.db} construct strips off the .db suffix from $f.






share|improve this answer























  • This would result in a file named file.db_1.db you could use scp "$f" user@host:/dir/"${f%.db}"_1.db
    – Zachary Brady
    Aug 31 '16 at 17:37










  • @ZacharyBrady thanks. That comes of my answering on a mobile without checking my answer before posting. I must have caught it just after you commented.
    – roaima
    Aug 31 '16 at 18:12










  • @Roaima and Zachary thanks both of you . I need some more help in this regards . I have several file name which is starting with example . Like example-hc-new34.db ,example-hc-new45.db,example-diff-hc-old63.db . I only want to send which file starting with example-hc and also rename those file like example-hc-new34_1.db .
    – Arya Ray
    Aug 31 '16 at 19:31










  • @AryaRay please edit your question to provide other mappings. The solution here matched the example you gave.
    – roaima
    Aug 31 '16 at 20:49










  • @AryaRay do you not understand the meaning of the wildcard *.db? Surely it's trivial to convert this into a wildcard pattern that matches db files also starting with example-hc?
    – roaima
    Aug 31 '16 at 20:51


















up vote
2
down vote













scp can't rename files so you'll need to use some other tool in addition or instead.



If you have SFTP access and not just SCP access, then you can use SSHFS to make the remote files appear on your local machine. This allows you to use any file copy-and-renaming tool.



mkdir mnt
sshfs user@host:/dir
pax -rw -pe -s'/.db$/_1.db/' *.db mnt
fusermount -u mnt


Instead of pax (which is POSIX but sometimes not installed by default on Linux though it's always available as a package), you might use GNU or BSD tar, zsh's zcp, etc. Or just a loop that does the copy:



for x in *.db; do
cp -p "$x" "mnt/${x%.db}_1.db"
done


You can use the loop method even if you don't have SSHFS, but then you have to use scp in the loop.



for x in *.db; do
scp -p "$x" "user@host:/dir/${x%.db}_1.db"
done


Setting up an SSH connection for each time can be a little slow. With OpenSSH, you can open the connection once and then piggyback on it. See Using an already established SSH channel



Another method (requiring full shell access on the server) is to archive the files and copy the archive, and apply a renaming step when archiving or when extracting. For example, if you have GNU tar locally (which is always the case on non-embedded Linux and often available, perhaps as gtar, on other unix variants):



tar -cf - --transform '/.db$/_1.db/ *.db | ssh user@host 'cd /dir && tar -xf -'


With BSD tar, replace --transform by -s. If you have a very limited tar locally but GNU tar or BSD tar on the server, you can do the renaming on the server side instead.



You might want to insert a step of compression if the network bandwidth is the bottleneck. With the archive method, you can insert steps in the pipeline:



tar -czf - --transform '/.db$/_1.db/ *.db | ssh user@host 'cd /dir && gunzip | tar -xf -'


Or you can do the compression at the SSH level, by passing the -C option to ssh, sshfs or scp.






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',
    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
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f307022%2fusing-scp-renaming-all-files%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








    up vote
    5
    down vote



    accepted










    This can easily be achieved with a loop



    for f in *.db
    do
    scp "$f" user@host:/dir/"${f%.db}"_1.db
    done


    The ${f%.db} construct strips off the .db suffix from $f.






    share|improve this answer























    • This would result in a file named file.db_1.db you could use scp "$f" user@host:/dir/"${f%.db}"_1.db
      – Zachary Brady
      Aug 31 '16 at 17:37










    • @ZacharyBrady thanks. That comes of my answering on a mobile without checking my answer before posting. I must have caught it just after you commented.
      – roaima
      Aug 31 '16 at 18:12










    • @Roaima and Zachary thanks both of you . I need some more help in this regards . I have several file name which is starting with example . Like example-hc-new34.db ,example-hc-new45.db,example-diff-hc-old63.db . I only want to send which file starting with example-hc and also rename those file like example-hc-new34_1.db .
      – Arya Ray
      Aug 31 '16 at 19:31










    • @AryaRay please edit your question to provide other mappings. The solution here matched the example you gave.
      – roaima
      Aug 31 '16 at 20:49










    • @AryaRay do you not understand the meaning of the wildcard *.db? Surely it's trivial to convert this into a wildcard pattern that matches db files also starting with example-hc?
      – roaima
      Aug 31 '16 at 20:51















    up vote
    5
    down vote



    accepted










    This can easily be achieved with a loop



    for f in *.db
    do
    scp "$f" user@host:/dir/"${f%.db}"_1.db
    done


    The ${f%.db} construct strips off the .db suffix from $f.






    share|improve this answer























    • This would result in a file named file.db_1.db you could use scp "$f" user@host:/dir/"${f%.db}"_1.db
      – Zachary Brady
      Aug 31 '16 at 17:37










    • @ZacharyBrady thanks. That comes of my answering on a mobile without checking my answer before posting. I must have caught it just after you commented.
      – roaima
      Aug 31 '16 at 18:12










    • @Roaima and Zachary thanks both of you . I need some more help in this regards . I have several file name which is starting with example . Like example-hc-new34.db ,example-hc-new45.db,example-diff-hc-old63.db . I only want to send which file starting with example-hc and also rename those file like example-hc-new34_1.db .
      – Arya Ray
      Aug 31 '16 at 19:31










    • @AryaRay please edit your question to provide other mappings. The solution here matched the example you gave.
      – roaima
      Aug 31 '16 at 20:49










    • @AryaRay do you not understand the meaning of the wildcard *.db? Surely it's trivial to convert this into a wildcard pattern that matches db files also starting with example-hc?
      – roaima
      Aug 31 '16 at 20:51













    up vote
    5
    down vote



    accepted







    up vote
    5
    down vote



    accepted






    This can easily be achieved with a loop



    for f in *.db
    do
    scp "$f" user@host:/dir/"${f%.db}"_1.db
    done


    The ${f%.db} construct strips off the .db suffix from $f.






    share|improve this answer














    This can easily be achieved with a loop



    for f in *.db
    do
    scp "$f" user@host:/dir/"${f%.db}"_1.db
    done


    The ${f%.db} construct strips off the .db suffix from $f.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Aug 31 '16 at 18:09

























    answered Aug 31 '16 at 17:34









    roaima

    42k550115




    42k550115












    • This would result in a file named file.db_1.db you could use scp "$f" user@host:/dir/"${f%.db}"_1.db
      – Zachary Brady
      Aug 31 '16 at 17:37










    • @ZacharyBrady thanks. That comes of my answering on a mobile without checking my answer before posting. I must have caught it just after you commented.
      – roaima
      Aug 31 '16 at 18:12










    • @Roaima and Zachary thanks both of you . I need some more help in this regards . I have several file name which is starting with example . Like example-hc-new34.db ,example-hc-new45.db,example-diff-hc-old63.db . I only want to send which file starting with example-hc and also rename those file like example-hc-new34_1.db .
      – Arya Ray
      Aug 31 '16 at 19:31










    • @AryaRay please edit your question to provide other mappings. The solution here matched the example you gave.
      – roaima
      Aug 31 '16 at 20:49










    • @AryaRay do you not understand the meaning of the wildcard *.db? Surely it's trivial to convert this into a wildcard pattern that matches db files also starting with example-hc?
      – roaima
      Aug 31 '16 at 20:51


















    • This would result in a file named file.db_1.db you could use scp "$f" user@host:/dir/"${f%.db}"_1.db
      – Zachary Brady
      Aug 31 '16 at 17:37










    • @ZacharyBrady thanks. That comes of my answering on a mobile without checking my answer before posting. I must have caught it just after you commented.
      – roaima
      Aug 31 '16 at 18:12










    • @Roaima and Zachary thanks both of you . I need some more help in this regards . I have several file name which is starting with example . Like example-hc-new34.db ,example-hc-new45.db,example-diff-hc-old63.db . I only want to send which file starting with example-hc and also rename those file like example-hc-new34_1.db .
      – Arya Ray
      Aug 31 '16 at 19:31










    • @AryaRay please edit your question to provide other mappings. The solution here matched the example you gave.
      – roaima
      Aug 31 '16 at 20:49










    • @AryaRay do you not understand the meaning of the wildcard *.db? Surely it's trivial to convert this into a wildcard pattern that matches db files also starting with example-hc?
      – roaima
      Aug 31 '16 at 20:51
















    This would result in a file named file.db_1.db you could use scp "$f" user@host:/dir/"${f%.db}"_1.db
    – Zachary Brady
    Aug 31 '16 at 17:37




    This would result in a file named file.db_1.db you could use scp "$f" user@host:/dir/"${f%.db}"_1.db
    – Zachary Brady
    Aug 31 '16 at 17:37












    @ZacharyBrady thanks. That comes of my answering on a mobile without checking my answer before posting. I must have caught it just after you commented.
    – roaima
    Aug 31 '16 at 18:12




    @ZacharyBrady thanks. That comes of my answering on a mobile without checking my answer before posting. I must have caught it just after you commented.
    – roaima
    Aug 31 '16 at 18:12












    @Roaima and Zachary thanks both of you . I need some more help in this regards . I have several file name which is starting with example . Like example-hc-new34.db ,example-hc-new45.db,example-diff-hc-old63.db . I only want to send which file starting with example-hc and also rename those file like example-hc-new34_1.db .
    – Arya Ray
    Aug 31 '16 at 19:31




    @Roaima and Zachary thanks both of you . I need some more help in this regards . I have several file name which is starting with example . Like example-hc-new34.db ,example-hc-new45.db,example-diff-hc-old63.db . I only want to send which file starting with example-hc and also rename those file like example-hc-new34_1.db .
    – Arya Ray
    Aug 31 '16 at 19:31












    @AryaRay please edit your question to provide other mappings. The solution here matched the example you gave.
    – roaima
    Aug 31 '16 at 20:49




    @AryaRay please edit your question to provide other mappings. The solution here matched the example you gave.
    – roaima
    Aug 31 '16 at 20:49












    @AryaRay do you not understand the meaning of the wildcard *.db? Surely it's trivial to convert this into a wildcard pattern that matches db files also starting with example-hc?
    – roaima
    Aug 31 '16 at 20:51




    @AryaRay do you not understand the meaning of the wildcard *.db? Surely it's trivial to convert this into a wildcard pattern that matches db files also starting with example-hc?
    – roaima
    Aug 31 '16 at 20:51












    up vote
    2
    down vote













    scp can't rename files so you'll need to use some other tool in addition or instead.



    If you have SFTP access and not just SCP access, then you can use SSHFS to make the remote files appear on your local machine. This allows you to use any file copy-and-renaming tool.



    mkdir mnt
    sshfs user@host:/dir
    pax -rw -pe -s'/.db$/_1.db/' *.db mnt
    fusermount -u mnt


    Instead of pax (which is POSIX but sometimes not installed by default on Linux though it's always available as a package), you might use GNU or BSD tar, zsh's zcp, etc. Or just a loop that does the copy:



    for x in *.db; do
    cp -p "$x" "mnt/${x%.db}_1.db"
    done


    You can use the loop method even if you don't have SSHFS, but then you have to use scp in the loop.



    for x in *.db; do
    scp -p "$x" "user@host:/dir/${x%.db}_1.db"
    done


    Setting up an SSH connection for each time can be a little slow. With OpenSSH, you can open the connection once and then piggyback on it. See Using an already established SSH channel



    Another method (requiring full shell access on the server) is to archive the files and copy the archive, and apply a renaming step when archiving or when extracting. For example, if you have GNU tar locally (which is always the case on non-embedded Linux and often available, perhaps as gtar, on other unix variants):



    tar -cf - --transform '/.db$/_1.db/ *.db | ssh user@host 'cd /dir && tar -xf -'


    With BSD tar, replace --transform by -s. If you have a very limited tar locally but GNU tar or BSD tar on the server, you can do the renaming on the server side instead.



    You might want to insert a step of compression if the network bandwidth is the bottleneck. With the archive method, you can insert steps in the pipeline:



    tar -czf - --transform '/.db$/_1.db/ *.db | ssh user@host 'cd /dir && gunzip | tar -xf -'


    Or you can do the compression at the SSH level, by passing the -C option to ssh, sshfs or scp.






    share|improve this answer



























      up vote
      2
      down vote













      scp can't rename files so you'll need to use some other tool in addition or instead.



      If you have SFTP access and not just SCP access, then you can use SSHFS to make the remote files appear on your local machine. This allows you to use any file copy-and-renaming tool.



      mkdir mnt
      sshfs user@host:/dir
      pax -rw -pe -s'/.db$/_1.db/' *.db mnt
      fusermount -u mnt


      Instead of pax (which is POSIX but sometimes not installed by default on Linux though it's always available as a package), you might use GNU or BSD tar, zsh's zcp, etc. Or just a loop that does the copy:



      for x in *.db; do
      cp -p "$x" "mnt/${x%.db}_1.db"
      done


      You can use the loop method even if you don't have SSHFS, but then you have to use scp in the loop.



      for x in *.db; do
      scp -p "$x" "user@host:/dir/${x%.db}_1.db"
      done


      Setting up an SSH connection for each time can be a little slow. With OpenSSH, you can open the connection once and then piggyback on it. See Using an already established SSH channel



      Another method (requiring full shell access on the server) is to archive the files and copy the archive, and apply a renaming step when archiving or when extracting. For example, if you have GNU tar locally (which is always the case on non-embedded Linux and often available, perhaps as gtar, on other unix variants):



      tar -cf - --transform '/.db$/_1.db/ *.db | ssh user@host 'cd /dir && tar -xf -'


      With BSD tar, replace --transform by -s. If you have a very limited tar locally but GNU tar or BSD tar on the server, you can do the renaming on the server side instead.



      You might want to insert a step of compression if the network bandwidth is the bottleneck. With the archive method, you can insert steps in the pipeline:



      tar -czf - --transform '/.db$/_1.db/ *.db | ssh user@host 'cd /dir && gunzip | tar -xf -'


      Or you can do the compression at the SSH level, by passing the -C option to ssh, sshfs or scp.






      share|improve this answer

























        up vote
        2
        down vote










        up vote
        2
        down vote









        scp can't rename files so you'll need to use some other tool in addition or instead.



        If you have SFTP access and not just SCP access, then you can use SSHFS to make the remote files appear on your local machine. This allows you to use any file copy-and-renaming tool.



        mkdir mnt
        sshfs user@host:/dir
        pax -rw -pe -s'/.db$/_1.db/' *.db mnt
        fusermount -u mnt


        Instead of pax (which is POSIX but sometimes not installed by default on Linux though it's always available as a package), you might use GNU or BSD tar, zsh's zcp, etc. Or just a loop that does the copy:



        for x in *.db; do
        cp -p "$x" "mnt/${x%.db}_1.db"
        done


        You can use the loop method even if you don't have SSHFS, but then you have to use scp in the loop.



        for x in *.db; do
        scp -p "$x" "user@host:/dir/${x%.db}_1.db"
        done


        Setting up an SSH connection for each time can be a little slow. With OpenSSH, you can open the connection once and then piggyback on it. See Using an already established SSH channel



        Another method (requiring full shell access on the server) is to archive the files and copy the archive, and apply a renaming step when archiving or when extracting. For example, if you have GNU tar locally (which is always the case on non-embedded Linux and often available, perhaps as gtar, on other unix variants):



        tar -cf - --transform '/.db$/_1.db/ *.db | ssh user@host 'cd /dir && tar -xf -'


        With BSD tar, replace --transform by -s. If you have a very limited tar locally but GNU tar or BSD tar on the server, you can do the renaming on the server side instead.



        You might want to insert a step of compression if the network bandwidth is the bottleneck. With the archive method, you can insert steps in the pipeline:



        tar -czf - --transform '/.db$/_1.db/ *.db | ssh user@host 'cd /dir && gunzip | tar -xf -'


        Or you can do the compression at the SSH level, by passing the -C option to ssh, sshfs or scp.






        share|improve this answer














        scp can't rename files so you'll need to use some other tool in addition or instead.



        If you have SFTP access and not just SCP access, then you can use SSHFS to make the remote files appear on your local machine. This allows you to use any file copy-and-renaming tool.



        mkdir mnt
        sshfs user@host:/dir
        pax -rw -pe -s'/.db$/_1.db/' *.db mnt
        fusermount -u mnt


        Instead of pax (which is POSIX but sometimes not installed by default on Linux though it's always available as a package), you might use GNU or BSD tar, zsh's zcp, etc. Or just a loop that does the copy:



        for x in *.db; do
        cp -p "$x" "mnt/${x%.db}_1.db"
        done


        You can use the loop method even if you don't have SSHFS, but then you have to use scp in the loop.



        for x in *.db; do
        scp -p "$x" "user@host:/dir/${x%.db}_1.db"
        done


        Setting up an SSH connection for each time can be a little slow. With OpenSSH, you can open the connection once and then piggyback on it. See Using an already established SSH channel



        Another method (requiring full shell access on the server) is to archive the files and copy the archive, and apply a renaming step when archiving or when extracting. For example, if you have GNU tar locally (which is always the case on non-embedded Linux and often available, perhaps as gtar, on other unix variants):



        tar -cf - --transform '/.db$/_1.db/ *.db | ssh user@host 'cd /dir && tar -xf -'


        With BSD tar, replace --transform by -s. If you have a very limited tar locally but GNU tar or BSD tar on the server, you can do the renaming on the server side instead.



        You might want to insert a step of compression if the network bandwidth is the bottleneck. With the archive method, you can insert steps in the pipeline:



        tar -czf - --transform '/.db$/_1.db/ *.db | ssh user@host 'cd /dir && gunzip | tar -xf -'


        Or you can do the compression at the SSH level, by passing the -C option to ssh, sshfs or scp.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Apr 13 '17 at 12:36









        Community

        1




        1










        answered Sep 1 '16 at 7:51









        Gilles

        521k12610401570




        521k12610401570






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f307022%2fusing-scp-renaming-all-files%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

            サソリ

            広島県道265号伴広島線

            Accessing regular linux commands in Huawei's Dopra Linux