编写一个 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
将
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"
}