exclude certain files in ls











up vote
39
down vote

favorite
14












I would like to run ls and exclude certain files in the output.



When I run the following command, I get all files with each on one line:



$ ls -1
file1
file2
file3
temp


I would like to run this command in a way so it shows



$ ls -1 <insert magic here> temp
file1
file2
file3









share|improve this question


























    up vote
    39
    down vote

    favorite
    14












    I would like to run ls and exclude certain files in the output.



    When I run the following command, I get all files with each on one line:



    $ ls -1
    file1
    file2
    file3
    temp


    I would like to run this command in a way so it shows



    $ ls -1 <insert magic here> temp
    file1
    file2
    file3









    share|improve this question
























      up vote
      39
      down vote

      favorite
      14









      up vote
      39
      down vote

      favorite
      14






      14





      I would like to run ls and exclude certain files in the output.



      When I run the following command, I get all files with each on one line:



      $ ls -1
      file1
      file2
      file3
      temp


      I would like to run this command in a way so it shows



      $ ls -1 <insert magic here> temp
      file1
      file2
      file3









      share|improve this question













      I would like to run ls and exclude certain files in the output.



      When I run the following command, I get all files with each on one line:



      $ ls -1
      file1
      file2
      file3
      temp


      I would like to run this command in a way so it shows



      $ ls -1 <insert magic here> temp
      file1
      file2
      file3






      bash ls






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Aug 18 '14 at 14:54









      Alice Ryhl

      3671412




      3671412






















          4 Answers
          4






          active

          oldest

          votes

















          up vote
          57
          down vote



          accepted










          ls -I <filename>


          -I = Ignore the filename, i.e., don't list the specified file.



          To ignore more than one file add an -I before each filename.



          ls -I file1 -I file2


          To ignore files by their name extensions do the following, for example.



          ls -I "*.jpg" -I "*.svg"





          share|improve this answer



















          • 4




            If you use the long-form option --ignore you can extend that to glob patterns e.g. ls --ignore="file?" or ls --ignore="file*"
            – steeldriver
            Aug 18 '14 at 15:23








          • 2




            you can use glob patterns with the short form as well by quoting the patterns
            – verboze
            Dec 29 '15 at 5:42


















          up vote
          15
          down vote













          For me if I use -I once, it works but if I use twice it doesn't.
          e.g:
          ls -I *.csv works.



          But
          ls -I *.csv -I *.txt doesn't work and returns txt files instead.



          --ignore did the trick for me. This is what I needed and worked.



          ls -lhrt --ignore="*.gz" --ignore="*.1"



          That will list me files from log folder where it exclude old backup logs.






          share|improve this answer

















          • 3




            quoting *.csv in the first form works, e.g.., ls -I '*.txt'. The reason it doesn't work unquoted is because of shell expansion, i.e. you're telling the shell to list all csv files instead of excluding them. What's actually happening is that it ignores the first .csv and .1 files after expansion, but lists the remainders
            – verboze
            Dec 29 '15 at 5:40










          • Double-quoting worked for me as well. Thanks @verboze
            – user38537
            Sep 29 '16 at 20:41


















          up vote
          3
          down vote













          ls --ignore={"*.jpg","*.png","*.svg"}






          share|improve this answer

















          • 3




            This is simply using bash's brace expansion to get three --ignore options. You could have also used --ignore="*."{jpg,png,svg}
            – muru
            Feb 14 '17 at 0:27


















          up vote
          1
          down vote













          I think that this produces the output you're looking for:



          ls -1 !(temp)


          Apparently, you need shopt -s extglob for that to work

          (I have it enabled, so I guess some time in the distant past I found it useful and enabled it).



          I guess you could also use grep to filter the output:



          ls -1 | grep -v '^temp$'


          Using a pipe and filters provides a lot more flexibility, and skills that are transferable to other commands/situations, though you might not be interested in that for this specific case.






          share|improve this answer























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "89"
            };
            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: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            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%2faskubuntu.com%2fquestions%2f512961%2fexclude-certain-files-in-ls%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            4 Answers
            4






            active

            oldest

            votes








            4 Answers
            4






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            57
            down vote



            accepted










            ls -I <filename>


            -I = Ignore the filename, i.e., don't list the specified file.



            To ignore more than one file add an -I before each filename.



            ls -I file1 -I file2


            To ignore files by their name extensions do the following, for example.



            ls -I "*.jpg" -I "*.svg"





            share|improve this answer



















            • 4




              If you use the long-form option --ignore you can extend that to glob patterns e.g. ls --ignore="file?" or ls --ignore="file*"
              – steeldriver
              Aug 18 '14 at 15:23








            • 2




              you can use glob patterns with the short form as well by quoting the patterns
              – verboze
              Dec 29 '15 at 5:42















            up vote
            57
            down vote



            accepted










            ls -I <filename>


            -I = Ignore the filename, i.e., don't list the specified file.



            To ignore more than one file add an -I before each filename.



            ls -I file1 -I file2


            To ignore files by their name extensions do the following, for example.



            ls -I "*.jpg" -I "*.svg"





            share|improve this answer



















            • 4




              If you use the long-form option --ignore you can extend that to glob patterns e.g. ls --ignore="file?" or ls --ignore="file*"
              – steeldriver
              Aug 18 '14 at 15:23








            • 2




              you can use glob patterns with the short form as well by quoting the patterns
              – verboze
              Dec 29 '15 at 5:42













            up vote
            57
            down vote



            accepted







            up vote
            57
            down vote



            accepted






            ls -I <filename>


            -I = Ignore the filename, i.e., don't list the specified file.



            To ignore more than one file add an -I before each filename.



            ls -I file1 -I file2


            To ignore files by their name extensions do the following, for example.



            ls -I "*.jpg" -I "*.svg"





            share|improve this answer














            ls -I <filename>


            -I = Ignore the filename, i.e., don't list the specified file.



            To ignore more than one file add an -I before each filename.



            ls -I file1 -I file2


            To ignore files by their name extensions do the following, for example.



            ls -I "*.jpg" -I "*.svg"






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Feb 13 '17 at 22:48









            jdthood

            10.3k14161




            10.3k14161










            answered Aug 18 '14 at 14:57









            Sudheer

            3,22531826




            3,22531826








            • 4




              If you use the long-form option --ignore you can extend that to glob patterns e.g. ls --ignore="file?" or ls --ignore="file*"
              – steeldriver
              Aug 18 '14 at 15:23








            • 2




              you can use glob patterns with the short form as well by quoting the patterns
              – verboze
              Dec 29 '15 at 5:42














            • 4




              If you use the long-form option --ignore you can extend that to glob patterns e.g. ls --ignore="file?" or ls --ignore="file*"
              – steeldriver
              Aug 18 '14 at 15:23








            • 2




              you can use glob patterns with the short form as well by quoting the patterns
              – verboze
              Dec 29 '15 at 5:42








            4




            4




            If you use the long-form option --ignore you can extend that to glob patterns e.g. ls --ignore="file?" or ls --ignore="file*"
            – steeldriver
            Aug 18 '14 at 15:23






            If you use the long-form option --ignore you can extend that to glob patterns e.g. ls --ignore="file?" or ls --ignore="file*"
            – steeldriver
            Aug 18 '14 at 15:23






            2




            2




            you can use glob patterns with the short form as well by quoting the patterns
            – verboze
            Dec 29 '15 at 5:42




            you can use glob patterns with the short form as well by quoting the patterns
            – verboze
            Dec 29 '15 at 5:42












            up vote
            15
            down vote













            For me if I use -I once, it works but if I use twice it doesn't.
            e.g:
            ls -I *.csv works.



            But
            ls -I *.csv -I *.txt doesn't work and returns txt files instead.



            --ignore did the trick for me. This is what I needed and worked.



            ls -lhrt --ignore="*.gz" --ignore="*.1"



            That will list me files from log folder where it exclude old backup logs.






            share|improve this answer

















            • 3




              quoting *.csv in the first form works, e.g.., ls -I '*.txt'. The reason it doesn't work unquoted is because of shell expansion, i.e. you're telling the shell to list all csv files instead of excluding them. What's actually happening is that it ignores the first .csv and .1 files after expansion, but lists the remainders
              – verboze
              Dec 29 '15 at 5:40










            • Double-quoting worked for me as well. Thanks @verboze
              – user38537
              Sep 29 '16 at 20:41















            up vote
            15
            down vote













            For me if I use -I once, it works but if I use twice it doesn't.
            e.g:
            ls -I *.csv works.



            But
            ls -I *.csv -I *.txt doesn't work and returns txt files instead.



            --ignore did the trick for me. This is what I needed and worked.



            ls -lhrt --ignore="*.gz" --ignore="*.1"



            That will list me files from log folder where it exclude old backup logs.






            share|improve this answer

















            • 3




              quoting *.csv in the first form works, e.g.., ls -I '*.txt'. The reason it doesn't work unquoted is because of shell expansion, i.e. you're telling the shell to list all csv files instead of excluding them. What's actually happening is that it ignores the first .csv and .1 files after expansion, but lists the remainders
              – verboze
              Dec 29 '15 at 5:40










            • Double-quoting worked for me as well. Thanks @verboze
              – user38537
              Sep 29 '16 at 20:41













            up vote
            15
            down vote










            up vote
            15
            down vote









            For me if I use -I once, it works but if I use twice it doesn't.
            e.g:
            ls -I *.csv works.



            But
            ls -I *.csv -I *.txt doesn't work and returns txt files instead.



            --ignore did the trick for me. This is what I needed and worked.



            ls -lhrt --ignore="*.gz" --ignore="*.1"



            That will list me files from log folder where it exclude old backup logs.






            share|improve this answer












            For me if I use -I once, it works but if I use twice it doesn't.
            e.g:
            ls -I *.csv works.



            But
            ls -I *.csv -I *.txt doesn't work and returns txt files instead.



            --ignore did the trick for me. This is what I needed and worked.



            ls -lhrt --ignore="*.gz" --ignore="*.1"



            That will list me files from log folder where it exclude old backup logs.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jul 13 '15 at 1:47









            Damodar Bashyal

            26137




            26137








            • 3




              quoting *.csv in the first form works, e.g.., ls -I '*.txt'. The reason it doesn't work unquoted is because of shell expansion, i.e. you're telling the shell to list all csv files instead of excluding them. What's actually happening is that it ignores the first .csv and .1 files after expansion, but lists the remainders
              – verboze
              Dec 29 '15 at 5:40










            • Double-quoting worked for me as well. Thanks @verboze
              – user38537
              Sep 29 '16 at 20:41














            • 3




              quoting *.csv in the first form works, e.g.., ls -I '*.txt'. The reason it doesn't work unquoted is because of shell expansion, i.e. you're telling the shell to list all csv files instead of excluding them. What's actually happening is that it ignores the first .csv and .1 files after expansion, but lists the remainders
              – verboze
              Dec 29 '15 at 5:40










            • Double-quoting worked for me as well. Thanks @verboze
              – user38537
              Sep 29 '16 at 20:41








            3




            3




            quoting *.csv in the first form works, e.g.., ls -I '*.txt'. The reason it doesn't work unquoted is because of shell expansion, i.e. you're telling the shell to list all csv files instead of excluding them. What's actually happening is that it ignores the first .csv and .1 files after expansion, but lists the remainders
            – verboze
            Dec 29 '15 at 5:40




            quoting *.csv in the first form works, e.g.., ls -I '*.txt'. The reason it doesn't work unquoted is because of shell expansion, i.e. you're telling the shell to list all csv files instead of excluding them. What's actually happening is that it ignores the first .csv and .1 files after expansion, but lists the remainders
            – verboze
            Dec 29 '15 at 5:40












            Double-quoting worked for me as well. Thanks @verboze
            – user38537
            Sep 29 '16 at 20:41




            Double-quoting worked for me as well. Thanks @verboze
            – user38537
            Sep 29 '16 at 20:41










            up vote
            3
            down vote













            ls --ignore={"*.jpg","*.png","*.svg"}






            share|improve this answer

















            • 3




              This is simply using bash's brace expansion to get three --ignore options. You could have also used --ignore="*."{jpg,png,svg}
              – muru
              Feb 14 '17 at 0:27















            up vote
            3
            down vote













            ls --ignore={"*.jpg","*.png","*.svg"}






            share|improve this answer

















            • 3




              This is simply using bash's brace expansion to get three --ignore options. You could have also used --ignore="*."{jpg,png,svg}
              – muru
              Feb 14 '17 at 0:27













            up vote
            3
            down vote










            up vote
            3
            down vote









            ls --ignore={"*.jpg","*.png","*.svg"}






            share|improve this answer












            ls --ignore={"*.jpg","*.png","*.svg"}







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Feb 13 '17 at 22:11









            Kartikey Tanna

            1335




            1335








            • 3




              This is simply using bash's brace expansion to get three --ignore options. You could have also used --ignore="*."{jpg,png,svg}
              – muru
              Feb 14 '17 at 0:27














            • 3




              This is simply using bash's brace expansion to get three --ignore options. You could have also used --ignore="*."{jpg,png,svg}
              – muru
              Feb 14 '17 at 0:27








            3




            3




            This is simply using bash's brace expansion to get three --ignore options. You could have also used --ignore="*."{jpg,png,svg}
            – muru
            Feb 14 '17 at 0:27




            This is simply using bash's brace expansion to get three --ignore options. You could have also used --ignore="*."{jpg,png,svg}
            – muru
            Feb 14 '17 at 0:27










            up vote
            1
            down vote













            I think that this produces the output you're looking for:



            ls -1 !(temp)


            Apparently, you need shopt -s extglob for that to work

            (I have it enabled, so I guess some time in the distant past I found it useful and enabled it).



            I guess you could also use grep to filter the output:



            ls -1 | grep -v '^temp$'


            Using a pipe and filters provides a lot more flexibility, and skills that are transferable to other commands/situations, though you might not be interested in that for this specific case.






            share|improve this answer



























              up vote
              1
              down vote













              I think that this produces the output you're looking for:



              ls -1 !(temp)


              Apparently, you need shopt -s extglob for that to work

              (I have it enabled, so I guess some time in the distant past I found it useful and enabled it).



              I guess you could also use grep to filter the output:



              ls -1 | grep -v '^temp$'


              Using a pipe and filters provides a lot more flexibility, and skills that are transferable to other commands/situations, though you might not be interested in that for this specific case.






              share|improve this answer

























                up vote
                1
                down vote










                up vote
                1
                down vote









                I think that this produces the output you're looking for:



                ls -1 !(temp)


                Apparently, you need shopt -s extglob for that to work

                (I have it enabled, so I guess some time in the distant past I found it useful and enabled it).



                I guess you could also use grep to filter the output:



                ls -1 | grep -v '^temp$'


                Using a pipe and filters provides a lot more flexibility, and skills that are transferable to other commands/situations, though you might not be interested in that for this specific case.






                share|improve this answer














                I think that this produces the output you're looking for:



                ls -1 !(temp)


                Apparently, you need shopt -s extglob for that to work

                (I have it enabled, so I guess some time in the distant past I found it useful and enabled it).



                I guess you could also use grep to filter the output:



                ls -1 | grep -v '^temp$'


                Using a pipe and filters provides a lot more flexibility, and skills that are transferable to other commands/situations, though you might not be interested in that for this specific case.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Oct 31 at 19:18









                zx485

                1,45231114




                1,45231114










                answered Oct 31 at 18:24









                Max Waterman

                111




                111






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Ask Ubuntu!


                    • 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%2faskubuntu.com%2fquestions%2f512961%2fexclude-certain-files-in-ls%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 change which sound is reproduced for terminal bell?

                    Can I use Tabulator js library in my java Spring + Thymeleaf project?

                    Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents