国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Linux > 正文

Shell腳本統計當前目錄下目錄和文件的數量

2019-10-26 18:43:43
字體:
來源:轉載
供稿:網友

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

可以看到,數據統計是準確的。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 嘉兴市| 永和县| 普洱| 蒙自县| 宽甸| 茌平县| 福州市| 和硕县| 商都县| 临海市| 衡阳县| 锦屏县| 旺苍县| 凤庆县| 留坝县| 三门峡市| 泗洪县| 利川市| 临桂县| 淮南市| 武安市| 高台县| 大悟县| 交城县| 平阴县| SHOW| 西充县| 龙胜| 桐乡市| 防城港市| 休宁县| 大埔县| 太保市| 巴彦淖尔市| 太白县| 奇台县| 即墨市| 麻阳| 南澳县| 新乡县| 新巴尔虎左旗|