读取文件名,统计文件数量

[root@linuxstudy sh]# cat ./s1.sh
#!/bin/bash


filelist=$(ls ~/)

echo "-----------begin------------"
for file in $filelist
do
echo $file
   if [ -f ~/$file ]
  
   then  echo "this is a file"
 
   else   echo "this is a dir"
 
   fi

done

echo "-----------end------------"


[root@linuxstudy sh]# ./s1.sh
-----------begin------------
anaconda-ks.cfg
this is a file
apache-maven-3.1.1-bin.tar.gz
this is a file
apache-maven-3.2.5-bin.tar.gz
this is a file
Desktop
this is a dir
Documents
this is a dir
Downloads
this is a dir
forktest.py
this is a file
forktest.py~
this is a file
glibc-2.17
this is a dir
glibc-2.17.tar.gz
this is a file
glibc-2.9.tar.bz2
this is a file
glibc-linuxthreads-2.5.tar.bz2
this is a file
hadoop-2.6.0-src.tar.gz
this is a file
hadoopsrc
this is a dir
ifcfg-eth0
this is a file
ifcfg-lo
this is a file
install.log
this is a file


[root@linuxstudy sh]# ll
total 600
-rwxr-xr-x. 1 root root     47 May  3 05:43 calculate.sh
-rwxr-xr-x. 1 root root    200 May  3 06:10 canshu.sh
-rwxr-xr-x. 1 root root     84 Aug 23 08:16 cs.sh
-rwxr-xr-x. 1 root root    162 May  3 05:47 hello.sh
-rwxr-xr-x. 1 root root    227 May  3 06:22 readtest.sh
-rwxr-xr-x. 1 root root    246 Aug 31 08:23 s1.sh
-rw-r--r--. 1 root root 575289 May  9 19:22 services
-rw-r--r--. 1 root root     25 Aug 31 08:09 stest.txt
-rw-r--r--. 1 root root     69 May  4 07:09 student.txt
-rw-r--r--. 1 root root    140 May  9 18:18 vitest.txt
[root@linuxstudy sh]# ls -l | grep ^- | wc -l
10
[root@linuxstudy sh]#

猜你喜欢

转载自fypop.iteye.com/blog/2239810