如何获得一个文件夹中的变量文件的数量? [英] How to get the number of files in a folder as a variable?

查看:187
本文介绍了如何获得一个文件夹中的变量文件的数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用bash,一个人如何能得到的文件数量的文件夹中,排除一个shell脚本目录,而跨preTER抱怨?

Using bash, how can one get the number of files in a folder, excluding directories from a shell script without the interpreter complaining?

通过一个朋友的帮助下,我试过

With the help of a friend, I've tried

$files=$(find ../ -maxdepth 1 -type f | sort -n)
$num=$("ls -l" | "grep ^-" | "wc -l")

从命令行返回:

../1-prefix_blended_fused.jpg: No such file or directory
ls -l :  command not found
grep ^-: command not found
wc -l:   command not found

分别。这些命令在命令行上工作,但不是一个bash脚本。

respectively. These commands work on the command line, but NOT with a bash script.

鉴于充满1- pano.jpg 格式如图像文件,我想抓住目录中的所有图像来获得最大的编号的文件钉到一个文件接下来的图像正在处理中。

Given a file filled with image files formatted like 1-pano.jpg, I want to grab all the images in the directory to get the largest numbered file to tack onto the next image being processed.

为什么不一致?

推荐答案

引号导致该错误消息。

要获得文件的计数目录:

To get a count of files in the directory:

numfiles=(*)
numfiles=${#numfiles[@]}

它创建的阵列,然后用其元素的计数替换它。这将包括文件和目录,而不是点文件或 .. 或其它点目录。

您可以改用找到型的F 或者你可以指望的目录加减:

You can instead use find -type f or you can count the directories and subtract:

# continuing from above
numdirs=(*/)
numdirs=${#numdirs[@]}
(( numfiles -= numdirs ))

另请参阅我怎么能找到最新的(最新的,最早的,最古老的)目录中的文件?

只要你想执行块里面你可以有很多的空间。他们往往有助于可读性。唯一的缺点是,它们使文件有点大,可能(只)稍慢初步分析。有几个地方必须有空格(如周围 [ [] ]] = 在比较中)和少数不得(如各地 = 中的分配。

You can have as many spaces as you want inside an execution block. They often aid in readability. The only downside is that they make the file a little larger and may slow initial parsing (only) slightly. There are a few places that must have spaces (e.g. around [, [[, ], ]] and = in comparisons) and a few that must not (e.g. around = in an assignment.

这篇关于如何获得一个文件夹中的变量文件的数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