How to delete files with more than 1 line in a linux server? [closed]
Clash 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?
linux
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.
add a comment |Â
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?
linux
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
add a comment |Â
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?
linux
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
linux
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
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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 1rm "$1"' _ '
remove files only in the current directory.
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
add a comment |Â
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.
as per OP, $1 should be greater then 1
â user294648
Aug 15 at 17:56
Oops. Fixed. Thx.
â xenoid
Aug 15 at 21:09
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
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
edited Aug 15 at 9:13
ilkkachu
51.1k678141
51.1k678141
answered Aug 15 at 7:56
RalfFriedl
3,7001523
3,7001523
add a comment |Â
add a comment |Â
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 1rm "$1"' _ '
remove files only in the current directory.
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
add a comment |Â
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 1rm "$1"' _ '
remove files only in the current directory.
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
add a comment |Â
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 1rm "$1"' _ '
remove files only in the current directory.
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.
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
add a comment |Â
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
add a comment |Â
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.
as per OP, $1 should be greater then 1
â user294648
Aug 15 at 17:56
Oops. Fixed. Thx.
â xenoid
Aug 15 at 21:09
add a comment |Â
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.
as per OP, $1 should be greater then 1
â user294648
Aug 15 at 17:56
Oops. Fixed. Thx.
â xenoid
Aug 15 at 21:09
add a comment |Â
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.
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.
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
add a comment |Â
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
add a comment |Â
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