如何删除多于或less于指定行的目录(所有文件都有“.txt”后缀)的文件?
validation失败
Linux / UNIX中COM编程的模拟
上次访问时间。 如何挂载根文件系统,一次,norelatime
SSH命令执行挂起,虽然交互式shellfunction正常
这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
玩了一下0x6adb015的答案。 这适用于我:
LInes=10 for f in *.txt; do a=`cat "$f" | wc -l`; if [ "$a" -ne "$LInes" ] then rm -f "$f" fi done
这一班轮也应该这样做
find -name '*.txt' | xargs wc -l | awk '{if($1 > 1000 && index($2,"txt")>0 ) print $2}' | xargs rm
选择>和<以及相应的行数。
试试这个bash脚本:
LInes=10 for f in *.txt; do if [ `cat "$f" | wc -l` -ne $LInes ]; then rm -f "$f" fi done
(未测试)
我的命令行混合是相当生锈的,但我认为这样的东西可以安全地工作(即使文件名中有空格,将“10”更改为grep中的任意行)。 根据需要调整。 如果文件名中的换行符是可能的,你需要调整它。
find . -name *.txt -type f -exec wc -l {} ; | grep -v "^10 .*$" | cut --complement -f 1 -d " " | tr ' 12' ' 00' | xargs -0 rm -f
rm `find $DIR -type f -exec wc -l {} ; | grep "^$RLInes " | awk '{print $2}'`
有人问这个问题有点迟了。 我刚才也有同样的问题,这就是乍得坎贝尔 ( Chad Campbell)的观点
find $DIR -name '*.txt' -exec wc -l {} ; | grep -v "$LInes" | awk '{print $2}' | xargs rm
第一部分查找以* .txt结尾的所有文件,并打印行数。
第三部分只打印文件名。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。