How to delete files with more than 1 line in a linux server? [closed]

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











up vote
-2
down vote

favorite












I have a large list of files exists inside a particular directory (with full path). I'm trying to delete files from this directory where the line count in the file is greater than 1 (2 or more).



How can it be done?










share|improve this question















closed as unclear what you're asking by Vlastimil, schily, dr01, msp9011, Wildcard Aug 15 at 23:31


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.










  • 2




    More detail would be useful: What OS, which shell you use, one file or several?
    – Isaac
    Aug 15 at 8:10






  • 1




    Sorry I am struggling to understand your question. Can you amend question to explain: What you mean by “with full path”? and “where the line count in the file is greater than 1”? (is this the number of line of the filename? if so is it just one line but wrapping, or is it more than one line?
    – ctrl-alt-delor
    Aug 15 at 9:48










  • I do not understand the question at all, voting to close.
    – Vlastimil
    Aug 15 at 11:26










  • Interestingly enough, I almost never have difficulties understanding these sort of questions, claimed by the powers-to-be. This question is perfectly clear.
    – ajeh
    Aug 15 at 21:31






  • 1




    Possible duplicate of List (or move) only files with a certain number of lines?
    – Wildcard
    Aug 15 at 23:31














up vote
-2
down vote

favorite












I have a large list of files exists inside a particular directory (with full path). I'm trying to delete files from this directory where the line count in the file is greater than 1 (2 or more).



How can it be done?










share|improve this question















closed as unclear what you're asking by Vlastimil, schily, dr01, msp9011, Wildcard Aug 15 at 23:31


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.










  • 2




    More detail would be useful: What OS, which shell you use, one file or several?
    – Isaac
    Aug 15 at 8:10






  • 1




    Sorry I am struggling to understand your question. Can you amend question to explain: What you mean by “with full path”? and “where the line count in the file is greater than 1”? (is this the number of line of the filename? if so is it just one line but wrapping, or is it more than one line?
    – ctrl-alt-delor
    Aug 15 at 9:48










  • I do not understand the question at all, voting to close.
    – Vlastimil
    Aug 15 at 11:26










  • Interestingly enough, I almost never have difficulties understanding these sort of questions, claimed by the powers-to-be. This question is perfectly clear.
    – ajeh
    Aug 15 at 21:31






  • 1




    Possible duplicate of List (or move) only files with a certain number of lines?
    – Wildcard
    Aug 15 at 23:31












up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











I have a large list of files exists inside a particular directory (with full path). I'm trying to delete files from this directory where the line count in the file is greater than 1 (2 or more).



How can it be done?










share|improve this question















I have a large list of files exists inside a particular directory (with full path). I'm trying to delete files from this directory where the line count in the file is greater than 1 (2 or more).



How can it be done?







linux






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 15 at 8:07









Isaac

7,1061834




7,1061834










asked Aug 15 at 7:53









Diksha

11




11




closed as unclear what you're asking by Vlastimil, schily, dr01, msp9011, Wildcard Aug 15 at 23:31


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.






closed as unclear what you're asking by Vlastimil, schily, dr01, msp9011, Wildcard Aug 15 at 23:31


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.









  • 2




    More detail would be useful: What OS, which shell you use, one file or several?
    – Isaac
    Aug 15 at 8:10






  • 1




    Sorry I am struggling to understand your question. Can you amend question to explain: What you mean by “with full path”? and “where the line count in the file is greater than 1”? (is this the number of line of the filename? if so is it just one line but wrapping, or is it more than one line?
    – ctrl-alt-delor
    Aug 15 at 9:48










  • I do not understand the question at all, voting to close.
    – Vlastimil
    Aug 15 at 11:26










  • Interestingly enough, I almost never have difficulties understanding these sort of questions, claimed by the powers-to-be. This question is perfectly clear.
    – ajeh
    Aug 15 at 21:31






  • 1




    Possible duplicate of List (or move) only files with a certain number of lines?
    – Wildcard
    Aug 15 at 23:31












  • 2




    More detail would be useful: What OS, which shell you use, one file or several?
    – Isaac
    Aug 15 at 8:10






  • 1




    Sorry I am struggling to understand your question. Can you amend question to explain: What you mean by “with full path”? and “where the line count in the file is greater than 1”? (is this the number of line of the filename? if so is it just one line but wrapping, or is it more than one line?
    – ctrl-alt-delor
    Aug 15 at 9:48










  • I do not understand the question at all, voting to close.
    – Vlastimil
    Aug 15 at 11:26










  • Interestingly enough, I almost never have difficulties understanding these sort of questions, claimed by the powers-to-be. This question is perfectly clear.
    – ajeh
    Aug 15 at 21:31






  • 1




    Possible duplicate of List (or move) only files with a certain number of lines?
    – Wildcard
    Aug 15 at 23:31







2




2




More detail would be useful: What OS, which shell you use, one file or several?
– Isaac
Aug 15 at 8:10




More detail would be useful: What OS, which shell you use, one file or several?
– Isaac
Aug 15 at 8:10




1




1




Sorry I am struggling to understand your question. Can you amend question to explain: What you mean by “with full path”? and “where the line count in the file is greater than 1”? (is this the number of line of the filename? if so is it just one line but wrapping, or is it more than one line?
– ctrl-alt-delor
Aug 15 at 9:48




Sorry I am struggling to understand your question. Can you amend question to explain: What you mean by “with full path”? and “where the line count in the file is greater than 1”? (is this the number of line of the filename? if so is it just one line but wrapping, or is it more than one line?
– ctrl-alt-delor
Aug 15 at 9:48












I do not understand the question at all, voting to close.
– Vlastimil
Aug 15 at 11:26




I do not understand the question at all, voting to close.
– Vlastimil
Aug 15 at 11:26












Interestingly enough, I almost never have difficulties understanding these sort of questions, claimed by the powers-to-be. This question is perfectly clear.
– ajeh
Aug 15 at 21:31




Interestingly enough, I almost never have difficulties understanding these sort of questions, claimed by the powers-to-be. This question is perfectly clear.
– ajeh
Aug 15 at 21:31




1




1




Possible duplicate of List (or move) only files with a certain number of lines?
– Wildcard
Aug 15 at 23:31




Possible duplicate of List (or move) only files with a certain number of lines?
– Wildcard
Aug 15 at 23:31










3 Answers
3






active

oldest

votes

















up vote
2
down vote













You can use this. Before you execute it, you should first try with echo instead of rm.



for i in dir/*; do
lines=$(wc -l "$i")
if test $lines -gt 1; then
rm "$i"
fi
done





share|improve this answer





























    up vote
    1
    down vote













    Try this,



    find . -type f -maxdepth 1 -exec bash -c '[[ $(wc -l < "$1") -gt 1 ]] && rm "$1"' _ '' ;



    • . -type f -maxdepth 1 to find files in the current directory


    • $(wc -l < "$1") -gt 1 check if the line count of is greater than 1


    • rm "$1"' _ ' remove files only in the current directory.





    share|improve this answer






















    • Hmm, I can't see how this would be limited to files in the current directory? You'd need -maxdepth 1 or some -type d -prune style trick
      – ilkkachu
      Aug 15 at 9:16

















    up vote
    1
    down vote













    The awk solution:



    wc -l /path/to/dir/* | head -n -1` | awk '$1>1 print $2' | xargs rm


    Notes:



    • No support for special characters in that simple version

    • Remember that wc -l doesn't count lines but occurrences of linefeeds. So a file with two lines (but without a LF on the second one) will be reported has having "1" line.





    share|improve this answer






















    • as per OP, $1 should be greater then 1
      – user294648
      Aug 15 at 17:56










    • Oops. Fixed. Thx.
      – xenoid
      Aug 15 at 21:09

















    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote













    You can use this. Before you execute it, you should first try with echo instead of rm.



    for i in dir/*; do
    lines=$(wc -l "$i")
    if test $lines -gt 1; then
    rm "$i"
    fi
    done





    share|improve this answer


























      up vote
      2
      down vote













      You can use this. Before you execute it, you should first try with echo instead of rm.



      for i in dir/*; do
      lines=$(wc -l "$i")
      if test $lines -gt 1; then
      rm "$i"
      fi
      done





      share|improve this answer
























        up vote
        2
        down vote










        up vote
        2
        down vote









        You can use this. Before you execute it, you should first try with echo instead of rm.



        for i in dir/*; do
        lines=$(wc -l "$i")
        if test $lines -gt 1; then
        rm "$i"
        fi
        done





        share|improve this answer














        You can use this. Before you execute it, you should first try with echo instead of rm.



        for i in dir/*; do
        lines=$(wc -l "$i")
        if test $lines -gt 1; then
        rm "$i"
        fi
        done






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Aug 15 at 9:13









        ilkkachu

        51.1k678141




        51.1k678141










        answered Aug 15 at 7:56









        RalfFriedl

        3,7001523




        3,7001523






















            up vote
            1
            down vote













            Try this,



            find . -type f -maxdepth 1 -exec bash -c '[[ $(wc -l < "$1") -gt 1 ]] && rm "$1"' _ '' ;



            • . -type f -maxdepth 1 to find files in the current directory


            • $(wc -l < "$1") -gt 1 check if the line count of is greater than 1


            • rm "$1"' _ ' remove files only in the current directory.





            share|improve this answer






















            • Hmm, I can't see how this would be limited to files in the current directory? You'd need -maxdepth 1 or some -type d -prune style trick
              – ilkkachu
              Aug 15 at 9:16














            up vote
            1
            down vote













            Try this,



            find . -type f -maxdepth 1 -exec bash -c '[[ $(wc -l < "$1") -gt 1 ]] && rm "$1"' _ '' ;



            • . -type f -maxdepth 1 to find files in the current directory


            • $(wc -l < "$1") -gt 1 check if the line count of is greater than 1


            • rm "$1"' _ ' remove files only in the current directory.





            share|improve this answer






















            • Hmm, I can't see how this would be limited to files in the current directory? You'd need -maxdepth 1 or some -type d -prune style trick
              – ilkkachu
              Aug 15 at 9:16












            up vote
            1
            down vote










            up vote
            1
            down vote









            Try this,



            find . -type f -maxdepth 1 -exec bash -c '[[ $(wc -l < "$1") -gt 1 ]] && rm "$1"' _ '' ;



            • . -type f -maxdepth 1 to find files in the current directory


            • $(wc -l < "$1") -gt 1 check if the line count of is greater than 1


            • rm "$1"' _ ' remove files only in the current directory.





            share|improve this answer














            Try this,



            find . -type f -maxdepth 1 -exec bash -c '[[ $(wc -l < "$1") -gt 1 ]] && rm "$1"' _ '' ;



            • . -type f -maxdepth 1 to find files in the current directory


            • $(wc -l < "$1") -gt 1 check if the line count of is greater than 1


            • rm "$1"' _ ' remove files only in the current directory.






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Aug 15 at 10:00

























            answered Aug 15 at 7:58









            msp9011

            3,46643862




            3,46643862











            • Hmm, I can't see how this would be limited to files in the current directory? You'd need -maxdepth 1 or some -type d -prune style trick
              – ilkkachu
              Aug 15 at 9:16
















            • Hmm, I can't see how this would be limited to files in the current directory? You'd need -maxdepth 1 or some -type d -prune style trick
              – ilkkachu
              Aug 15 at 9:16















            Hmm, I can't see how this would be limited to files in the current directory? You'd need -maxdepth 1 or some -type d -prune style trick
            – ilkkachu
            Aug 15 at 9:16




            Hmm, I can't see how this would be limited to files in the current directory? You'd need -maxdepth 1 or some -type d -prune style trick
            – ilkkachu
            Aug 15 at 9:16










            up vote
            1
            down vote













            The awk solution:



            wc -l /path/to/dir/* | head -n -1` | awk '$1>1 print $2' | xargs rm


            Notes:



            • No support for special characters in that simple version

            • Remember that wc -l doesn't count lines but occurrences of linefeeds. So a file with two lines (but without a LF on the second one) will be reported has having "1" line.





            share|improve this answer






















            • as per OP, $1 should be greater then 1
              – user294648
              Aug 15 at 17:56










            • Oops. Fixed. Thx.
              – xenoid
              Aug 15 at 21:09














            up vote
            1
            down vote













            The awk solution:



            wc -l /path/to/dir/* | head -n -1` | awk '$1>1 print $2' | xargs rm


            Notes:



            • No support for special characters in that simple version

            • Remember that wc -l doesn't count lines but occurrences of linefeeds. So a file with two lines (but without a LF on the second one) will be reported has having "1" line.





            share|improve this answer






















            • as per OP, $1 should be greater then 1
              – user294648
              Aug 15 at 17:56










            • Oops. Fixed. Thx.
              – xenoid
              Aug 15 at 21:09












            up vote
            1
            down vote










            up vote
            1
            down vote









            The awk solution:



            wc -l /path/to/dir/* | head -n -1` | awk '$1>1 print $2' | xargs rm


            Notes:



            • No support for special characters in that simple version

            • Remember that wc -l doesn't count lines but occurrences of linefeeds. So a file with two lines (but without a LF on the second one) will be reported has having "1" line.





            share|improve this answer














            The awk solution:



            wc -l /path/to/dir/* | head -n -1` | awk '$1>1 print $2' | xargs rm


            Notes:



            • No support for special characters in that simple version

            • Remember that wc -l doesn't count lines but occurrences of linefeeds. So a file with two lines (but without a LF on the second one) will be reported has having "1" line.






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Aug 15 at 21:08

























            answered Aug 15 at 10:48









            xenoid

            1,7051620




            1,7051620











            • as per OP, $1 should be greater then 1
              – user294648
              Aug 15 at 17:56










            • Oops. Fixed. Thx.
              – xenoid
              Aug 15 at 21:09
















            • as per OP, $1 should be greater then 1
              – user294648
              Aug 15 at 17:56










            • Oops. Fixed. Thx.
              – xenoid
              Aug 15 at 21:09















            as per OP, $1 should be greater then 1
            – user294648
            Aug 15 at 17:56




            as per OP, $1 should be greater then 1
            – user294648
            Aug 15 at 17:56












            Oops. Fixed. Thx.
            – xenoid
            Aug 15 at 21:09




            Oops. Fixed. Thx.
            – xenoid
            Aug 15 at 21:09


            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay