copy files from one path to another path in linux

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite
1












I am trying to copy files from one path to another path. I have a text file which has all names of files in the following pattern:



file-1.txt
file-2.pdf
file-3.ppt
....


I created a .sh file with the following code:



 #!/bin/bash
file=`cat filenames.txt`;
fromPath='/root/Backup/upload/';
toPath='/root/Desktop/custom/upload/';
for i in $file;
do
filePath=$fromPath$i
#echo $filePath
if [ -e $filePath ];
then
echo $filePath
yes | cp -rf $filePath $toPath
else
echo 'no files'
fi
done


The above code is copying only the last file name from the text instead of all to the destination path.










share|improve this question























  • protip: ` syntax is deprecated. use $() instead.
    – strugee
    Apr 8 '14 at 10:51














up vote
1
down vote

favorite
1












I am trying to copy files from one path to another path. I have a text file which has all names of files in the following pattern:



file-1.txt
file-2.pdf
file-3.ppt
....


I created a .sh file with the following code:



 #!/bin/bash
file=`cat filenames.txt`;
fromPath='/root/Backup/upload/';
toPath='/root/Desktop/custom/upload/';
for i in $file;
do
filePath=$fromPath$i
#echo $filePath
if [ -e $filePath ];
then
echo $filePath
yes | cp -rf $filePath $toPath
else
echo 'no files'
fi
done


The above code is copying only the last file name from the text instead of all to the destination path.










share|improve this question























  • protip: ` syntax is deprecated. use $() instead.
    – strugee
    Apr 8 '14 at 10:51












up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





I am trying to copy files from one path to another path. I have a text file which has all names of files in the following pattern:



file-1.txt
file-2.pdf
file-3.ppt
....


I created a .sh file with the following code:



 #!/bin/bash
file=`cat filenames.txt`;
fromPath='/root/Backup/upload/';
toPath='/root/Desktop/custom/upload/';
for i in $file;
do
filePath=$fromPath$i
#echo $filePath
if [ -e $filePath ];
then
echo $filePath
yes | cp -rf $filePath $toPath
else
echo 'no files'
fi
done


The above code is copying only the last file name from the text instead of all to the destination path.










share|improve this question















I am trying to copy files from one path to another path. I have a text file which has all names of files in the following pattern:



file-1.txt
file-2.pdf
file-3.ppt
....


I created a .sh file with the following code:



 #!/bin/bash
file=`cat filenames.txt`;
fromPath='/root/Backup/upload/';
toPath='/root/Desktop/custom/upload/';
for i in $file;
do
filePath=$fromPath$i
#echo $filePath
if [ -e $filePath ];
then
echo $filePath
yes | cp -rf $filePath $toPath
else
echo 'no files'
fi
done


The above code is copying only the last file name from the text instead of all to the destination path.







bash shell-script file-copy






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 at 20:33









Rui F Ribeiro

38.3k1476127




38.3k1476127










asked Apr 8 '14 at 10:08









Mr_Green

108115




108115











  • protip: ` syntax is deprecated. use $() instead.
    – strugee
    Apr 8 '14 at 10:51
















  • protip: ` syntax is deprecated. use $() instead.
    – strugee
    Apr 8 '14 at 10:51















protip: ` syntax is deprecated. use $() instead.
– strugee
Apr 8 '14 at 10:51




protip: ` syntax is deprecated. use $() instead.
– strugee
Apr 8 '14 at 10:51










2 Answers
2






active

oldest

votes

















up vote
3
down vote



accepted










file=/path/to/filenames.txt
fromPath=/root/Backup/upload/
toPath=/root/Desktop/custom/upload/

cd "$fromPath" && xargs mv -t "$toPath" < "$file"





