The most recent files in directory with a specific format in each one











up vote
-1
down vote

favorite












I would like to have in a variable the most recent file with a specific format.



exemple : in /home/test, 5 files :





  • file_test_hadoop_20181130.csv (This is the last modified one for hadoop file)

  • file_test_hadoop_20181130.txt

  • file_test_hadoop_20181130.ini


  • file_test_hub_20181130.txt (This is the last modified one for txt file)


  • file_test_hub_20181130.csv

  • file_test_hub_20181130.ini


So the result that i want is the last modified one in each type :
HADOOP_NAME=file_test_hadoop_20181130.csv

HUB_NAME=file_test_hub_20181130.txt



So i started to do something like this :



HADOOP_NAME=$(ls -tr /home/test | tail -n 1 | find /home/test -maxdepth 1 -name "file_test_hadoop*" -printf '%fn')



HUB_NAME=$(ls -tr /home/test | tail -n 1 | find /home/test -maxdepth 1 -name "file_test_hub*" -printf '%fn')



But i get all files.










share|improve this question




























    up vote
    -1
    down vote

    favorite












    I would like to have in a variable the most recent file with a specific format.



    exemple : in /home/test, 5 files :





    • file_test_hadoop_20181130.csv (This is the last modified one for hadoop file)

    • file_test_hadoop_20181130.txt

    • file_test_hadoop_20181130.ini


    • file_test_hub_20181130.txt (This is the last modified one for txt file)


    • file_test_hub_20181130.csv

    • file_test_hub_20181130.ini


    So the result that i want is the last modified one in each type :
    HADOOP_NAME=file_test_hadoop_20181130.csv

    HUB_NAME=file_test_hub_20181130.txt



    So i started to do something like this :



    HADOOP_NAME=$(ls -tr /home/test | tail -n 1 | find /home/test -maxdepth 1 -name "file_test_hadoop*" -printf '%fn')



    HUB_NAME=$(ls -tr /home/test | tail -n 1 | find /home/test -maxdepth 1 -name "file_test_hub*" -printf '%fn')



    But i get all files.










    share|improve this question


























      up vote
      -1
      down vote

      favorite









      up vote
      -1
      down vote

      favorite











      I would like to have in a variable the most recent file with a specific format.



      exemple : in /home/test, 5 files :





      • file_test_hadoop_20181130.csv (This is the last modified one for hadoop file)

      • file_test_hadoop_20181130.txt

      • file_test_hadoop_20181130.ini


      • file_test_hub_20181130.txt (This is the last modified one for txt file)


      • file_test_hub_20181130.csv

      • file_test_hub_20181130.ini


      So the result that i want is the last modified one in each type :
      HADOOP_NAME=file_test_hadoop_20181130.csv

      HUB_NAME=file_test_hub_20181130.txt



      So i started to do something like this :



      HADOOP_NAME=$(ls -tr /home/test | tail -n 1 | find /home/test -maxdepth 1 -name "file_test_hadoop*" -printf '%fn')



      HUB_NAME=$(ls -tr /home/test | tail -n 1 | find /home/test -maxdepth 1 -name "file_test_hub*" -printf '%fn')



      But i get all files.










      share|improve this question















      I would like to have in a variable the most recent file with a specific format.



      exemple : in /home/test, 5 files :





      • file_test_hadoop_20181130.csv (This is the last modified one for hadoop file)

      • file_test_hadoop_20181130.txt

      • file_test_hadoop_20181130.ini


      • file_test_hub_20181130.txt (This is the last modified one for txt file)


      • file_test_hub_20181130.csv

      • file_test_hub_20181130.ini


      So the result that i want is the last modified one in each type :
      HADOOP_NAME=file_test_hadoop_20181130.csv

      HUB_NAME=file_test_hub_20181130.txt



      So i started to do something like this :



      HADOOP_NAME=$(ls -tr /home/test | tail -n 1 | find /home/test -maxdepth 1 -name "file_test_hadoop*" -printf '%fn')



      HUB_NAME=$(ls -tr /home/test | tail -n 1 | find /home/test -maxdepth 1 -name "file_test_hub*" -printf '%fn')



      But i get all files.







      shell-script files find ls






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 30 at 12:34









      Rui F Ribeiro

      38.5k1479128




      38.5k1479128










      asked Nov 30 at 10:47









      amine tabenyoussef

      105




      105






















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          0
          down vote













          So you want to retrieve the most recently modified file with a specific naming pattern. The following commands should work for you:



          [haxiel@testvm1 ~]$ ls -1t file_test_hadoop* | head -n1
          file_test_hadoop_20181130.csv

          [haxiel@testvm1 ~]$ ls -1t file_test_hub* | head -n1
          file_test_hub_20181130.txt





          share|improve this answer




























            up vote
            0
            down vote













            Tried with below command and it worked fine



            ls -ltrh /home/test/file_test_hadoop*| tail -n1
            ls -ltrh /home/test/file_test_hub* | tail -n1





            share|improve this answer




























              up vote
              0
              down vote













              Instead of trying to parse ls or pipe ls to find & print, just use bash arrays, shell globbing, and the natural sorting order of YYYYMMDD:



              cd /home/test
              hadoop_files=(file_test_hadoop*)
              HADOOP_NAME=${hadoop_files[-1]}
              unset hadoop_files
              hub_files=(file_test_hub*)
              HUB_NAME=${hub_files[-1]}
              unset hub_files
              cd -


              This populates temporary array variables with the list of filenames that match the various patterns. You'd want to add error-checking for the situations where there are no files at all, or files of an unexpected pattern (file_test_hadoop9, for example).






              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%2f485116%2fthe-most-recent-files-in-directory-with-a-specific-format-in-each-one%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
                0
                down vote













                So you want to retrieve the most recently modified file with a specific naming pattern. The following commands should work for you:



                [haxiel@testvm1 ~]$ ls -1t file_test_hadoop* | head -n1
                file_test_hadoop_20181130.csv

                [haxiel@testvm1 ~]$ ls -1t file_test_hub* | head -n1
                file_test_hub_20181130.txt





                share|improve this answer

























                  up vote
                  0
                  down vote













                  So you want to retrieve the most recently modified file with a specific naming pattern. The following commands should work for you:



                  [haxiel@testvm1 ~]$ ls -1t file_test_hadoop* | head -n1
                  file_test_hadoop_20181130.csv

                  [haxiel@testvm1 ~]$ ls -1t file_test_hub* | head -n1
                  file_test_hub_20181130.txt





                  share|improve this answer























                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    So you want to retrieve the most recently modified file with a specific naming pattern. The following commands should work for you:



                    [haxiel@testvm1 ~]$ ls -1t file_test_hadoop* | head -n1
                    file_test_hadoop_20181130.csv

                    [haxiel@testvm1 ~]$ ls -1t file_test_hub* | head -n1
                    file_test_hub_20181130.txt





                    share|improve this answer












                    So you want to retrieve the most recently modified file with a specific naming pattern. The following commands should work for you:



                    [haxiel@testvm1 ~]$ ls -1t file_test_hadoop* | head -n1
                    file_test_hadoop_20181130.csv

                    [haxiel@testvm1 ~]$ ls -1t file_test_hub* | head -n1
                    file_test_hub_20181130.txt






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 30 at 10:56









                    Haxiel

                    826310




                    826310
























                        up vote
                        0
                        down vote













                        Tried with below command and it worked fine



                        ls -ltrh /home/test/file_test_hadoop*| tail -n1
                        ls -ltrh /home/test/file_test_hub* | tail -n1





                        share|improve this answer

























                          up vote
                          0
                          down vote













                          Tried with below command and it worked fine



                          ls -ltrh /home/test/file_test_hadoop*| tail -n1
                          ls -ltrh /home/test/file_test_hub* | tail -n1





                          share|improve this answer























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            Tried with below command and it worked fine



                            ls -ltrh /home/test/file_test_hadoop*| tail -n1
                            ls -ltrh /home/test/file_test_hub* | tail -n1





                            share|improve this answer












                            Tried with below command and it worked fine



                            ls -ltrh /home/test/file_test_hadoop*| tail -n1
                            ls -ltrh /home/test/file_test_hub* | tail -n1






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 30 at 15:13









                            Praveen Kumar BS

                            1,162138




                            1,162138






















                                up vote
                                0
                                down vote













                                Instead of trying to parse ls or pipe ls to find & print, just use bash arrays, shell globbing, and the natural sorting order of YYYYMMDD:



                                cd /home/test
                                hadoop_files=(file_test_hadoop*)
                                HADOOP_NAME=${hadoop_files[-1]}
                                unset hadoop_files
                                hub_files=(file_test_hub*)
                                HUB_NAME=${hub_files[-1]}
                                unset hub_files
                                cd -


                                This populates temporary array variables with the list of filenames that match the various patterns. You'd want to add error-checking for the situations where there are no files at all, or files of an unexpected pattern (file_test_hadoop9, for example).






                                share|improve this answer

























                                  up vote
                                  0
                                  down vote













                                  Instead of trying to parse ls or pipe ls to find & print, just use bash arrays, shell globbing, and the natural sorting order of YYYYMMDD:



                                  cd /home/test
                                  hadoop_files=(file_test_hadoop*)
                                  HADOOP_NAME=${hadoop_files[-1]}
                                  unset hadoop_files
                                  hub_files=(file_test_hub*)
                                  HUB_NAME=${hub_files[-1]}
                                  unset hub_files
                                  cd -


                                  This populates temporary array variables with the list of filenames that match the various patterns. You'd want to add error-checking for the situations where there are no files at all, or files of an unexpected pattern (file_test_hadoop9, for example).






                                  share|improve this answer























                                    up vote
                                    0
                                    down vote










                                    up vote
                                    0
                                    down vote









                                    Instead of trying to parse ls or pipe ls to find & print, just use bash arrays, shell globbing, and the natural sorting order of YYYYMMDD:



                                    cd /home/test
                                    hadoop_files=(file_test_hadoop*)
                                    HADOOP_NAME=${hadoop_files[-1]}
                                    unset hadoop_files
                                    hub_files=(file_test_hub*)
                                    HUB_NAME=${hub_files[-1]}
                                    unset hub_files
                                    cd -


                                    This populates temporary array variables with the list of filenames that match the various patterns. You'd want to add error-checking for the situations where there are no files at all, or files of an unexpected pattern (file_test_hadoop9, for example).






                                    share|improve this answer












                                    Instead of trying to parse ls or pipe ls to find & print, just use bash arrays, shell globbing, and the natural sorting order of YYYYMMDD:



                                    cd /home/test
                                    hadoop_files=(file_test_hadoop*)
                                    HADOOP_NAME=${hadoop_files[-1]}
                                    unset hadoop_files
                                    hub_files=(file_test_hub*)
                                    HUB_NAME=${hub_files[-1]}
                                    unset hub_files
                                    cd -


                                    This populates temporary array variables with the list of filenames that match the various patterns. You'd want to add error-checking for the situations where there are no files at all, or files of an unexpected pattern (file_test_hadoop9, for example).







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 30 at 18:21









                                    Jeff Schaller

                                    37.5k1052121




                                    37.5k1052121






























                                        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%2f485116%2fthe-most-recent-files-in-directory-with-a-specific-format-in-each-one%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

                                        Morgemoulin

                                        Scott Moir

                                        Souastre