简体   繁体   English

Bash脚本-显示目录中文件和文件夹的数量

[英]Bash Scripting - Display number of files and folders in a directory

I'm new to bash scripting, and I found this example on stackoverflow . 我是bash脚本的新手,我在stackoverflow上找到了此示例 Now, it seems this is what I need however, I'm unsure of a few things. 现在,看来这是我需要的,但是我不确定一些事情。

  1. How would I properly modify it to display the number of files and folders in the downloads directory 我如何正确修改它以显示下载目录中的文件和文件夹数量

  2. What does the "$@" mean? “ $ @”是什么意思?

My code so far: 到目前为止,我的代码:

cleanPath="/home/someuser/Downloads/*"

if [ -d $cleanPath]; then
    find "$cleanPath" -type f | ls -l $cleanPath | wc -l | echo "Number of files is $@"
    find "$cleanPath" -type d | ls -l $cleanPath | wc -l | echo "Number of directorieis $@"
fi

You're nearly there, but you have some syntax errors (spaces needed around if statement brackets) and some other mistakes. 您快到这里了,但是您会遇到一些语法错误(if语句括号内需要空格)和其他一些错误。 cleanPath="/home/someuser/Downloads/*" can cause problems if you don't properly quote cleanPath like "$cleanPath" because the shell expands *, so you actually get a list of all files and directories in Downloads (try echo $cleanPath and you will see). 如果由于外壳扩展*而没有正确引用cleanPath之类的"$cleanPath"cleanPath="/home/someuser/Downloads/*"可能会引起问题,因此您实际上会获得Downloads中所有文件和目录的列表(尝试echo $cleanPath ,您将看到)。 Also, I don't see why you pass the output of find to ls, ls will not even use the input, it will just list all the files and directories. 另外,我不明白为什么将find的输出传递给ls,ls甚至不会使用输入,它只会列出所有文件和目录。

Try this: 尝试这个:

cleanPath="/home/someuser/Downloads"

if [ -d "$cleanPath" ]; then
  echo "No of files is ""$(find "$cleanPath" -mindepth 1 -type f | wc -l)"
  echo "No of directories is ""$(find "$cleanPath" -mindepth 1 -type d | wc -l)"
fi

Be aware this is recursive - default find behaviour. 请注意,这是递归的-默认为find行为。 You didn't make it clear whether that was what you wanted. 您没有明确说明这是否是您想要的。 For a non recursive list: 对于非递归列表:

cleanPath="/home/someuser/Downloads"

if [ -d "$cleanPath" ]; then
  echo "No of files is ""$(find "$cleanPath" -mindepth 1 -maxdepth 1 -type f | wc -l)"
  echo "No of directories is ""$(find "$cleanPath" -mindepth 1 -maxdepth 1 -type d | wc -l)"
fi

Also you can use $@ as an array representation of all the positional parameters passed to a script. 您也可以使用$@作为传递给脚本的所有位置参数的数组表示形式。

https://www.gnu.org/software/bash/manual/html_node/Special-Parameters.html#index-_0024_0040 https://www.gnu.org/software/bash/manual/html_node/Special-Parameters.html#index-_0024_0040

Beware: -mindepth -maxdepth are not POSIX compliant, and this doesn't work for files with newlines. 注意:-mindepth -maxdepth不符合POSIX,并且不适用于带有换行符的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Linux bash脚本同时用于2个文件夹 - Linux bash scripting for 2 folders at the same time 仅显示 tcsh 或 bash 中为符号链接的文件和文件夹 - Display only files and folders that are symbolic links in tcsh or bash 在 Shell Scripting Array of folders in directory 和显示每个文件夹中的文件,重命名/删除选项 - In Shell Scripting Array of folders in directory and show files in each folder, rename/delete option 如何使bash脚本按特定编号对文件夹中的文件进行分类 - How to make a bash script classify files in folders by a specific number Bash脚本:跳过文件和grep - Bash scripting: Skipping files and grep 如何通过限制bash shell脚本中的线程数来一次复制五个文件而不是一个文件? - How to copy five files at once instead of one file by limiting the number of threads in bash shell scripting? 如何在 Bash 中使用 ls 命令仅显示目录中的隐藏文件 - How to display only hidden files in directory using ls command in Bash BASH遍历文件夹和文件 - BASH loop through folders and files 示例Bash脚本或命令以检索目录中的数字文件 - sample Bash script or command to retrieve number files in directory BASH:如果目录中的文件数大于或等于2,则需要运行if语句 - BASH: If statement needed to run if number of files in directory is 2 or greater
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM