gpt4 book ai didi

Linux watch 命令 "no such file or directory"

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:03:35 25 4
gpt4 key购买 nike

在 Linux 中,我试图监视当前目录的所有子目录并输出它们的文件数。我目前的命令是:

watch 'for FILE in `find . -maxdepth 1 -mindepth 1 -type d | grep -v "unwanted\|directories\|here"`; do echo -n $FILE " "; find $FILE -type f | wc -l; done | column -t'

这很好用,除了偶尔会出现这个错误,

find: `FILE_NAME': No such file or directory

其中 FILE_NAME 是其中一个子目录中文件的名称。

有谁知道为什么会发生这种情况以及如何解决?谢谢!

最佳答案

这只使用 bash 内置函数(但据我所知,唯一的 bashism 是 export -f,所以如果你想把它变成一个难看的、不可读的单行代码,你可以使用/bin/sh)

#!/bin/bash
myfunc(){
for DIR in "$1/"*; do
case "$DIR" in
unwanted|dirs)continue;;
*)[ -d "$DIR" ] && {
printf "%30s " "$DIR"
i=0
for FILE in "$DIR/"*; do
[ -f "$FILE" ] && i=$((i+1))
done
echo $i
};;
esac
done
}
export -f myfunc
watch myfunc "$HOME"

请注意,任何可能是带空格的文件名的内容都用双引号引起来,以防止尝试统计 2 个目录:Program 和 Files 而不是“Program Files”。最好的做法是对可能有分隔符的变量进行双引号(顺便说一句,文件名中可以​​有制表符和换行符 + 一些非常奇怪的东西 - 除了 "/"几乎什么都没有)

关于Linux watch 命令 "no such file or directory",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16574968/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com