Avoid listing of files with ~ or twin files

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











up vote
1
down vote

favorite












My requirement is to list all files in a directory. I tried to use command :



ls -l | grep -v ~ 


This is to list all files, and to avoid all files or twin files. But the output I get is



asdasad
asdasad~
file_names.txt
normaltest.txt
target_filename
testshell1.sh
testshell1.sh~
testshell2.sh
testshell2.sh~
testtwo.txt
testtwo.txt~
test.txt
test.txt~


I want to get only these files



asdasad
file_names.txt
normaltest.txt
target_filename
testshell1.sh
testshell2.sh
testtwo.txt
test.txt


Please help.










share|improve this question









New contributor




curious_one is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.























    up vote
    1
    down vote

    favorite












    My requirement is to list all files in a directory. I tried to use command :



    ls -l | grep -v ~ 


    This is to list all files, and to avoid all files or twin files. But the output I get is



    asdasad
    asdasad~
    file_names.txt
    normaltest.txt
    target_filename
    testshell1.sh
    testshell1.sh~
    testshell2.sh
    testshell2.sh~
    testtwo.txt
    testtwo.txt~
    test.txt
    test.txt~


    I want to get only these files



    asdasad
    file_names.txt
    normaltest.txt
    target_filename
    testshell1.sh
    testshell2.sh
    testtwo.txt
    test.txt


    Please help.










    share|improve this question









    New contributor




    curious_one is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      My requirement is to list all files in a directory. I tried to use command :



      ls -l | grep -v ~ 


      This is to list all files, and to avoid all files or twin files. But the output I get is



      asdasad
      asdasad~
      file_names.txt
      normaltest.txt
      target_filename
      testshell1.sh
      testshell1.sh~
      testshell2.sh
      testshell2.sh~
      testtwo.txt
      testtwo.txt~
      test.txt
      test.txt~


      I want to get only these files



      asdasad
      file_names.txt
      normaltest.txt
      target_filename
      testshell1.sh
      testshell2.sh
      testtwo.txt
      test.txt


      Please help.










      share|improve this question









      New contributor




      curious_one is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      My requirement is to list all files in a directory. I tried to use command :



      ls -l | grep -v ~ 


      This is to list all files, and to avoid all files or twin files. But the output I get is



      asdasad
      asdasad~
      file_names.txt
      normaltest.txt
      target_filename
      testshell1.sh
      testshell1.sh~
      testshell2.sh
      testshell2.sh~
      testtwo.txt
      testtwo.txt~
      test.txt
      test.txt~


      I want to get only these files



      asdasad
      file_names.txt
      normaltest.txt
      target_filename
      testshell1.sh
      testshell2.sh
      testtwo.txt
      test.txt


      Please help.







      linux command-line filenames






      share|improve this question









      New contributor




      curious_one is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      curious_one is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 13 mins ago









      Jeff Schaller

      32.8k849110




      32.8k849110






      New contributor




      curious_one is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 48 mins ago









      curious_one

      61




      61




      New contributor




      curious_one is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      curious_one is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      curious_one is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















          4 Answers
          4






          active

          oldest

          votes

















          up vote
          5
          down vote













          ls has an option for that: -B Ignore backups:



          ls --ignore-backups





          share|improve this answer




















          • That's a GNU feature, though probably fine since this was tagged linux. But e.g. on FreeBSD, ls -B means something different.
            – ilkkachu
            38 mins ago


















          up vote
          4
          down vote













          Make sure extglob is enable:



          shopt -s extglob


          Then you can use:



          ls -d !(*~)



          • -d to not show directories contents


          • !(*~) all files except the ones ending with ~

          But I would go with: ls --ignore-backups.






          share|improve this answer






















          • given the Linux tab, it's not a stretch to assume bash, but shopt -s extglob is a bash-specific feature.
            – Jeff Schaller
            14 mins ago

















          up vote
          2
          down vote













          This should help:



          ls -l | grep -v '~' 


          Reason: The ~ char is replaced by your home directory before the command is executed. Try



          echo ~


          and



          echo '~'





          share|improve this answer



























            up vote
            2
            down vote













            ls -l | grep -v ~


            The reason this doesn't work is that the tilde gets expanded to your home directory, so grep never sees a literal tilde. (See e.g. Bash's manual on Tilde Expansion.) You need to quote it to prevent the expansion, i.e.



            ls -l | grep -v "~"


            Of course, this will still remove any output lines with a tilde anywhere, even in the middle of a file name or elsewhere in the ls output (though it's probably not likely to appear in usernames, dates or such).






            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
              );



              );






              curious_one is a new contributor. Be nice, and check out our Code of Conduct.









               

              draft saved


              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f471577%2favoid-listing-of-files-with-or-twin-files%23new-answer', 'question_page');

              );

              Post as a guest






























              4 Answers
              4






              active

              oldest

              votes








              4 Answers
              4






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              5
              down vote













              ls has an option for that: -B Ignore backups:



              ls --ignore-backups





              share|improve this answer




















              • That's a GNU feature, though probably fine since this was tagged linux. But e.g. on FreeBSD, ls -B means something different.
                – ilkkachu
                38 mins ago















              up vote
              5
              down vote













              ls has an option for that: -B Ignore backups:



              ls --ignore-backups





              share|improve this answer




















              • That's a GNU feature, though probably fine since this was tagged linux. But e.g. on FreeBSD, ls -B means something different.
                – ilkkachu
                38 mins ago













              up vote
              5
              down vote










              up vote
              5
              down vote









              ls has an option for that: -B Ignore backups:



              ls --ignore-backups





              share|improve this answer












              ls has an option for that: -B Ignore backups:



              ls --ignore-backups






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered 45 mins ago









              hschou

              1,71349




              1,71349











              • That's a GNU feature, though probably fine since this was tagged linux. But e.g. on FreeBSD, ls -B means something different.
                – ilkkachu
                38 mins ago

















              • That's a GNU feature, though probably fine since this was tagged linux. But e.g. on FreeBSD, ls -B means something different.
                – ilkkachu
                38 mins ago
















              That's a GNU feature, though probably fine since this was tagged linux. But e.g. on FreeBSD, ls -B means something different.
              – ilkkachu
              38 mins ago





              That's a GNU feature, though probably fine since this was tagged linux. But e.g. on FreeBSD, ls -B means something different.
              – ilkkachu
              38 mins ago













              up vote
              4
              down vote













              Make sure extglob is enable:



              shopt -s extglob


              Then you can use:



              ls -d !(*~)



              • -d to not show directories contents


              • !(*~) all files except the ones ending with ~

              But I would go with: ls --ignore-backups.






              share|improve this answer






















              • given the Linux tab, it's not a stretch to assume bash, but shopt -s extglob is a bash-specific feature.
                – Jeff Schaller
                14 mins ago














              up vote
              4
              down vote













              Make sure extglob is enable:



              shopt -s extglob


              Then you can use:



              ls -d !(*~)



              • -d to not show directories contents


              • !(*~) all files except the ones ending with ~

              But I would go with: ls --ignore-backups.






              share|improve this answer






















              • given the Linux tab, it's not a stretch to assume bash, but shopt -s extglob is a bash-specific feature.
                – Jeff Schaller
                14 mins ago












              up vote
              4
              down vote










              up vote
              4
              down vote









              Make sure extglob is enable:



              shopt -s extglob


              Then you can use:



              ls -d !(*~)



              • -d to not show directories contents


              • !(*~) all files except the ones ending with ~

              But I would go with: ls --ignore-backups.






              share|improve this answer














              Make sure extglob is enable:



              shopt -s extglob


              Then you can use:



              ls -d !(*~)



              • -d to not show directories contents


              • !(*~) all files except the ones ending with ~

              But I would go with: ls --ignore-backups.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited 38 mins ago

























              answered 42 mins ago









              Ravexina

              1,037719




              1,037719











              • given the Linux tab, it's not a stretch to assume bash, but shopt -s extglob is a bash-specific feature.
                – Jeff Schaller
                14 mins ago
















              • given the Linux tab, it's not a stretch to assume bash, but shopt -s extglob is a bash-specific feature.
                – Jeff Schaller
                14 mins ago















              given the Linux tab, it's not a stretch to assume bash, but shopt -s extglob is a bash-specific feature.
              – Jeff Schaller
              14 mins ago




              given the Linux tab, it's not a stretch to assume bash, but shopt -s extglob is a bash-specific feature.
              – Jeff Schaller
              14 mins ago










              up vote
              2
              down vote













              This should help:



              ls -l | grep -v '~' 


              Reason: The ~ char is replaced by your home directory before the command is executed. Try



              echo ~


              and



              echo '~'





              share|improve this answer
























                up vote
                2
                down vote













                This should help:



                ls -l | grep -v '~' 


                Reason: The ~ char is replaced by your home directory before the command is executed. Try



                echo ~


                and



                echo '~'





                share|improve this answer






















                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  This should help:



                  ls -l | grep -v '~' 


                  Reason: The ~ char is replaced by your home directory before the command is executed. Try



                  echo ~


                  and



                  echo '~'





                  share|improve this answer












                  This should help:



                  ls -l | grep -v '~' 


                  Reason: The ~ char is replaced by your home directory before the command is executed. Try



                  echo ~


                  and



                  echo '~'






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 43 mins ago









                  Nico Mittenzwey

                  6114




                  6114




















                      up vote
                      2
                      down vote













                      ls -l | grep -v ~


                      The reason this doesn't work is that the tilde gets expanded to your home directory, so grep never sees a literal tilde. (See e.g. Bash's manual on Tilde Expansion.) You need to quote it to prevent the expansion, i.e.



                      ls -l | grep -v "~"


                      Of course, this will still remove any output lines with a tilde anywhere, even in the middle of a file name or elsewhere in the ls output (though it's probably not likely to appear in usernames, dates or such).






                      share|improve this answer
























                        up vote
                        2
                        down vote













                        ls -l | grep -v ~


                        The reason this doesn't work is that the tilde gets expanded to your home directory, so grep never sees a literal tilde. (See e.g. Bash's manual on Tilde Expansion.) You need to quote it to prevent the expansion, i.e.



                        ls -l | grep -v "~"


                        Of course, this will still remove any output lines with a tilde anywhere, even in the middle of a file name or elsewhere in the ls output (though it's probably not likely to appear in usernames, dates or such).






                        share|improve this answer






















                          up vote
                          2
                          down vote










                          up vote
                          2
                          down vote









                          ls -l | grep -v ~


                          The reason this doesn't work is that the tilde gets expanded to your home directory, so grep never sees a literal tilde. (See e.g. Bash's manual on Tilde Expansion.) You need to quote it to prevent the expansion, i.e.



                          ls -l | grep -v "~"


                          Of course, this will still remove any output lines with a tilde anywhere, even in the middle of a file name or elsewhere in the ls output (though it's probably not likely to appear in usernames, dates or such).






                          share|improve this answer












                          ls -l | grep -v ~


                          The reason this doesn't work is that the tilde gets expanded to your home directory, so grep never sees a literal tilde. (See e.g. Bash's manual on Tilde Expansion.) You need to quote it to prevent the expansion, i.e.



                          ls -l | grep -v "~"


                          Of course, this will still remove any output lines with a tilde anywhere, even in the middle of a file name or elsewhere in the ls output (though it's probably not likely to appear in usernames, dates or such).







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 42 mins ago









                          ilkkachu

                          51.6k678143




                          51.6k678143




















                              curious_one is a new contributor. Be nice, and check out our Code of Conduct.









                               

                              draft saved


                              draft discarded


















                              curious_one is a new contributor. Be nice, and check out our Code of Conduct.












                              curious_one is a new contributor. Be nice, and check out our Code of Conduct.











                              curious_one is a new contributor. Be nice, and check out our Code of Conduct.













                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f471577%2favoid-listing-of-files-with-or-twin-files%23new-answer', 'question_page');

                              );

                              Post as a guest













































































                              Comments

                              Popular posts from this blog

                              White Anglo-Saxon Protestant

                              BuddyTV

                              Conflict (narrative)