bash 在文件名中间添加字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18158345/
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
add character to the middle of a file name
提问by steve-er-rino
I need to add a character to the middle of a file name, I need to do this for about 8000 files all in the same directory. I'm looking for a command-line option:
我需要在文件名的中间添加一个字符,我需要对同一目录中的大约 8000 个文件执行此操作。我正在寻找一个命令行选项:
Example (all files that need renaming are five digits, they are in a directory that contains other six-digit filenames that do not need to be renamed):
示例(所有需要重命名的文件都是五位数,它们所在的目录包含其他不需要重命名的六位数文件名):
01011
02022
12193
To:
到:
010101
020202
121903
I've tried a couple of things: rename, mv, etc. Similar to this (Bash - Adding 0's in the middle of a file name) but not exactly
我尝试了几件事:重命名、mv 等。与此类似(Bash - 在文件名中间添加 0)但不完全相同
回答by ruakh
You can write:
你可以写:
for file in * ; do
mv ./"$file" "${file:0:4}0${file:4}"
done
(See the explanation of ${parameter:offset}
and ${parameter:offset:length}
in §3.5.3 "Shell Parameter Expansion" of the Bash Reference Manual.)
(请参阅Bash 参考手册的§3.5.3 “Shell Parameter Expansion”和中的解释。)${parameter:offset}
${parameter:offset:length}
Edited to add:If you only want to capture a specific subset of files, you can change *
to a more-specific pattern such as [0-9][0-9][0-9][0-9][0-9]
(which matches filenames consisting of five digits).
编辑添加:如果您只想捕获特定的文件子集,您可以更改*
为更具体的模式,例如[0-9][0-9][0-9][0-9][0-9]
(匹配由五位数字组成的文件名)。
Incidentally, you can format the whole thing on one line:
顺便说一句,您可以在一行中格式化整个内容:
for file in [0-9][0-9][0-9][0-9][0-9] ; do mv "$file" "${file:0:4}0${file:4}" ; done
回答by edu
Try using below code.
尝试使用以下代码。
rename -v -n "s/(S01S0[0-2][0-9])/$1-/" *
-n
to check the command output.
-n
检查命令输出。
回答by Roger Barreto
For a Windows prompt environment suposing that all the files start with the same name you could use rename.
对于假设所有文件都以相同名称开头的 Windows 提示环境,您可以使用重命名。
rename 0101* 01010*
For a delimited size you could use
对于分隔大小,您可以使用
rename ????* ????0*
Real example
真实例子
Pasta de C:\temp
10/08/2013 00:37 <DIR> .
10/08/2013 00:37 <DIR> ..
10/08/2013 00:37 0 teste.txt
1 arquivo(s) 0 bytes
2 pasta(s) 846.577.762.304 bytes disponíveis
C:\temp>rename ???* ???0*
C:\temp>dir
O volume na unidade C nao tem nome.
O Número de Série do Volume é 7868-7679
Pasta de C:\temp
10/08/2013 00:38 <DIR> .
10/08/2013 00:38 <DIR> ..
10/08/2013 00:37 0 tes0e.txt
1 arquivo(s) 0 bytes
2 pasta(s) 846.577.762.304 bytes disponíveis
回答by jrd1
How about:
怎么样:
for file in * ; do mv "$file" "${file:0:4}0${file:4:1}" ; done