-2

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?

asked Aug 15, 2018 at 7:53
5
  • 2
    More detail would be useful: What OS, which shell you use, one file or several? Commented Aug 15, 2018 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? Commented Aug 15, 2018 at 9:48
  • I do not understand the question at all, voting to close. Commented Aug 15, 2018 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. Commented Aug 15, 2018 at 21:31
  • 1
    Possible duplicate of List (or move) only files with a certain number of lines? Commented Aug 15, 2018 at 23:31

3 Answers 3

2

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
ilkkachu
148k16 gold badges268 silver badges440 bronze badges
answered Aug 15, 2018 at 7:56
1

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.
answered Aug 15, 2018 at 7:58
1
  • 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 Commented Aug 15, 2018 at 9:16
1

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.
answered Aug 15, 2018 at 10:48
2
  • as per OP, 1ドル should be greater then 1 Commented Aug 15, 2018 at 17:56
  • Oops. Fixed. Thx. Commented Aug 15, 2018 at 21:09

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.