相关疑难解决方法(0)

将 for 循环与 find 命令一起使用

我想阅读我系统上的所有 java 版本。

for i in 'find / -name java 2>/dev/null' 
do
echo $i checking
$i -version
done
Run Code Online (Sandbox Code Playgroud)

我收到一个错误:

查找:路径必须位于表达式之前:2>/dev/null

用法: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [路径...] [表达式]

问题是什么?

find for

3
推荐指数
1
解决办法
1万
查看次数

Bash:子shell中的转义引号

当我执行以下命令时:

#!/bin/bash
while IFS= read -r -d '' file; do
    files+=$file
done < <(find -type f -name '*.c' -print0)
echo "${files[@]}"
Run Code Online (Sandbox Code Playgroud)

我没有得到与此相同的结果:

#!/bin/bash
find_args="-type f '*.c' -print0"
while IFS= read -r -d '' file; do
    files+=$file
done < <(find $find_args)
echo "${files[@]}"
Run Code Online (Sandbox Code Playgroud)

如何将第二个场景修复为与第一个相同?

我的理解是,因为双引号中有单引号,单引号会被转义,这会产生一个糟糕的扩展,看起来像这样:

find -type f -name ''\''*.c'\'' -print0
Run Code Online (Sandbox Code Playgroud)

bash quoting escape-characters subshell

3
推荐指数
1
解决办法
1868
查看次数

如何在没有替换或解释的情况下将字符串文字存储到变量中?

要扁平化目录结构,我可以这样做:

find . -type f -exec sh -c 'mv "{}" "./`basename "{}"`"'  \;
Run Code Online (Sandbox Code Playgroud)

我想将以下内容作为 $FLATTEN 存储在我的个人资料中

-exec sh -c 'mv "{}" "./`basename "{}"`"'  \;
Run Code Online (Sandbox Code Playgroud)

这样以后我就可以执行 find . $FLATTEN

我在存储变量时遇到问题,因为它过早地被解释。我希望将它存储为字符串文字,并且仅在 shell 上使用时进行解释,而不是在来源时进行解释。

shell variable

3
推荐指数
1
解决办法
1689
查看次数

即使给定了开始搜索的路径,shellcheck 也会警告 find 输出上的循环

Ubuntu 16.04

#!/bin/bash

site="hello"
wDir="/home/websites/${site}/httpdocs/"

for file in $(find "${wDir}" -name "*.css")
do
   echo "$file";
done
exit 0;
Run Code Online (Sandbox Code Playgroud)

即使我定义了开始目录,shellcheck 也会警告我,但脚本工作得很好。

root@me /scripts/ # shellcheck test.sh

In test.sh line 6:
for file in $(find "${wDir}" -name "*.css")
            ^-- SC2044: For loops over find output are fragile. Use find -exec or a while read loop.
Run Code Online (Sandbox Code Playgroud)

bash find shellcheck

3
推荐指数
2
解决办法
1260
查看次数

使用 find -exec 删除文件扩展名

使用时find,如何.pdf从第二对-exec大括号 ( {}) 中删除原始文件扩展名 (即)?


例如:

find ~/Documents -regex 'LOGIC.*\.pdf' -exec pdf2svg {} {}.svg \;
Run Code Online (Sandbox Code Playgroud)

输入文件名:

~/Documents/LOGIC-P_OR_Q.pdf
Run Code Online (Sandbox Code Playgroud)

输出文件名:

~/Documents/LOGIC-P_OR_Q.pdf.svg
Run Code Online (Sandbox Code Playgroud)

所需文件名:

~/Documents/LOGIC-P_OR_Q.svg
Run Code Online (Sandbox Code Playgroud)

command-line find filenames

3
推荐指数
1
解决办法
3278
查看次数

如何对所有匹配的文件使用 sed?

我正在尝试在输出上运行 sedfind | grep \.ext$
但是,sed 需要输入文件和输出文件。
如何将输出行保存到变量,或将其传递给 sed?

pipe sed find

3
推荐指数
1
解决办法
8026
查看次数

为没有扩展名的文件添加文件扩展名

我在各种不同的子目录中有数百个文件。其中一些具有正确的文件扩展名,但其中一些没有。我想重命名所有没有文件扩展名的文件,并在其文件名后附加 .mp4 扩展名。其他文件应该保持不变。如何使用 Bash 自动执行此重命名操作?或者我需要像 Perl 或 Python 这样的真正的脚本语言吗?

bash rename files

2
推荐指数
1
解决办法
1万
查看次数

如何从bash中的文件名列表中删除文件扩展名

我正在尝试从使用以下 bash 脚本生成的文件列表中删除文件扩展名:

 #!/bin/bash
 file_list=$(find . -type f) #assuming that the files are stored in the same directory
 trimmed_file_list=$(printf '%s\n' "${file_list[@]%.*}")
 printf '%s\n' "${trimmed_file_list[@]}"
Run Code Online (Sandbox Code Playgroud)

此扩展从列表中的最后一个条目中删除扩展名,但不会删除任何较早的条目。

例如,我想要以下列表:

 file1.pdf
 file2.pdf
 file3.png
Run Code Online (Sandbox Code Playgroud)

成为

 file1
 file2
 file3
Run Code Online (Sandbox Code Playgroud)

但相反,我得到:

 file1.pdf
 file2.pdf
 file3
Run Code Online (Sandbox Code Playgroud)

我不想在循环中执行此操作,我想使用参数扩展。我想避免剪切,因为我只想删除文件中的最后一个扩展名。

有很多关于参数扩展的主题,似乎 bash 可能会因为find使用换行符而窒息......我真的不确定。如果另一个预先存在的主题解释了这个问题,我不完全理解发生了什么。

似乎相关但似乎无法解决我的问题的主题:

bash find string printf

2
推荐指数
1
解决办法
4833
查看次数

查找所有空目录和具有单个(特定)文件的目录

我正在尝试编写一个 shell 脚本,删除所有空目录 \nas\xc2\xa0well\xc2\xa0 作为仅包含.DS_StoreMac 生成的文件的任何目录。\xc2\xa0\nI\xc2\xa0 可以很容易地完成前者和

\n
find -depth -type d -empty\n
Run Code Online (Sandbox Code Playgroud)\n

但 I\xc2\xa0 无法弄清楚如何查找 .DS_Store包含.

\n

有没有一种简单的方法可以做到这一点,而无需编写自己的递归搜索函数?

\n

find shell-script

2
推荐指数
1
解决办法
1508
查看次数

如何在 bash 函数中正确使用引号?

我试图在我的.bashrc文件中定义以下 bash 函数:

function myfind() {
    find $1 -not -path venv -not -path .tox -name "$2" | xargs grep -n "$3"
}
Run Code Online (Sandbox Code Playgroud)

这没有达到我的预期。当我使用该函数搜索文件中的文本时,例如

myfind . *.py test
Run Code Online (Sandbox Code Playgroud)

它不返回任何内容。但是当我直接使用表达式时,即

find . -not -path venv -not -path .tox -name "*.py" | xargs grep -n "test"
Run Code Online (Sandbox Code Playgroud)

它返回一些匹配项。

我已经尝试使用双引号,例如

function myfind() {
    find $1 -not -path venv -not -path .tox -name '"$2"' | xargs grep -n '"$3"'
}
Run Code Online (Sandbox Code Playgroud)

或者

function myfind() {
    find $1 -not -path venv -not -path .tox -name "'$2'" …
Run Code Online (Sandbox Code Playgroud)

bash quoting

2
推荐指数
1
解决办法
643
查看次数