How to write content of several files into a single one in Unix?











up vote
0
down vote

favorite
1












Suppose in a directory there are some files like:



file1.txt  
file2.txt
file3.txt
file4
file5
fab
text1


I need to eliminate the files with the .txt extension and append the content of the remaining files having a file name started with file(file4, file5) to a single file.



I tried the below command, but it appends all 5 file content to a single file.



ls -ltr file*|grep -vE ".txt" | cat * > final









share|improve this question




























    up vote
    0
    down vote

    favorite
    1












    Suppose in a directory there are some files like:



    file1.txt  
    file2.txt
    file3.txt
    file4
    file5
    fab
    text1


    I need to eliminate the files with the .txt extension and append the content of the remaining files having a file name started with file(file4, file5) to a single file.



    I tried the below command, but it appends all 5 file content to a single file.



    ls -ltr file*|grep -vE ".txt" | cat * > final









    share|improve this question


























      up vote
      0
      down vote

      favorite
      1









      up vote
      0
      down vote

      favorite
      1






      1





      Suppose in a directory there are some files like:



      file1.txt  
      file2.txt
      file3.txt
      file4
      file5
      fab
      text1


      I need to eliminate the files with the .txt extension and append the content of the remaining files having a file name started with file(file4, file5) to a single file.



      I tried the below command, but it appends all 5 file content to a single file.



      ls -ltr file*|grep -vE ".txt" | cat * > final









      share|improve this question















      Suppose in a directory there are some files like:



      file1.txt  
      file2.txt
      file3.txt
      file4
      file5
      fab
      text1


      I need to eliminate the files with the .txt extension and append the content of the remaining files having a file name started with file(file4, file5) to a single file.



      I tried the below command, but it appends all 5 file content to a single file.



      ls -ltr file*|grep -vE ".txt" | cat * > final






      shell






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 at 21:36









      Rui F Ribeiro

      38.2k1475125




      38.2k1475125










      asked Mar 2 '16 at 14:05









      user159194

      1




      1






















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          2
          down vote













          If you have bash available, you can use the following:



          shopt -s extglob
          cat !(*.txt) > final


          Or using zsh:



          setopt extended_glob
          cat ^*.txt > final





          share|improve this answer




























            up vote
            0
            down vote













            find . ! -name '*.txt' | xargs cat >> final






            share|improve this answer





















            • This version has a couple of downsides. 1) it's recursive, which may not be desired. 2) it will break on filenames that contain spaces. 3) the fact that it appends prevents running it twice and getting the same resulting file.
              – jordanm
              Mar 2 '16 at 14:16










            • you can use find with -maxdepth 1 -type f parameters
              – mazs
              Mar 2 '16 at 14:22












            • That works, but not all versions of find support -maxdepth and OP did not specify his OS.
              – jordanm
              Mar 2 '16 at 14:23










            • The solution using 'cat' is definitely better. On the other hand, the nice thing in Linux is that there are multiple ways of solving any issue.
              – mazs
              Mar 2 '16 at 14:40


















            up vote
            0
            down vote













            Try this



            #!/bin/bash

            find . -name '*.txt' -exec rm {} +

            for f in file*
            do
            cat $f >> final_file
            done


            In one line without removing .txt files but ignoring them for the cat



            $ find . -name 'file*' ! -name '*.txt' -exec cat {} ; > final





            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%2f267083%2fhow-to-write-content-of-several-files-into-a-single-one-in-unix%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              2
              down vote













              If you have bash available, you can use the following:



              shopt -s extglob
              cat !(*.txt) > final


              Or using zsh:



              setopt extended_glob
              cat ^*.txt > final





              share|improve this answer

























                up vote
                2
                down vote













                If you have bash available, you can use the following:



                shopt -s extglob
                cat !(*.txt) > final


                Or using zsh:



                setopt extended_glob
                cat ^*.txt > final





                share|improve this answer























                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  If you have bash available, you can use the following:



                  shopt -s extglob
                  cat !(*.txt) > final


                  Or using zsh:



                  setopt extended_glob
                  cat ^*.txt > final





                  share|improve this answer












                  If you have bash available, you can use the following:



                  shopt -s extglob
                  cat !(*.txt) > final


                  Or using zsh:



                  setopt extended_glob
                  cat ^*.txt > final






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 2 '16 at 14:13









                  jordanm

                  29.8k28192




                  29.8k28192
























                      up vote
                      0
                      down vote













                      find . ! -name '*.txt' | xargs cat >> final






                      share|improve this answer





















                      • This version has a couple of downsides. 1) it's recursive, which may not be desired. 2) it will break on filenames that contain spaces. 3) the fact that it appends prevents running it twice and getting the same resulting file.
                        – jordanm
                        Mar 2 '16 at 14:16










                      • you can use find with -maxdepth 1 -type f parameters
                        – mazs
                        Mar 2 '16 at 14:22












                      • That works, but not all versions of find support -maxdepth and OP did not specify his OS.
                        – jordanm
                        Mar 2 '16 at 14:23










                      • The solution using 'cat' is definitely better. On the other hand, the nice thing in Linux is that there are multiple ways of solving any issue.
                        – mazs
                        Mar 2 '16 at 14:40















                      up vote
                      0
                      down vote













                      find . ! -name '*.txt' | xargs cat >> final






                      share|improve this answer





















                      • This version has a couple of downsides. 1) it's recursive, which may not be desired. 2) it will break on filenames that contain spaces. 3) the fact that it appends prevents running it twice and getting the same resulting file.
                        – jordanm
                        Mar 2 '16 at 14:16










                      • you can use find with -maxdepth 1 -type f parameters
                        – mazs
                        Mar 2 '16 at 14:22












                      • That works, but not all versions of find support -maxdepth and OP did not specify his OS.
                        – jordanm
                        Mar 2 '16 at 14:23










                      • The solution using 'cat' is definitely better. On the other hand, the nice thing in Linux is that there are multiple ways of solving any issue.
                        – mazs
                        Mar 2 '16 at 14:40













                      up vote
                      0
                      down vote










                      up vote
                      0
                      down vote









                      find . ! -name '*.txt' | xargs cat >> final






                      share|improve this answer












                      find . ! -name '*.txt' | xargs cat >> final







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Mar 2 '16 at 14:14









                      mazs

                      2,5521622




                      2,5521622












                      • This version has a couple of downsides. 1) it's recursive, which may not be desired. 2) it will break on filenames that contain spaces. 3) the fact that it appends prevents running it twice and getting the same resulting file.
                        – jordanm
                        Mar 2 '16 at 14:16










                      • you can use find with -maxdepth 1 -type f parameters
                        – mazs
                        Mar 2 '16 at 14:22












                      • That works, but not all versions of find support -maxdepth and OP did not specify his OS.
                        – jordanm
                        Mar 2 '16 at 14:23










                      • The solution using 'cat' is definitely better. On the other hand, the nice thing in Linux is that there are multiple ways of solving any issue.
                        – mazs
                        Mar 2 '16 at 14:40


















                      • This version has a couple of downsides. 1) it's recursive, which may not be desired. 2) it will break on filenames that contain spaces. 3) the fact that it appends prevents running it twice and getting the same resulting file.
                        – jordanm
                        Mar 2 '16 at 14:16










                      • you can use find with -maxdepth 1 -type f parameters
                        – mazs
                        Mar 2 '16 at 14:22












                      • That works, but not all versions of find support -maxdepth and OP did not specify his OS.
                        – jordanm
                        Mar 2 '16 at 14:23










                      • The solution using 'cat' is definitely better. On the other hand, the nice thing in Linux is that there are multiple ways of solving any issue.
                        – mazs
                        Mar 2 '16 at 14:40
















                      This version has a couple of downsides. 1) it's recursive, which may not be desired. 2) it will break on filenames that contain spaces. 3) the fact that it appends prevents running it twice and getting the same resulting file.
                      – jordanm
                      Mar 2 '16 at 14:16




                      This version has a couple of downsides. 1) it's recursive, which may not be desired. 2) it will break on filenames that contain spaces. 3) the fact that it appends prevents running it twice and getting the same resulting file.
                      – jordanm
                      Mar 2 '16 at 14:16












                      you can use find with -maxdepth 1 -type f parameters
                      – mazs
                      Mar 2 '16 at 14:22






                      you can use find with -maxdepth 1 -type f parameters
                      – mazs
                      Mar 2 '16 at 14:22














                      That works, but not all versions of find support -maxdepth and OP did not specify his OS.
                      – jordanm
                      Mar 2 '16 at 14:23




                      That works, but not all versions of find support -maxdepth and OP did not specify his OS.
                      – jordanm
                      Mar 2 '16 at 14:23












                      The solution using 'cat' is definitely better. On the other hand, the nice thing in Linux is that there are multiple ways of solving any issue.
                      – mazs
                      Mar 2 '16 at 14:40




                      The solution using 'cat' is definitely better. On the other hand, the nice thing in Linux is that there are multiple ways of solving any issue.
                      – mazs
                      Mar 2 '16 at 14:40










                      up vote
                      0
                      down vote













                      Try this



                      #!/bin/bash

                      find . -name '*.txt' -exec rm {} +

                      for f in file*
                      do
                      cat $f >> final_file
                      done


                      In one line without removing .txt files but ignoring them for the cat



                      $ find . -name 'file*' ! -name '*.txt' -exec cat {} ; > final





                      share|improve this answer

























                        up vote
                        0
                        down vote













                        Try this



                        #!/bin/bash

                        find . -name '*.txt' -exec rm {} +

                        for f in file*
                        do
                        cat $f >> final_file
                        done


                        In one line without removing .txt files but ignoring them for the cat



                        $ find . -name 'file*' ! -name '*.txt' -exec cat {} ; > final





                        share|improve this answer























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          Try this



                          #!/bin/bash

                          find . -name '*.txt' -exec rm {} +

                          for f in file*
                          do
                          cat $f >> final_file
                          done


                          In one line without removing .txt files but ignoring them for the cat



                          $ find . -name 'file*' ! -name '*.txt' -exec cat {} ; > final





                          share|improve this answer












                          Try this



                          #!/bin/bash

                          find . -name '*.txt' -exec rm {} +

                          for f in file*
                          do
                          cat $f >> final_file
                          done


                          In one line without removing .txt files but ignoring them for the cat



                          $ find . -name 'file*' ! -name '*.txt' -exec cat {} ; > final






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Mar 2 '16 at 15:00









                          tachomi

                          3,52731134




                          3,52731134






























                               

                              draft saved


                              draft discarded



















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f267083%2fhow-to-write-content-of-several-files-into-a-single-one-in-unix%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号伴広島線

                              Setup Asymptote in Texstudio