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?
-
2More detail would be useful: What OS, which shell you use, one file or several?user232326– user2323262018年08月15日 08:10:14 +00:00Commented Aug 15, 2018 at 8:10
-
1Sorry 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– ctrl-alt-delor2018年08月15日 09:48:50 +00:00Commented Aug 15, 2018 at 9:48
-
I do not understand the question at all, voting to close.Vlastimil Burián– Vlastimil Burián2018年08月15日 11:26:59 +00:00Commented 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.ajeh– ajeh2018年08月15日 21:31:25 +00:00Commented Aug 15, 2018 at 21:31
-
1Possible duplicate of List (or move) only files with a certain number of lines?Wildcard– Wildcard2018年08月15日 23:31:17 +00:00Commented Aug 15, 2018 at 23:31
3 Answers 3
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
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 1rm "1ドル"' _ '
remove files only in the current directory.
answered Aug 15, 2018 at 7:58
-
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 trickilkkachu– ilkkachu2018年08月15日 09:16:00 +00:00Commented Aug 15, 2018 at 9:16
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
-
as per OP, 1ドル should be greater then 1user294648– user2946482018年08月15日 17:56:51 +00:00Commented Aug 15, 2018 at 17:56
-