How to redirect all the content of one file to another file?

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











up vote
-4
down vote

favorite












I have two files: file_a and file_b. I want to redirect (>) all content of file_a into file_b.



pseudo code is file_a > file_b. how to do that? I feel I should use cat.







share|improve this question




















  • cat file_a > file_b
    – RomanPerekhrest
    Feb 1 at 14:53






  • 2




    @RomanPerekhrest pleaes make that an answer. Trivial though it is.
    – roaima
    Feb 1 at 14:53






  • 2




    @roaima, I can't: it's too primitive and not imposing ...
    – RomanPerekhrest
    Feb 1 at 14:54











  • @roaima It's only "trivial" if you disregard what's going on ;-) (what's opening what file and what's happening to permissions etc. in various cases)
    – Kusalananda
    Feb 1 at 15:42











  • @Kusalananda at the level the OP is asking the simple form of the answer is trivial ("How do you get a directory listing? - ls"), although I accept that if you have zero knowledge nothing is really trivial. I like your answer because it gives a solution at the simplest possible level, but then goes on to explain for other readers some of the extras that might be relelvant.
    – roaima
    Feb 1 at 19:50














up vote
-4
down vote

favorite












I have two files: file_a and file_b. I want to redirect (>) all content of file_a into file_b.



pseudo code is file_a > file_b. how to do that? I feel I should use cat.







share|improve this question




















  • cat file_a > file_b
    – RomanPerekhrest
    Feb 1 at 14:53






  • 2




    @RomanPerekhrest pleaes make that an answer. Trivial though it is.
    – roaima
    Feb 1 at 14:53






  • 2




    @roaima, I can't: it's too primitive and not imposing ...
    – RomanPerekhrest
    Feb 1 at 14:54











  • @roaima It's only "trivial" if you disregard what's going on ;-) (what's opening what file and what's happening to permissions etc. in various cases)
    – Kusalananda
    Feb 1 at 15:42











  • @Kusalananda at the level the OP is asking the simple form of the answer is trivial ("How do you get a directory listing? - ls"), although I accept that if you have zero knowledge nothing is really trivial. I like your answer because it gives a solution at the simplest possible level, but then goes on to explain for other readers some of the extras that might be relelvant.
    – roaima
    Feb 1 at 19:50












up vote
-4
down vote

favorite









up vote
-4
down vote

favorite











I have two files: file_a and file_b. I want to redirect (>) all content of file_a into file_b.



pseudo code is file_a > file_b. how to do that? I feel I should use cat.







share|improve this question












I have two files: file_a and file_b. I want to redirect (>) all content of file_a into file_b.



pseudo code is file_a > file_b. how to do that? I feel I should use cat.









share|improve this question











share|improve this question




share|improve this question










asked Feb 1 at 14:51









redirector

61




61











  • cat file_a > file_b
    – RomanPerekhrest
    Feb 1 at 14:53






  • 2




    @RomanPerekhrest pleaes make that an answer. Trivial though it is.
    – roaima
    Feb 1 at 14:53






  • 2




    @roaima, I can't: it's too primitive and not imposing ...
    – RomanPerekhrest
    Feb 1 at 14:54











  • @roaima It's only "trivial" if you disregard what's going on ;-) (what's opening what file and what's happening to permissions etc. in various cases)
    – Kusalananda
    Feb 1 at 15:42











  • @Kusalananda at the level the OP is asking the simple form of the answer is trivial ("How do you get a directory listing? - ls"), although I accept that if you have zero knowledge nothing is really trivial. I like your answer because it gives a solution at the simplest possible level, but then goes on to explain for other readers some of the extras that might be relelvant.
    – roaima
    Feb 1 at 19:50
















  • cat file_a > file_b
    – RomanPerekhrest
    Feb 1 at 14:53






  • 2




    @RomanPerekhrest pleaes make that an answer. Trivial though it is.
    – roaima
    Feb 1 at 14:53






  • 2




    @roaima, I can't: it's too primitive and not imposing ...
    – RomanPerekhrest
    Feb 1 at 14:54











  • @roaima It's only "trivial" if you disregard what's going on ;-) (what's opening what file and what's happening to permissions etc. in various cases)
    – Kusalananda
    Feb 1 at 15:42











  • @Kusalananda at the level the OP is asking the simple form of the answer is trivial ("How do you get a directory listing? - ls"), although I accept that if you have zero knowledge nothing is really trivial. I like your answer because it gives a solution at the simplest possible level, but then goes on to explain for other readers some of the extras that might be relelvant.
    – roaima
    Feb 1 at 19:50















