• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >linux shell > Shell脚本统计当前目录下目录和文件的数量

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

作者:junjie 字体:[增加 减小] 来源:互联网

junjie 通过本文主要向大家介绍了shell脚本创建目录,shell脚本切换目录,shell脚本遍历目录,shell脚本,shell脚本编程等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

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
</div>

我们测试一下:

[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]#
</div>

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

</div>

您可能想查找下面的文章:

  • Shell脚本监控目录内文件改动
  • 一个shell小案例(创建日期目录)
  • 获取shell脚本自身所在目录的Shell脚本分享
  • Shell脚本遍历目录并批量修改文件编码
  • Shell脚本统计当前目录下目录和文件的数量
  • 查找目录下同名但不同后缀名文件的shell脚本代码
  • shell遍历目录处理特定目录的脚本代码

相关文章

  • Shell脚本创建指定大小文件的测试数据
  • Linux进程通信(IPC)方式简介
  • Linux下查找后门程序 CentOS 查后门程序的shell脚本
  • 一句话Shell命令关闭不需要的随机启动服务
  • mysql源码安装脚本分享
  • shell中函数的应用
  • linux下2个检查tcp连接的命令
  • Python使用淘宝API查询IP归属地功能分享
  • Linux下的tar压缩解压缩命令详解(小结)
  • 利用管道实现sudo命令免输入密码的方法

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Linux 中C语言getcwd()函数的用法
    • linux删除无效链接文件脚本分享
    • 远程SSH连接服务与基本排错经验总结
    • linux shell 字符串操作(长度,查找,替换)详解
    • 关于vi和vim的区别及命令详解
    • Shell正则表达式验证IP地址
    • 分享20个Unix/Linux 命令技巧
    • linux系统安装字体详细介绍
    • Shell中if的基本语法和常见判断用法
    • Shell脚本实现检测文件是否被修改过代码分享

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有