Change Names of Multiple Files Linux
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6985873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change Names of Multiple Files Linux
提问by Terry Li
I have a number of files with names a1.txt, b1.txt, c1,txt...on ubuntu machine.
I have a number of files with names a1.txt, b1.txt, c1,txt...on ubuntu machine.
Is there any quick way to change all file names to a2.txt, b2.txt, c2.txt...?
Is there any quick way to change all file names to a2.txt, b2.txt, c2.txt...?
In particular, I'd like to replace part of the name string. For instance, every file name contains a string called "apple" and I want to replace "apple" with "pear" in all file names.
In particular, I'd like to replace part of the name string. For instance, every file name contains a string called "apple" and I want to replace "apple" with "pear" in all file names.
Any command or script?
Any command or script?
采纳答案by Micha? ?rajer
without any extra software you can:
without any extra software you can:
for FILE in *1.txt; do mv "$FILE" $(echo "$FILE" | sed 's/1/2/'); done
回答by Rob?
ls *1.txt | perl -ne 'chomp; $x = $_; $x =~ s/1/2/; rename $_, $x;'
回答by dogbane
The following command will rename the specified files by replacing the first occurrence of 1
in their name by 2
:
The following command will rename the specified files by replacing the first occurrence of 1
in their name by 2
:
rename 1 2 *1.txt
回答by Louis Marascio
Something like this should work:
Something like this should work:
for i in *1.txt; do
name=$(echo $i | cut -b1)
mv $i ${name}2.txt
done
Modify to suit your needs.
Modify to suit your needs.
回答by user unknown
for f in {a..c}1.txt; do echo "$f" "${f/1/2}"; done
replace 'echo' with 'mv' if the output looks correct.
replace 'echo' with 'mv' if the output looks correct.
and I want to replace "apple" with "linux"
and I want to replace "apple" with "linux"
for f in *apple*; do mv "$f" "${f/apple/linux}"; done
The curly brackets in line 1 should work with bash at least.
The curly brackets in line 1 should work with bash at least.
回答by ZZZ
Here's another option that worked for me (following the examples above) for files in different subdirectories
Here's another option that worked for me (following the examples above) for files in different subdirectories
for FILE in $(find . -name *1.txt); do mv "$FILE" "${FILE/1/2}"; done;