cat file_a > file_b
– RomanPerekhrest
Feb 1 at 14:53




cat file_a > file_b
– RomanPerekhrest
Feb 1 at 14:53




2




2




@RomanPerekhrest pleaes make that an answer. Trivial though it is.
– roaima
Feb 1 at 14:53




@RomanPerekhrest pleaes make that an answer. Trivial though it is.
– roaima
Feb 1 at 14:53




2




2




@roaima, I can't: it's too primitive and not imposing ...
– RomanPerekhrest
Feb 1 at 14:54





@roaima, I can't: it's too primitive and not imposing ...
– RomanPerekhrest
Feb 1 at 14:54













@roaima It's only "trivial" if you disregard what's going on ;-) (what's opening what file and what's happening to permissions etc. in various cases)
– Kusalananda
Feb 1 at 15:42





@roaima It's only "trivial" if you disregard what's going on ;-) (what's opening what file and what's happening to permissions etc. in various cases)
– Kusalananda
Feb 1 at 15:42













@Kusalananda at the level the OP is asking the simple form of the answer is trivial ("How do you get a directory listing? - ls"), although I accept that if you have zero knowledge nothing is really trivial. I like your answer because it gives a solution at the simplest possible level, but then goes on to explain for other readers some of the extras that might be relelvant.
– roaima
Feb 1 at 19:50




@Kusalananda at the level the OP is asking the simple form of the answer is trivial ("How do you get a directory listing? - ls"), although I accept that if you have zero knowledge nothing is really trivial. I like your answer because it gives a solution at the simplest possible level, but then goes on to explain for other readers some of the extras that might be relelvant.
– roaima
Feb 1 at 19:50










2 Answers
2






active

oldest

votes

















up vote
8
down vote



accepted










Nitpick: In Unix, you can redirect output or input streams, but you can't redirect files.



As RomanPerekhrest suggested in comments to the question:



cat file_a >file_b


This redirects the standard output stream (or just "the output") of cat into file_b. The output of cat will be the contents of file_a.



This has the same effect (disregarding edge-case differences regarding permissions and ownership) as



cp file_a file_b


There are many other ways of duplicating a file's complete contents into another file, including trivial examples that apply non-modifying filters on text files, such as



dd if=file_a of=file_b

awk '1' file_a >file_b

sed '' file_a >file_b


etc.



All the above examples will overwrite the previous contents of file_b.



To append to file_b, replace > in the examples above that uses > with >>, e.g.



cat file_a >>file_b


To append the contents of file_a to that of file_b and store that in a third file:



cat file_b file_a >file_c


cat will output the contents of each of its file arguments after each other, in order, and the result will be redirected into the new file_c file.






share|improve this answer


















  • 1




    Depending on the implementation, the sed/awk ones may not work for non-text files. The awk one would add back a missing trailing newline character (sed as well with some implementations, some others would remove the unterminated line)
    – Stéphane Chazelas
    Feb 1 at 15:48










  • @StéphaneChazelas Yes. I've changed my wording to be slightly more careful.
    – Kusalananda
    Feb 1 at 15:52






  • 1




    I think what would make your nitpick even better is to say that you can redirect standard streams, or perhaps file descriptors in general. "Output of a program" can be arguably stretched as a term.
    – Sergiy Kolodyazhnyy
    Feb 1 at 16:47

















up vote
0
down vote













simple way is to copy:



cp file1 file2


there are multiple ways to divert the flow of output data into another file as well e.g.:



cat file1 > file2
less file1 > file2
more file1 > file2


You can use rsync to take backup:



rsync -r /media/hdd1/Folder1/ /media/hdd2/Folder2/


