用shell脚本统计当前目录下目录和文件的各自数量

简介:

Linux下如何统计当前目录下文件有多少个,目录又有多少个呢?


下面用shell写一个脚本,放置在当前目录下,执行即可。


#!/bin/bash

#脚本名称 dir
#定义一个函数fun_directory

fun_directory() {      
  let "filenum=0"
  let "dirnum=0"
  
for i in $( ls )
do
   if [ -d $i ]
   then
      let dirnum+=1
   else
      let filenum+=1
   fi
done

echo "The number of directorys is $dirnum"
echo "The number of files is $filenum"

}

#调用函数

fun_directory


我们测试一下:

 

[root@localhost scripts]# pwd
/root/scripts
[root@localhost scripts]# ll |sort
drwxr-xr-x 2 root root 4096 06-12 10:44 charpter8
drwxr-xr-x 2 root root 4096 06-13 12:34 aaa
-rw-r--r-- 1 root root  105 06-13 08:56 file1
-rw-r--r-- 1 root root  106 06-12 14:24 8-9
-rw-r--r-- 1 root root  121 06-12 09:36 jiu
-rw-r--r-- 1 root root  133 06-13 11:09 temp
-rw-r--r-- 1 root root  210 06-12 13:40 8-8
-rw-r--r-- 1 root root  222 06-12 11:51 8-6
-rw-r--r-- 1 root root  247 06-12 11:35 8-5
-rw-r--r-- 1 root root  273 06-12 13:13 8-7
-rw-r--r-- 1 root root  292 06-12 10:57 8-1
-rw-r--r-- 1 root root  309 06-12 14:51 8-11
-rw-r--r-- 1 root root  314 06-12 15:01 8-17
-rw-r--r-- 1 root root  317 06-13 12:10 test
-rw-r--r-- 1 root root   51 06-12 11:00 8-2
-rw-r--r-- 1 root root   53 06-13 08:51 file
-rw-r--r-- 1 root root   67 06-13 10:17 10-4
-rw-r--r-- 1 root root   78 06-13 10:09 test.out
-rwxr-xr-x 1 root root  103 06-12 11:06 8-3
-rwxr-xr-x 1 root root  124 06-13 10:02 10-32
-rwxr-xr-x 1 root root  304 06-13 12:47 dir
-rwxr-xr-x 1 root root  316 06-12 11:21 8-4
#执行脚本
[root@localhost scripts]# sh dir
The number of directorys is 2
The number of files is 20
[root@localhost scripts]#


可以看到,数据统计是准确的。




     本文转自marbury 51CTO博客,原文链接:http://blog.51cto.com/magic3/1425858,如需转载请自行联系原作者


相关文章
|
6天前
|
分布式计算 Hadoop Shell
使用shell脚本实现自动SSH互信功能
使用shell脚本实现自动SSH互信功能
17 1
|
6天前
|
Unix Shell Linux
轻松编写 AIX Shell 脚本
轻松编写 AIX Shell 脚本
14 1
|
6天前
|
监控 关系型数据库 Shell
Shell脚本入门:从基础到实践,轻松掌握Shell编程
Shell脚本入门:从基础到实践,轻松掌握Shell编程
|
6天前
|
关系型数据库 MySQL Shell
在Centos7中利用Shell脚本:实现MySQL的数据备份
在Centos7中利用Shell脚本:实现MySQL的数据备份
|
6天前
|
Shell Linux 编译器
C语言,Linux,静态库编写方法,makefile与shell脚本的关系。
总结:C语言在Linux上编写静态库时,通常会使用Makefile来管理编译和链接过程,以及Shell脚本来自动化构建任务。Makefile包含了编译规则和链接信息,而Shell脚本可以调用Makefile以及其他构建工具来构建项目。这种组合可以大大简化编译和构建过程,使代码更易于维护和分发。
30 5
|
6天前
|
Shell 程序员 数据安全/隐私保护
shell 脚本 if-else判断 和流程控制 (基本语法|基础命令)
shell 脚本 if-else判断 和流程控制 (基本语法|基础命令)
|
6天前
|
存储 Shell C语言
shell脚本 编程 变量 基本入门(详解)
shell脚本 编程 变量 基本入门(详解)
|
6天前
|
Shell Linux 编译器
C语言,Linux,静态库编写方法,makefile与shell脚本的关系。
总结:C语言在Linux上编写静态库时,通常会使用Makefile来管理编译和链接过程,以及Shell脚本来自动化构建任务。Makefile包含了编译规则和链接信息,而Shell脚本可以调用Makefile以及其他构建工具来构建项目。这种组合可以大大简化编译和构建过程,使代码更易于维护和分发。
16 3
|
6天前
|
弹性计算 运维 监控
|
6天前
|
存储 弹性计算 运维
自动化收集员工信息的Shell脚本
【4月更文挑战第30天】
14 0