我试图在给定的目录中查找目录和文件的数量。我正在像这样运行我的bash脚本:
ARCHIVE=/path/to/archive ./myScript
在我的脚本中,我正在这样做:
#find the number of non-empty directories in the given dir
dirs=$(find $ARCHIVE -mindepth 1 -maxdepth 1 -not -empty -type d | wc -l)
#find the number of files in the given dir
msgs=$(find $ARCHIVE -type f | wc -l)
echo "Number of directories: $dirs"
echo "Total number of messages: $msgs"
当我在要查看的数据子集上运行脚本时,该脚本非常有用,该子集位于与脚本相同级别的目录中。但是,实际数据位于其他人的目录中,并且当我在ARCHIVE变量设置为该位置的情况下运行它时,两个值都返回0。我也使用了类似的脚本,并且在那里的find命令不起作用第二个目录。奇怪的是,我使用了一些egrep命令,它们对于两个命令都可以正常工作。
为什么不能以这种方式使用查找?
尝试将要搜索的目录作为参数传递给bash脚本:
#!/usr/bin/env bash
# First argument to script shall be directory in which to search
ARCHIVE=$1
#find the number of non-empty directories in the given dir
dirs=$(find "$ARCHIVE" -mindepth 1 -maxdepth 1 -not -empty -type d | wc -l)
#find the number of files in the given dir
msgs=$(find "$ARCHIVE" -type f | wc -l)
echo "Number of directories: $dirs"
echo "Total number of messages: $msgs"
dirfiles
在我的主目录中运行名为的脚本:
$ ./dirfiles ~
Number of directories: 27
Total number of messages: 8703
并继续/usr/lib
:
$ ./dirfiles /usr/lib
Number of directories: 161
Total number of messages: 9630
此外,find
提供了三种解决符号链接的方法:
-P
:不要遵循符号链接-L
:跟随符号链接-H
:除了处理命令行参数时,不要跟随符号链接。如果您不想遵循符号链接,而$ARCHIVE
恰好是其中之一,那么也许-H
是路要走。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句