rsync is backing up whole folder from one drive to another and -r option specifies to do it recursively for all files and subfolders.






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: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    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%2f421219%2fhow-to-redirect-all-the-content-of-one-file-to-another-file%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    8
    down vote



    accepted










    Nitpick: In Unix, you can redirect output or input streams, but you can't redirect files.



    As RomanPerekhrest suggested in comments to the question:



    cat file_a >file_b


    This redirects the standard output stream (or just "the output") of cat into file_b. The output of cat will be the contents of file_a.



    This has the same effect (disregarding edge-case differences regarding permissions and ownership) as



    cp file_a file_b


    There are many other ways of duplicating a file's complete contents into another file, including trivial examples that apply non-modifying filters on text files, such as



    dd if=file_a of=file_b

    awk '1' file_a >file_b

    sed '' file_a >file_b


    etc.



    All the above examples will overwrite the previous contents of file_b.



    To append to file_b, replace > in the examples above that uses > with >>, e.g.



    cat file_a >>file_b


    To append the contents of file_a to that of file_b and store that in a third file:



    cat file_b file_a >file_c


    cat will output the contents of each of its file arguments after each other, in order, and the result will be redirected into the new file_c file.






    share|improve this answer


















    • 1




      Depending on the implementation, the sed/awk ones may not work for non-text files. The awk one would add back a missing trailing newline character (sed as well with some implementations, some others would remove the unterminated line)
      – Stéphane Chazelas
      Feb 1 at 15:48










    • @StéphaneChazelas Yes. I've changed my wording to be slightly more careful.
      – Kusalananda
      Feb 1 at 15:52






    • 1




      I think what would make your nitpick even better is to say that you can redirect standard streams, or perhaps file descriptors in general. "Output of a program" can be arguably stretched as a term.
      – Sergiy Kolodyazhnyy
      Feb 1 at 16:47














    up vote
    8
    down vote



    accepted










    Nitpick: In Unix, you can redirect output or input streams, but you can't redirect files.



    As RomanPerekhrest suggested in comments to the question:



    cat file_a >file_b


    This redirects the standard output stream (or just "the output") of cat into file_b. The output of cat will be the contents of file_a.



    This has the same effect (disregarding edge-case differences regarding permissions and ownership) as



    cp file_a file_b


    There are many other ways of duplicating a file's complete contents into another file, including trivial examples that apply non-modifying filters on text files, such as



    dd if=file_a of=file_b

    awk '1' file_a >file_b

    sed '' file_a >file_b


    etc.



    All the above examples will overwrite the previous contents of file_b.



    To append to file_b, replace > in the examples above that uses > with >>, e.g.



    cat file_a >>file_b


    To append the contents of file_a to that of file_b and store that in a third file:



    cat file_b file_a >file_c


    cat will output the contents of each of its file arguments after each other, in order, and the result will be redirected into the new file_c file.






    share|improve this answer


















    • 1




      Depending on the implementation, the sed/awk ones may not work for non-text files. The awk one would add back a missing trailing newline character (sed as well with some implementations, some others would remove the unterminated line)
      – Stéphane Chazelas
      Feb 1 at 15:48










    • @StéphaneChazelas Yes. I've changed my wording to be slightly more careful.
      – Kusalananda
      Feb 1 at 15:52






    • 1




      I think what would make your nitpick even better is to say that you can redirect standard streams, or perhaps file descriptors in general. "Output of a program" can be arguably stretched as a term.
      – Sergiy Kolodyazhnyy
      Feb 1 at 16:47












    up vote
    8
    down vote



    accepted







    up vote
    8
    down vote



    accepted






    Nitpick: In Unix, you can redirect output or input streams, but you can't redirect files.



    As RomanPerekhrest suggested in comments to the question:



    cat file_a >file_b


    This redirects the standard output stream (or just "the output") of cat into file_b. The output of cat will be the contents of file_a.



    This has the same effect (disregarding edge-case differences regarding permissions and ownership) as



    cp file_a file_b


    There are many other ways of duplicating a file's complete contents into another file, including trivial examples that apply non-modifying filters on text files, such as



    dd if=file_a of=file_b

    awk '1' file_a >file_b

    sed '' file_a >file_b


    etc.



    All the above examples will overwrite the previous contents of file_b.



    To append to file_b, replace > in the examples above that uses > with >>, e.g.



    cat file_a >>file_b


    To append the contents of file_a to that of file_b and store that in a third file:



    cat file_b file_a >file_c


    cat will output the contents of each of its file arguments after each other, in order, and the result will be redirected into the new file_c file.






    share|improve this answer














    Nitpick: In Unix, you can redirect output or input streams, but you can't redirect files.



    As RomanPerekhrest suggested in comments to the question:



    cat file_a >file_b


    This redirects the standard output stream (or just "the output") of cat into file_b. The output of cat will be the contents of file_a.



    This has the same effect (disregarding edge-case differences regarding permissions and ownership) as



    cp file_a file_b


    There are many other ways of duplicating a file's complete contents into another file, including trivial examples that apply non-modifying filters on text files, such as



    dd if=file_a of=file_b

    awk '1' file_a >file_b

    sed '' file_a >file_b


    etc.



    All the above examples will overwrite the previous contents of file_b.



    To append to file_b, replace > in the examples above that uses > with >>, e.g.



    cat file_a >>file_b


    To append the contents of file_a to that of file_b and store that in a third file:



    cat file_b file_a >file_c


    cat will output the contents of each of its file arguments after each other, in order, and the result will be redirected into the new file_c file.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Feb 1 at 16:51

























    answered Feb 1 at 14:54









    Kusalananda

    103k13202318




    103k13202318







    • 1




      Depending on the implementation, the sed/awk ones may not work for non-text files. The awk one would add back a missing trailing newline character (sed as well with some implementations, some others would remove the unterminated line)
      – Stéphane Chazelas
      Feb 1 at 15:48










    • @StéphaneChazelas Yes. I've changed my wording to be slightly more careful.
      – Kusalananda
      Feb 1 at 15:52






    • 1




      I think what would make your nitpick even better is to say that you can redirect standard streams, or perhaps file descriptors in general. "Output of a program" can be arguably stretched as a term.
      – Sergiy Kolodyazhnyy
      Feb 1 at 16:47












    • 1




      Depending on the implementation, the sed/awk ones may not work for non-text files. The awk one would add back a missing trailing newline character (sed as well with some implementations, some others would remove the unterminated line)
      – Stéphane Chazelas
      Feb 1 at 15:48










    • @StéphaneChazelas Yes. I've changed my wording to be slightly more careful.
      – Kusalananda
      Feb 1 at 15:52






    • 1




      I think what would make your nitpick even better is to say that you can redirect standard streams, or perhaps file descriptors in general. "Output of a program" can be arguably stretched as a term.
      – Sergiy Kolodyazhnyy
      Feb 1 at 16:47







    1




    1




    Depending on the implementation, the sed/awk ones may not work for non-text files. The awk one would add back a missing trailing newline character (sed as well with some implementations, some others would remove the unterminated line)
    – Stéphane Chazelas
    Feb 1 at 15:48




    Depending on the implementation, the sed/awk ones may not work for non-text files. The awk one would add back a missing trailing newline character (sed as well with some implementations, some others would remove the unterminated line)
    – Stéphane Chazelas
    Feb 1 at 15:48












    @StéphaneChazelas Yes. I've changed my wording to be slightly more careful.
    – Kusalananda
    Feb 1 at 15:52




    @StéphaneChazelas Yes. I've changed my wording to be slightly more careful.
    – Kusalananda
    Feb 1 at 15:52




    1




    1




    I think what would make your nitpick even better is to say that you can redirect standard streams, or perhaps file descriptors in general. "Output of a program" can be arguably stretched as a term.
    – Sergiy Kolodyazhnyy
    Feb 1 at 16:47




    I think what would make your nitpick even better is to say that you can redirect standard streams, or perhaps file descriptors in general. "Output of a program" can be arguably stretched as a term.
    – Sergiy Kolodyazhnyy
    Feb 1 at 16:47












    up vote
    0
    down vote













    simple way is to copy:



    cp file1 file2


    there are multiple ways to divert the flow of output data into another file as well e.g.:



    cat file1 > file2
    less file1 > file2
    more file1 > file2


    You can use rsync to take backup:



    rsync -r /media/hdd1/Folder1/ /media/hdd2/Folder2/


    rsync is backing up whole folder from one drive to another and -r option specifies to do it recursively for all files and subfolders.






    share|improve this answer
























      up vote
      0
      down vote













      simple way is to copy:



      cp file1 file2


      there are multiple ways to divert the flow of output data into another file as well e.g.:



      cat file1 > file2
      less file1 > file2
      more file1 > file2


      You can use rsync to take backup:



      rsync -r /media/hdd1/Folder1/ /media/hdd2/Folder2/


      rsync is backing up whole folder from one drive to another and -r option specifies to do it recursively for all files and subfolders.






      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        simple way is to copy:



        cp file1 file2


        there are multiple ways to divert the flow of output data into another file as well e.g.:



        cat file1 > file2
        less file1 > file2
        more file1 > file2


        You can use rsync to take backup:



        rsync -r /media/hdd1/Folder1/ /media/hdd2/Folder2/


        rsync is backing up whole folder from one drive to another and -r option specifies to do it recursively for all files and subfolders.






        share|improve this answer












        simple way is to copy:



        cp file1 file2


        there are multiple ways to divert the flow of output data into another file as well e.g.:



        cat file1 > file2
        less file1 > file2
        more file1 > file2


        You can use rsync to take backup:



        rsync -r /media/hdd1/Folder1/ /media/hdd2/Folder2/


        rsync is backing up whole folder from one drive to another and -r option specifies to do it recursively for all files and subfolders.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Feb 1 at 16:14









        Aqeel Asghar

        11




        11






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f421219%2fhow-to-redirect-all-the-content-of-one-file-to-another-file%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Peggy Mitchell

            Palaiologos

            The Forum (Inglewood, California)