linux删除文件行数,Linux:删除不包含特定行数的文件

这个bash脚本应该可以解决问题.保存为“rmlc.sh”.

样品用法:

rmlc.sh -more 20 *.txt # Remove all .txt files with more than 20 lines

rmlc.sh -less 15 * # Remove ALL files with fewer than 15 lines

请注意,如果rmlc.sh脚本位于当前目录中,则会对其进行保护以防删除.

#!/bin/sh

# rmlc.sh - Remove by line count

SCRIPTNAME="rmlc.sh"

IFS=""

# Parse arguments

if [ $# -lt 3 ]; then

echo "Usage:"

echo "$SCRIPTNAME [-more|-less] [numlines] file1 file2..."

exit

fi

if [ $1 == "-more" ]; then

COMPARE="-gt"

elif [ $1 == "-less" ]; then

COMPARE="-lt"

else

echo "First argument must be -more or -less"

exit

fi

LINECOUNT=$2

# Discard non-filename arguments

shift 2

for filename in $*; do

# Make sure we're dealing with a regular file first

if [ ! -f "$filename" ]; then

echo "Ignoring $filename"

continue

fi

# We probably don't want to delete ourselves if script is in current dir

if [ "$filename" == "$SCRIPTNAME" ]; then

continue

fi

# Feed wc with stdin so that output doesn't include filename

lines=`cat "$filename" | wc -l`

# Check criteria and delete

if [ $lines $COMPARE $LINECOUNT ]; then

echo "Deleting $filename"

rm "$filename"

fi

done

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值