Shell脚本统计当前目录下目录和文件的数量

 更新时间:2014年12月22日 09:18:57   投稿:junjie  
这篇文章主要介绍了Shell脚本统计当前目录下目录和文件的数量,Linux下如何统计当前目录下文件有多少个,目录又有多少个呢,使用本文脚本即可实现,需要的朋友可以参考下

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]#

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

相关文章

  • 阿里云主机一键安装lamp、lnmp环境的shell脚本分享

    阿里云主机一键安装lamp、lnmp环境的shell脚本分享

    这篇文章主要介绍了阿里云主机一键安装lamp、lnmp环境的shell脚本分享,需要的朋友可以参考下
    2014-07-07
  • Linux中sftp常用命令整理

    Linux中sftp常用命令整理

    sftp命令是一款交互式的文件传输程序,不仅对传输的所有信息使用ssh加密,它还支持公钥认证和压缩等功能。这篇文章整理了Linux中sftp的常用命令,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-06-06
  • Linux截取某一段时间的日志问题

    Linux截取某一段时间的日志问题

    这篇文章主要介绍了Linux截取某一段时间的日志问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-01-01
  • Linux Shell函数返回值

    Linux Shell函数返回值

    这篇文章主要介绍了Linux Shell函数返回值,需要的朋友可以参考下
    2016-11-11
  • shell脚本编程之if语句学习笔记

    shell脚本编程之if语句学习笔记

    这篇文章主要介绍了shell脚本编程之if语句学习笔记,本文先是给出了程序代码,然后详细的分解了第句代码的作用,需要的朋友可以参考下
    2014-09-09
  • 一天一个shell命令 linux文本系列-file命令用法

    一天一个shell命令 linux文本系列-file命令用法

    这篇文章主要介绍了一天一个shell命令 linux文本系列-file命令用法,需要的朋友可以参考下
    2016-06-06
  • Linux文件查找和解压缩命令详解

    Linux文件查找和解压缩命令详解

    这篇文章主要介绍了Linux文件查找和解压缩命令,文件搜索查找包括按照名字搜索,按照文件所属`主用户`搜索,本文给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧
    2024-02-02
  • shell脚本批量删除es索引的方法

    shell脚本批量删除es索引的方法

    今天小编就为大家分享一篇关于shell脚本批量删除es索引的方法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-03-03
  • 建立基于虚拟用户的VSftpd服务

    建立基于虚拟用户的VSftpd服务

    这篇文章主要介绍了建立基于虚拟用户的VSftpd服务的相关资料,需要的朋友可以参考下
    2015-09-09
  • shell脚本打印日期时间的实现示例

    shell脚本打印日期时间的实现示例

    在shell 中,可以使用 `date` 命令打印系统时间,本文主要介绍了shell脚本打印日期时间的实现示例,具有一定的参考价值,感兴趣的可以了解一下
    2023-09-09

最新评论