Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Shell script to count the number of directories and files in the current directory

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to use Shell script to count the number of directories and files under the current directory", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, to study and learn "how to use Shell scripts to count the number of directories and files under the current directory"!

Write a script in shell, place it in the current directory, and execute it.

The code is as follows:

#! / bin/bash

# script name dir

# define a function 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"

}

# call function

Fun_directory

Let's test it:

The code is as follows:

[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 2222 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

# execute script

[root@localhost scripts] # sh dir

The number of directorys is 2

The number of files is 20

[root@localhost scripts] #

As you can see, the statistics are accurate.

Thank you for your reading, the above is the content of "how to use Shell script to count the number of directories and files under the current directory". After the study of this article, I believe you have a deeper understanding of how to use Shell script to count the number of directories and files under the current directory. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report