Shell腳本統(tǒng)計(jì)當(dāng)前目錄下目錄和文件的數(shù)量
更新時(shí)間:2014年12月22日 09:18:57 投稿:junjie
這篇文章主要介紹了Shell腳本統(tǒng)計(jì)當(dāng)前目錄下目錄和文件的數(shù)量,Linux下如何統(tǒng)計(jì)當(dāng)前目錄下文件有多少個(gè),目錄又有多少個(gè)呢,使用本文腳本即可實(shí)現(xiàn),需要的朋友可以參考下
Linux下如何統(tǒng)計(jì)當(dāng)前目錄下文件有多少個(gè),目錄又有多少個(gè)呢?
下面用shell寫一個(gè)腳本,放置在當(dāng)前目錄下,執(zhí)行即可。
復(fù)制代碼 代碼如下:
#!/bin/bash
#腳本名稱 dir
#定義一個(gè)函數(shù)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"
}
#調(diào)用函數(shù)
fun_directory
我們測試一下:
復(fù)制代碼 代碼如下:
[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
#執(zhí)行腳本
[root@localhost scripts]# sh dir
The number of directorys is 2
The number of files is 20
[root@localhost scripts]#
可以看到,數(shù)據(jù)統(tǒng)計(jì)是準(zhǔn)確的。
相關(guān)文章
shell監(jiān)控腳本實(shí)例—監(jiān)控mysql主從復(fù)制
分享一例shell腳本,用于監(jiān)測mysql數(shù)據(jù)庫的主從復(fù)制,有需要的朋友不妨參考學(xué)習(xí)下2013-11-11
一句話Shell命令關(guān)閉不需要的隨機(jī)啟動服務(wù)
這篇文章主要介紹了一句話Shell命令關(guān)閉不需要的隨機(jī)啟動服務(wù),使用本文的一句話命令,可以達(dá)到優(yōu)化系統(tǒng)的目的,其中要保留的服務(wù)可以自己定義,需要的朋友可以參考下2014-12-12
linux使用select實(shí)現(xiàn)精確定時(shí)器詳解
本文講述如何使用select實(shí)現(xiàn)超級時(shí)鐘。使用select函數(shù),我們能實(shí)現(xiàn)微妙級別精度的定時(shí)器。同時(shí),select函數(shù)也是我們在編寫非阻塞程序時(shí)經(jīng)常用到的一個(gè)函數(shù)2013-11-11
學(xué)習(xí)shell腳本之前的基礎(chǔ)知識[圖文]
在學(xué)習(xí)shell腳本之前,需要你了解很多關(guān)于shell的知識,這些知識是編寫shell腳本的基礎(chǔ),所以希望你能夠熟練的掌握2013-03-03
bash scp command not found的解決方法
今天在一系統(tǒng)上運(yùn)行bash scp提示command not found,經(jīng)過如下方法解決了,需要的朋友可以參考下2013-03-03

