编写一个shell脚本,其功能是显示当前工作目录中的文件数量,但不显示子目录

问题描述 投票:0回答:1

编写一个 shell 脚本,其中包含一个显示当前工作目录中文件数量的函数,但不显示子目录


# Function to count files in the pwd excluding sub directories 
files_in_pwd() {
    num_files=$(ls -l | wc -l) |  -maxdepth 1
    echo "Number of files in the pwd: $num_files"
}

# Call the function
files_in_pwd

 

bash function subdirectory
1个回答
0
投票

find
-type f
选项一起使用可仅对常规文件进行计数,并使用
-maxdepth 1
来防止进入子目录。

files_in_pwd() {
    num_files=$(find . -maxdepth 1 -type f | wc -l)
    echo "Number of files in the pwd: $num_files"
}
© www.soinside.com 2019 - 2024. All rights reserved.