---
title: "遍历文件夹里的文件格式"
author: "Eileenlml"
date: "2017-05-26T17:29:00Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2017-06-06T19:51:09Z"
id: 2376568
url: "https://eileenlml.iteye.com/blog/2376568"
---

#!/bin/sh

. $HOME/.profile

 

DIRECTORY=/data/tran\_data/20170526

LOGDIR=/data/tran\_data/20170526/log

LOG=$LOGDIR/countdir.log

begin1=\`date +"%m/%d/%Y %T"\`

 

txt\_count=0  

xml\_count=0  

map\_count=0  

other\_count=0 

 

mail\_user=""

 

DIRECTORY=$1  

if \[ "\`ls -A $DIRECTORY\`" = "" ]; then  

  echo "$DIRECTORY is empty"  

else  

  echo "$DIRECTORY is not empty"  

fi  

  

    for file in \` ls $DIRECTORY \`  

    do  

        if \[ -f $DIRECTORY"/"$file ]  

        then  

        fileType=${file##\*.}  

                        if \[ "$fileType" = "txt" ];  

                         then  

                          txt\_count=$(($txt\_count+1))  

                        elif  \[ "$fileType" = "xml" ];  

                         then  

                          xml\_count=$(($xml\_count+1))  

                        elif  \[ "$fileType" = "map" ];  

                         then  

                          map\_count=$(($map\_count+1))  

                        else  

                          other\_count=$(($other\_count+1))  

                        fi  

                fi  

    done  

echo "'txt file count:'$txt\_count" &gt;&gt;$LOG  

echo "'xml file count:'$xml\_count" &gt;&gt;$LOG 

echo "'map file count:'$map\_count" &gt;&gt;$LOG 

echo "'other file count:'$other\_count" &gt;&gt;$LOG 

 

end1=\`date +"%T"\`

echo "Begin=\[ ${begin1} ] - End=\[ ${end1} ]"&gt;&gt;$LOG 

mailx -s "COUNTDIR, Begin=\[ ${begin1} ] - End=\[ ${end1} ]" ${mail\_users} &lt; /data/tran\_data/20170526/$LOG