判断所给目录没有某个文件

回复 收藏
判断所给目录内拿些二级目录下没有text.txt文件。有text.txt文件的子目录根据文件计算选项中单词数最大的值(选项间以|分割,单词间以空格分隔)
2016-08-17 11:04 举报
已邀请:
0

李宏伟

赞同来自:

#!/bin/sh

#需求:
#判断所给目录内哪些二级子目录下没有text.txt。有text.txt文件的子目录根据文件计算选项中单词数最大的值(选项间以|分隔,单词数以空格分隔)
#2016年9月1日 & 李宏伟
#version 1

cd /tmp/0101
for i in `ls`
  do
        if
        [ -f ${i}/test.txt ]
        then
        n=`cat ${i}/test.txt |wc -w` && echo "$n $i |" >> /tmp/tongji.txt
        fi
  done
cat /tmp/tongji.txt |xargs
echo "please hold on latter" && sleep 3
sort -k1 -rn /tmp/tongji.txt |xargs
echo > /tmp/tongji.txt


0

kw是id

赞同来自:

没看懂题目的需求,我私自改了下需求,查找/tmp下的二级子目录有无word.txt文件,没有则显示没有,如果有word.txt,统计出文件里字母最多的一个单词

留作存档,我忘了当时怎么写的

QQ截图20161121001039.jpg

#!/bin/bash
a=`find /tmp -maxdepth 3 -type f -name 'word.txt'`
if [ -z "$a" ]
then
   echo "file word.txt is not under /tmp"
   exit
else
   for i in `cat $a`
   do
     b=`echo $i |wc -m`
     let c=b-1
     echo $b $i >>/tmp/count.txt
   done
fi
cat /tmp/count.txt |sort -rn |head -n1 |awk '{print $2}'

0

王斌

赞同来自:

#!/bin/bash

#Auth: Citizen Wang

#Scripts: Search file test

read -p "Please type whole path of directory: " d

for i in `find $d -mindepth 1 -maxdepth 2 -type f -name test`

do

    if [ -n "$i" ];then

        echo $i

        echo '' > /tmp/count.txt

        for m in `cat $i`

        do

            b=`echo $m |wc -m`

            echo $b $m >> /tmp/count.txt

        done

    fi

cat /tmp/count.txt | sort -nr |head -n1 | awk '{print $2}'

done

回复帖子,请先登录注册

退出全屏模式 全屏模式 回复
评分
可选评分理由: