gpt4 book ai didi

bash - OSX 命令 find -type f -exec file {}\: not working

转载 作者:行者123 更新时间:2023-11-29 09:25:27 25 4
gpt4 key购买 nike

我正在尝试编写没有 DOS 行结尾的更改文件的脚本,以将它们更改为具有 DOS 行结尾,但我不知道如何找到不包含 DOS 行结尾的文件。

我有以下适用于 linux 的命令

$ find -type f -exec file {} \; | grep -v CRLF 

它工作并返回

./test/test.bat: ASCII text

但是,在 OSX 上我得到了

$ find -type f -exec file {} \; | grep -v CRLF
find: illegal option -- t
usage: find [-H | -L | -P] [-EXdsx] [-f path] path ... [expression]
find [-H | -L | -P] [-EXdsx] -f path [path ...] [expression]

我也试过

$ find . -type f -exec file {} \; | grep -v CRLF
cannot open `' (No such file or directory)
cannot open `' (No such file or directory)
cannot open `' (No such file or directory)
cannot open `' (No such file or directory)
... etc

OSX 上的 find 是否应该使用不同的语法来完成此操作?

最佳答案

在 MacOS X 上使用的 BSD find 需要在表达式之前提供路径(您的 -type f 是该表达式的一部分);因此它的用法消息将 path 放在 [brackets] 之外,这表明某些东西是可选的。


使用 -- 防止文件名被视为选项,并显式传递 . 作为要搜索的位置。

find . -type f -exec file -- '{}' + | grep -v CRLF

...已在 MacOS 10.11.6 上明确测试。


为了更好地追踪错误,请考虑以下事项:

find . -type f -exec bash -c '
for result; do
file "$result" 2>/dev/null || {
printf "Unable to run file against %q\n" "$result"
}
done
' _ {} +

关于bash - OSX 命令 find -type f -exec file {}\: not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39581456/

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