我正在尝试获取大小大于 1k 且扩展名为 txt 的文件,我的代码如下:
files=$(find foldername -size +1k -name \*.txt -exec {} \;)
for item in $files
do
echo $item
done
但我得到了意想不到的输出,如下所示。请帮忙 !!!
DEST/sample - Copy - Copy.txt: line 1: Hello: command not found
DEST/sample - Copy - Copy.txt: line 2: This: command not found
DEST/sample - Copy - Copy.txt: line 3: In: command not found
DEST/sample - Copy - Copy.txt: line 4: $'\r': command not found
DEST/sample - Copy - Copy.txt: line 5: User: command not found
DEST/sample - Copy - Copy.txt: line 6: -s: command not found
DEST/sample - Copy - Copy.txt: line 7: -d: command not found
DEST/sample - Copy - Copy.txt: line 8: -t: command not found
DEST/sample - Copy - Copy.txt: line 9: $'\r': command not found
DEST/sample - Copy - Copy.txt: line 10: $'\r': command not found
DEST/sample - Copy - Copy.txt: line 11: Hello: command not found
DEST/sample - Copy - Copy.txt: line 12: This: command not found
DEST/sample - Copy - Copy.txt: line 13: In: command not found
DEST/sample - Copy - Copy.txt: line 14: $'\r': command not found
DEST/sample - Copy - Copy.txt: line 15: User: command not found
DEST/sample - Copy - Copy.txt: line 16: -s: command not found
DEST/sample - Copy - Copy.txt: line 17: -d: command not found
DEST/sample - Copy - Copy.txt: line 18: -t: command not found
DEST/sample - Copy - Copy.txt: line 19: $'\r': command not found
DEST/sample - Copy - Copy.txt: line 20: Hello: command not found
DEST/sample - Copy - Copy.txt: line 21: This: command not found
DEST/sample - Copy - Copy.txt: line 22: In: command not found
DEST/sample - Copy - Copy.txt: line 23: $'\r': command not found
DEST/sample - Copy - Copy.txt: line 24: User: command not found
DEST/sample - Copy - Copy.txt: line 25: -s: command not found
DEST/sample - Copy - Copy.txt: line 26: -d: command not found
DEST/sample - Copy - Copy.txt: line 27: -t: command not found
DEST/sample - Copy.txt: line 1: Hello: command not found
DEST/sample - Copy.txt: line 2: This: command not found
DEST/sample - Copy.txt: line 3: In: command not found
DEST/sample - Copy.txt: line 4: $'\r': command not found
DEST/sample - Copy.txt: line 5: User: command not found
DEST/sample - Copy.txt: line 6: -s: command not found
DEST/sample - Copy.txt: line 7: -d: command not found
DEST/sample - Copy.txt: line 8: -t: command not found
DEST/sample - Copy.txt: line 9: $'\r': command not found
DEST/sample - Copy.txt: line 10: $'\r': command not found
DEST/sample - Copy.txt: line 11: Hello: command not found
DEST/sample - Copy.txt: line 12: This: command not found
DEST/sample - Copy.txt: line 13: In: command not found
DEST/sample - Copy.txt: line 14: $'\r': command not found
DEST/sample - Copy.txt: line 15: User: command not found
DEST/sample - Copy.txt: line 16: -s: command not found
DEST/sample - Copy.txt: line 17: -d: command not found
DEST/sample - Copy.txt: line 18: -t: command not found
DEST/sample - Copy.txt: line 19: $'\r': command not found
DEST/sample - Copy.txt: line 20: Hello: command not found
DEST/sample - Copy.txt: line 21: This: command not found
DEST/sample - Copy.txt: line 22: In: command not found
DEST/sample - Copy.txt: line 23: $'\r': command not found
DEST/sample - Copy.txt: line 24: User: command not found
DEST/sample - Copy.txt: line 25: -s: command not found
DEST/sample - Copy.txt: line 26: -d: command not found
DEST/sample - Copy.txt: line 27: -t: command not found
答案1
明显的问题: find 命令正在执行一个-exec {}
,它尝试执行找到的每个文件(大多数错误消息的来源)。也许你的意思只是-print
,例如,
files=$(find foldername -size +1k -name \*.txt -print)
如果碰巧找到名称中包含空格的文件,您也会在列表中遇到麻烦 - 但一旦正确找到文件,您就会看到这一点。
做这件事有很多种方法;这是对您的脚本进行一点更改的一个:
find foldername -size +1k -name \*.txt -print | \
while IFS= read -r item
do
echo "$item"
done
答案2
假设-exec
就是您想要执行的操作。(该选项-exec
执行文件而不是读取文件)
打印与查找选项匹配的文件名的简单解决方案是:
find foldername -size +1k -name \*.txt -print
如果您需要将名称分配给变量,则需要更多。为了能够处理由命令产生的文件名中的空格find
,该选项-print0
是通常的解决方案:
find foldername -size +1k -name \*.txt -print0
然而,要将结果读入 bash 变量并不容易。
里面有很长的解释这个优秀的格雷格维基页面
#!/bin/bash
unset a
while IFS= read -r -d $'\0' file; do
a+=( "$file" ) # or however you want to process each file
done < <(find foldername -size +1k -name \*.txt -print0)
printf 'filename=%s\n' "${a[@]}"
答案3
使用-exec {} \;
,您可以指示find
将它找到的每个文件作为命令执行。看来您打算打印出文件名,所以说:-print
.但如果文件名包含空格就会中断。解析ls
or的输出通常是一个坏主意find
;仅当您完全确定文件名不可能包含空格或其他特殊字符时才执行此操作。
根据结果采取行动的安全方法find
是触发find
行动。如果您需要使用某些 shell 结构(多个命令、if
变量扩展、管道等),请调用 shell。将文件名作为参数传递给该 shell。
find … -exec sh -c '
echo "$0"
' {} \;
您可以通过为一批文件调用 shell(而不是为每个文件调用一个 shell)来加快速度。
find … -exec sh -c '
for item do
echo "$item"
done
' sh {} +