I use this command to find files with a given pattern and then rename them to something else
我使用这个命令来查找具有给定模式的文件,然后将它们重命名为其他内容。
find . -name '*-GHBAG-*' -exec bash -c 'echo mv $0 ${0/GHBAG/stream-agg}' {} \;
As I run this command, I see some outputs like this
当我运行这个命令时,我看到一些像这样的输出
mv ./report-GHBAG-1B ./report-stream-agg-1B
mv ./reoprt-GHBAG-0.5B ./report-stream-agg-0.5B
However at the end, when I run ls
, I see the old file names.
但是在最后,当我运行ls时,我看到了旧的文件名。
4 个解决方案
#1
80
You are echo'ing your 'mv' command, not actually executing it. Change to:
你是在重复你的“mv”命令,而不是实际执行它。改变:
find . -name '*-GHBAG-*' -exec bash -c 'mv $0 ${0/GHBAG/stream-agg}' {} \;
#2
35
I would suggest using the rename
command to perform this task. rename
renames the filenames supplied according to the rule specified as a Perl regular expression.
我建议使用rename命令执行此任务。重命名根据Perl正则表达式指定的规则提供的文件名。
In this case, you could use:
在这种情况下,您可以使用:
rename 's/GHBAG/stream-agg/' *-GHBAG-*
#3
17
In reply to anumi's comment, you could in effect search recursively down directories by matching '**':
在回应安尼弥的评论时,你可以通过匹配“**”来递归地搜索目录:
rename 's/GHBAG/stream-agg/' **/*-GHBAG-*
#4
1
This works for my needs, replacing all matching files or file types. Be warned, this is a very greedy search
这适用于我的需要,替换所有匹配的文件或文件类型。注意,这是一个非常贪婪的搜索
# bashrc
function file_replace() {
for file in $(find . -type f -name "$1*"); do
mv $file $(echo "$file" | sed "s/$1/$2/");
done
}
I will usually run with find . -type f -name "MYSTRING*"
in advance to check the matches out before replacing.
我通常会用find来运行。-键入f -name“MYSTRING*”,在替换之前检查匹配项。
For example:
例如:
file_replace "Slider.js" "RangeSlider.ts"
renamed: packages/react-ui-core/src/Form/Slider.js -> packages/react-ui-core/src/Form/RangeSlider.ts
renamed: stories/examples/Slider.js -> stories/examples/RangeSlider.ts
or ditch the filetype to make it even greedier
或者抛弃文件类型,使其更加贪婪
file_replace Slider RangeSlider
renamed: packages/react-ui-core/src/Form/Slider.js -> packages/react-ui-core/src/Form/RangeSlider.js
renamed: stories/examples/Slider.js -> stories/examples/RangeSlider.js
renamed: stories/theme/Slider.css -> stories/theme/RangeSlider.css
#1
80
You are echo'ing your 'mv' command, not actually executing it. Change to:
你是在重复你的“mv”命令,而不是实际执行它。改变:
find . -name '*-GHBAG-*' -exec bash -c 'mv $0 ${0/GHBAG/stream-agg}' {} \;
#2
35
I would suggest using the rename
command to perform this task. rename
renames the filenames supplied according to the rule specified as a Perl regular expression.
我建议使用rename命令执行此任务。重命名根据Perl正则表达式指定的规则提供的文件名。
In this case, you could use:
在这种情况下,您可以使用:
rename 's/GHBAG/stream-agg/' *-GHBAG-*
#3
17
In reply to anumi's comment, you could in effect search recursively down directories by matching '**':
在回应安尼弥的评论时,你可以通过匹配“**”来递归地搜索目录:
rename 's/GHBAG/stream-agg/' **/*-GHBAG-*
#4
1
This works for my needs, replacing all matching files or file types. Be warned, this is a very greedy search
这适用于我的需要,替换所有匹配的文件或文件类型。注意,这是一个非常贪婪的搜索
# bashrc
function file_replace() {
for file in $(find . -type f -name "$1*"); do
mv $file $(echo "$file" | sed "s/$1/$2/");
done
}
I will usually run with find . -type f -name "MYSTRING*"
in advance to check the matches out before replacing.
我通常会用find来运行。-键入f -name“MYSTRING*”,在替换之前检查匹配项。
For example:
例如:
file_replace "Slider.js" "RangeSlider.ts"
renamed: packages/react-ui-core/src/Form/Slider.js -> packages/react-ui-core/src/Form/RangeSlider.ts
renamed: stories/examples/Slider.js -> stories/examples/RangeSlider.ts
or ditch the filetype to make it even greedier
或者抛弃文件类型,使其更加贪婪
file_replace Slider RangeSlider
renamed: packages/react-ui-core/src/Form/Slider.js -> packages/react-ui-core/src/Form/RangeSlider.js
renamed: stories/examples/Slider.js -> stories/examples/RangeSlider.js
renamed: stories/theme/Slider.css -> stories/theme/RangeSlider.css