share|improve this answer




















  • The problem was that each line in the text file was terminating differently instead of just n.. may be like nr.. I run sed 's:r$::' -i filename.txt at the text file path and run my script again. which worked fine. Anyway thanks for your short answer +1 :)
    – Mr_Green
    Apr 8 '14 at 11:24






  • 1




    @Mr_Green If your file had nr as new line characters, then may be it was copied from a windows machine. You may run the command "dos2unix filename" and then run your original script. It should work then.
    – Gautam Somani
    Apr 9 '14 at 9:29










  • @GautamSomani yes it was copied from a windows machine. Thanks for the tip :)
    – Mr_Green
    Apr 9 '14 at 9:44

















up vote
1
down vote













You might take a look at rsync, if you're not already familiar with it. This looks like a problem that shouldn't really require a script of its own.



Take a look here, or use your Google foo.



The rsync option you need is probably --files-from.



The rsync incantation will be something like:



rsync --files-from filenames.txt /root/Backup/upload /root/Desktop/custom/upload





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%2f123707%2fcopy-files-from-one-path-to-another-path-in-linux%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
    3
    down vote



    accepted










    file=/path/to/filenames.txt
    fromPath=/root/Backup/upload/
    toPath=/root/Desktop/custom/upload/

    cd "$fromPath" && xargs mv -t "$toPath" < "$file"





    share|improve this answer




















    • The problem was that each line in the text file was terminating differently instead of just n.. may be like nr.. I run sed 's:r$::' -i filename.txt at the text file path and run my script again. which worked fine. Anyway thanks for your short answer +1 :)
      – Mr_Green
      Apr 8 '14 at 11:24






    • 1




      @Mr_Green If your file had nr as new line characters, then may be it was copied from a windows machine. You may run the command "dos2unix filename" and then run your original script. It should work then.
      – Gautam Somani
      Apr 9 '14 at 9:29










    • @GautamSomani yes it was copied from a windows machine. Thanks for the tip :)
      – Mr_Green
      Apr 9 '14 at 9:44














    up vote
    3
    down vote



    accepted










    file=/path/to/filenames.txt
    fromPath=/root/Backup/upload/
    toPath=/root/Desktop/custom/upload/

    cd "$fromPath" && xargs mv -t "$toPath" < "$file"





    share|improve this answer




















    • The problem was that each line in the text file was terminating differently instead of just n.. may be like nr.. I run sed 's:r$::' -i filename.txt at the text file path and run my script again. which worked fine. Anyway thanks for your short answer +1 :)
      – Mr_Green
      Apr 8 '14 at 11:24






    • 1




      @Mr_Green If your file had nr as new line characters, then may be it was copied from a windows machine. You may run the command "dos2unix filename" and then run your original script. It should work then.
      – Gautam Somani
      Apr 9 '14 at 9:29










    • @GautamSomani yes it was copied from a windows machine. Thanks for the tip :)
      – Mr_Green
      Apr 9 '14 at 9:44












    up vote
    3
    down vote



    accepted







    up vote
    3
    down vote



    accepted






    file=/path/to/filenames.txt
    fromPath=/root/Backup/upload/
    toPath=/root/Desktop/custom/upload/

    cd "$fromPath" && xargs mv -t "$toPath" < "$file"





    share|improve this answer












    file=/path/to/filenames.txt
    fromPath=/root/Backup/upload/
    toPath=/root/Desktop/custom/upload/

    cd "$fromPath" && xargs mv -t "$toPath" < "$file"






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Apr 8 '14 at 11:10









    glenn jackman

    49.6k569106




    49.6k569106











    • The problem was that each line in the text file was terminating differently instead of just n.. may be like nr.. I run sed 's:r$::' -i filename.txt at the text file path and run my script again. which worked fine. Anyway thanks for your short answer +1 :)
      – Mr_Green
      Apr 8 '14 at 11:24






    • 1




      @Mr_Green If your file had nr as new line characters, then may be it was copied from a windows machine. You may run the command "dos2unix filename" and then run your original script. It should work then.
      – Gautam Somani
      Apr 9 '14 at 9:29










    • @GautamSomani yes it was copied from a windows machine. Thanks for the tip :)
      – Mr_Green
      Apr 9 '14 at 9:44
















    • The problem was that each line in the text file was terminating differently instead of just n.. may be like nr.. I run sed 's:r$::' -i filename.txt at the text file path and run my script again. which worked fine. Anyway thanks for your short answer +1 :)
      – Mr_Green
      Apr 8 '14 at 11:24






    • 1




      @Mr_Green If your file had nr as new line characters, then may be it was copied from a windows machine. You may run the command "dos2unix filename" and then run your original script. It should work then.
      – Gautam Somani
      Apr 9 '14 at 9:29










    • @GautamSomani yes it was copied from a windows machine. Thanks for the tip :)
      – Mr_Green
      Apr 9 '14 at 9:44















    The problem was that each line in the text file was terminating differently instead of just n.. may be like nr.. I run sed 's:r$::' -i filename.txt at the text file path and run my script again. which worked fine. Anyway thanks for your short answer +1 :)
    – Mr_Green
    Apr 8 '14 at 11:24




    The problem was that each line in the text file was terminating differently instead of just n.. may be like nr.. I run sed 's:r$::' -i filename.txt at the text file path and run my script again. which worked fine. Anyway thanks for your short answer +1 :)
    – Mr_Green
    Apr 8 '14 at 11:24




    1




    1




    @Mr_Green If your file had nr as new line characters, then may be it was copied from a windows machine. You may run the command "dos2unix filename" and then run your original script. It should work then.
    – Gautam Somani
    Apr 9 '14 at 9:29




    @Mr_Green If your file had nr as new line characters, then may be it was copied from a windows machine. You may run the command "dos2unix filename" and then run your original script. It should work then.
    – Gautam Somani
    Apr 9 '14 at 9:29












    @GautamSomani yes it was copied from a windows machine. Thanks for the tip :)
    – Mr_Green
    Apr 9 '14 at 9:44




    @GautamSomani yes it was copied from a windows machine. Thanks for the tip :)
    – Mr_Green
    Apr 9 '14 at 9:44












    up vote
    1
    down vote













    You might take a look at rsync, if you're not already familiar with it. This looks like a problem that shouldn't really require a script of its own.



    Take a look here, or use your Google foo.



    The rsync option you need is probably --files-from.



    The rsync incantation will be something like:



    rsync --files-from filenames.txt /root/Backup/upload /root/Desktop/custom/upload





    share|improve this answer


























      up vote
      1
      down vote













      You might take a look at rsync, if you're not already familiar with it. This looks like a problem that shouldn't really require a script of its own.



      Take a look here, or use your Google foo.



      The rsync option you need is probably --files-from.



      The rsync incantation will be something like:



      rsync --files-from filenames.txt /root/Backup/upload /root/Desktop/custom/upload





      share|improve this answer
























        up vote
        1
        down vote










        up vote
        1
        down vote









        You might take a look at rsync, if you're not already familiar with it. This looks like a problem that shouldn't really require a script of its own.



        Take a look here, or use your Google foo.



        The rsync option you need is probably --files-from.



        The rsync incantation will be something like:



        rsync --files-from filenames.txt /root/Backup/upload /root/Desktop/custom/upload





        share|improve this answer














        You might take a look at rsync, if you're not already familiar with it. This looks like a problem that shouldn't really require a script of its own.



        Take a look here, or use your Google foo.



        The rsync option you need is probably --files-from.



        The rsync incantation will be something like:



        rsync --files-from filenames.txt /root/Backup/upload /root/Desktop/custom/upload






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Apr 8 '14 at 11:34

























        answered Apr 8 '14 at 11:27









        Stephen Blott

        17114




        17114



























            draft saved

            draft discarded
















































            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f123707%2fcopy-files-from-one-path-to-another-path-in-linux%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 check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay