Avoid listing of files that end with ~ (backup files)

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











up vote
8
down vote

favorite












My requirement is to list all files in a directory, except files ending with a ~ (backup files).



I tried to use command:



ls -l | grep -v ~ 


I get this output:



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









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.



















  • It looks like you are trying to avoid listing files that end with a ~. These file are backup files created my some text editors.
    – ctrl-alt-delor
    4 hours ago










  • Which operating system are you using: is it a Gnu/Linux (such as Debian, Ubuntu, Redhat, Centos, Fedora, Suse, Mint, Arch, …)?
    – ctrl-alt-delor
    4 hours ago














up vote
8
down vote

favorite












My requirement is to list all files in a directory, except files ending with a ~ (backup files).



I tried to use command:



ls -l | grep -v ~ 


I get this output:



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









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.



















  • It looks like you are trying to avoid listing files that end with a ~. These file are backup files created my some text editors.
    – ctrl-alt-delor
    4 hours ago










  • Which operating system are you using: is it a Gnu/Linux (such as Debian, Ubuntu, Redhat, Centos, Fedora, Suse, Mint, Arch, …)?
    – ctrl-alt-delor
    4 hours ago












up vote
8
down vote

favorite









up vote
8
down vote

favorite











My requirement is to list all files in a directory, except files ending with a ~ (backup files).



I tried to use command:



ls -l | grep -v ~ 


I get this output:



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









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, except files ending with a ~ (backup files).



I tried to use command:



ls -l | grep -v ~ 


I get this output:



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






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 23 mins ago









wjandrea

464312




464312






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 7 hours ago









curious_one

411




411




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.











  • It looks like you are trying to avoid listing files that end with a ~. These file are backup files created my some text editors.
    – ctrl-alt-delor
    4 hours ago










  • Which operating system are you using: is it a Gnu/Linux (such as Debian, Ubuntu, Redhat, Centos, Fedora, Suse, Mint, Arch, …)?
    – ctrl-alt-delor
    4 hours ago
















  • It looks like you are trying to avoid listing files that end with a ~. These file are backup files created my some text editors.
    – ctrl-alt-delor
    4 hours ago










  • Which operating system are you using: is it a Gnu/Linux (such as Debian, Ubuntu, Redhat, Centos, Fedora, Suse, Mint, Arch, …)?
    – ctrl-alt-delor
    4 hours ago















It looks like you are trying to avoid listing files that end with a ~. These file are backup files created my some text editors.
– ctrl-alt-delor
4 hours ago




It looks like you are trying to avoid listing files that end with a ~. These file are backup files created my some text editors.
– ctrl-alt-delor
4 hours ago












Which operating system are you using: is it a Gnu/Linux (such as Debian, Ubuntu, Redhat, Centos, Fedora, Suse, Mint, Arch, …)?
– ctrl-alt-delor
4 hours ago




Which operating system are you using: is it a Gnu/Linux (such as Debian, Ubuntu, Redhat, Centos, Fedora, Suse, Mint, Arch, …)?
– ctrl-alt-delor
4 hours ago










5 Answers
5






active

oldest

votes

















up vote
16
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).
If you really only want to ignore files that end with a tilde, you can use



ls -l | grep -v "~$"





share|improve this answer





























    up vote
    14
    down vote













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



    ls --ignore-backups





    share|improve this answer
















    • 4




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


















    up vote
    10
    down vote













    If you're using Bash, 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 ~

    On "zsh", you can do the same:



    setopt extended_glob
    ls -d ^*~


    But the general idea is to use ls --ignore-backups.






    share|improve this answer


















    • 1




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










    • @JeffSchaller You're right :) I updated my answer to expand it a little bit ...
      – Ravexina
      4 hours ago

















    up vote
    4
    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
      0
      down vote













      As mentioned in other replies the reason you're likely having problems is because you didn't quote or escape the tilde.



      For one particular use case I've used shell filename expansion to do something similar. I've been using it since about 2003, which means it has worked on a very wide variety of shell implementations on different kinds of systems. (here I also use -C because I want a nice display in sorted columns)



      ls -C *[!~]


      (a limitation with using shell filename expansion is of course is that a directory with very large number of (non-backup) files will cause the expansion to exceed the available argument list size, though on most modern systems this limit is quite high, often 250KB or more, sometimes much more)






      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-that-end-with-backup-files%23new-answer', 'question_page');

        );

        Post as a guest






























        5 Answers
        5






        active

        oldest

        votes








        5 Answers
        5






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        16
        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).
        If you really only want to ignore files that end with a tilde, you can use



        ls -l | grep -v "~$"





        share|improve this answer


























          up vote
          16
          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).
          If you really only want to ignore files that end with a tilde, you can use



          ls -l | grep -v "~$"





          share|improve this answer
























            up vote
            16
            down vote










            up vote
            16
            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).
            If you really only want to ignore files that end with a tilde, you can use



            ls -l | grep -v "~$"





            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).
            If you really only want to ignore files that end with a tilde, you can use



            ls -l | grep -v "~$"






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 2 hours ago









            Foon

            1585




            1585










            answered 7 hours ago









            ilkkachu

            51.7k678144




            51.7k678144






















                up vote
                14
                down vote













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



                ls --ignore-backups





                share|improve this answer
















                • 4




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















                up vote
                14
                down vote













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



                ls --ignore-backups





                share|improve this answer
















                • 4




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













                up vote
                14
                down vote










                up vote
                14
                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 7 hours ago









                hschou

                1,803410




                1,803410







                • 4




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













                • 4




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








                4




                4




                That's a GNU feature, though probably fine since this was tagged linux. But e.g. on FreeBSD, ls -B means something different.
                – ilkkachu
                6 hours 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
                6 hours ago











                up vote
                10
                down vote













                If you're using Bash, 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 ~

                On "zsh", you can do the same:



                setopt extended_glob
                ls -d ^*~


                But the general idea is to use ls --ignore-backups.






                share|improve this answer


















                • 1




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










                • @JeffSchaller You're right :) I updated my answer to expand it a little bit ...
                  – Ravexina
                  4 hours ago














                up vote
                10
                down vote













                If you're using Bash, 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 ~

                On "zsh", you can do the same:



                setopt extended_glob
                ls -d ^*~


                But the general idea is to use ls --ignore-backups.






                share|improve this answer


















                • 1




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










                • @JeffSchaller You're right :) I updated my answer to expand it a little bit ...
                  – Ravexina
                  4 hours ago












                up vote
                10
                down vote










                up vote
                10
                down vote









                If you're using Bash, 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 ~

                On "zsh", you can do the same:



                setopt extended_glob
                ls -d ^*~


                But the general idea is to use ls --ignore-backups.






                share|improve this answer














                If you're using Bash, 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 ~

                On "zsh", you can do the same:



                setopt extended_glob
                ls -d ^*~


                But the general idea is to use ls --ignore-backups.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 4 hours ago

























                answered 7 hours ago









                Ravexina

                1,147720




                1,147720







                • 1




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










                • @JeffSchaller You're right :) I updated my answer to expand it a little bit ...
                  – Ravexina
                  4 hours ago












                • 1




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










                • @JeffSchaller You're right :) I updated my answer to expand it a little bit ...
                  – Ravexina
                  4 hours ago







                1




                1




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




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












                @JeffSchaller You're right :) I updated my answer to expand it a little bit ...
                – Ravexina
                4 hours ago




                @JeffSchaller You're right :) I updated my answer to expand it a little bit ...
                – Ravexina
                4 hours ago










                up vote
                4
                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
                  4
                  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
                    4
                    down vote










                    up vote
                    4
                    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 7 hours ago









                    Nico Mittenzwey

                    8114




                    8114




















                        up vote
                        0
                        down vote













                        As mentioned in other replies the reason you're likely having problems is because you didn't quote or escape the tilde.



                        For one particular use case I've used shell filename expansion to do something similar. I've been using it since about 2003, which means it has worked on a very wide variety of shell implementations on different kinds of systems. (here I also use -C because I want a nice display in sorted columns)



                        ls -C *[!~]


                        (a limitation with using shell filename expansion is of course is that a directory with very large number of (non-backup) files will cause the expansion to exceed the available argument list size, though on most modern systems this limit is quite high, often 250KB or more, sometimes much more)






                        share|improve this answer
























                          up vote
                          0
                          down vote













                          As mentioned in other replies the reason you're likely having problems is because you didn't quote or escape the tilde.



                          For one particular use case I've used shell filename expansion to do something similar. I've been using it since about 2003, which means it has worked on a very wide variety of shell implementations on different kinds of systems. (here I also use -C because I want a nice display in sorted columns)



                          ls -C *[!~]


                          (a limitation with using shell filename expansion is of course is that a directory with very large number of (non-backup) files will cause the expansion to exceed the available argument list size, though on most modern systems this limit is quite high, often 250KB or more, sometimes much more)






                          share|improve this answer






















                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            As mentioned in other replies the reason you're likely having problems is because you didn't quote or escape the tilde.



                            For one particular use case I've used shell filename expansion to do something similar. I've been using it since about 2003, which means it has worked on a very wide variety of shell implementations on different kinds of systems. (here I also use -C because I want a nice display in sorted columns)



                            ls -C *[!~]


                            (a limitation with using shell filename expansion is of course is that a directory with very large number of (non-backup) files will cause the expansion to exceed the available argument list size, though on most modern systems this limit is quite high, often 250KB or more, sometimes much more)






                            share|improve this answer












                            As mentioned in other replies the reason you're likely having problems is because you didn't quote or escape the tilde.



                            For one particular use case I've used shell filename expansion to do something similar. I've been using it since about 2003, which means it has worked on a very wide variety of shell implementations on different kinds of systems. (here I also use -C because I want a nice display in sorted columns)



                            ls -C *[!~]


                            (a limitation with using shell filename expansion is of course is that a directory with very large number of (non-backup) files will cause the expansion to exceed the available argument list size, though on most modern systems this limit is quite high, often 250KB or more, sometimes much more)







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered 41 mins ago









                            Greg A. Woods

                            33026




                            33026




















                                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-that-end-with-backup-files%23new-answer', 'question_page');

                                );

                                Post as a guest













































































                                Comments

                                Popular posts from this blog

                                Long meetings (6-7 hours a day): Being “babysat” by supervisor

                                What does second last employer means? [closed]

                                One-line joke