问题描述
我要搜索具有特定名称的文件,将该名称修改为完整路径,然后将结果复制到另一个文件夹.
I'm looking to search for files of a specific name, modify the name to the full path and then copy the results to another folder.
是否可以使用完整路径作为文件名来更新每个查找结果;即
Is it possible to update each find result with the full path as the file name; i.e.
./folder/subfolder/my-file.csv
成为
folder_subfolder_my-file.csv
我正在使用以下内容列出文件,并希望编写脚本.
I am listing the files using the following and would like to script it.
find . -name my-file.csv -exec ls {} \;
推荐答案
由于您正在使用bash,因此可以利用 globstar
并使用 for
循环:
Since you're using bash, you can take advantage of globstar
and use a for
loop:
shopt -s globstar # set globstar option
for csv in **/my-file.csv; do
echo "$csv" "${csv//\//_}"
done
shopt -u globstar # unset the option if you don't want it any more
启用 globstar
后, **
进行递归搜索(类似于 find
的基本功能).
With globstar
enabled, **
does a recursive search (similar to the basic functionality of find
).
"$ {csv//\//_}"
是 $ {var//match/replace}
的示例,它对所有 match
(此处是转义的/
)与 replace
的实例.
"${csv//\//_}"
is an example of ${var//match/replace}
, which does a global replacement of all instances of match
(here an escaped /
) with replace
.
如果您对输出感到满意,则将 echo
更改为 mv
.
If you're happy with the output, then change the echo
to mv
.
这篇关于递归查找文件并根据其完整路径重命名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!