如何在bash中重命名目录中的文件组? 例如 : 我有一组文件:
> 0001.txt
> 0002.txt
> 0003.txt
> 0004.txt
...
我需要0001.txt成为0002.txt; 0002.txt成为0003.txt等 结果应该如此:
0002.txt
0003.txt
0004.txt
0005.txt
...
答案 0 :(得分:1)
您可以使用以下简单的脚本: -
spark-submit
在行动中看到这一点
#!/bin/bash
while IFS= read -r -d '' file; do
filename=$(basename "$file") # Get the absolute path of the file
filename=${filename%.*} # Getting file-name without the extension part 'tet0002', 'tet0001'
filename=${filename:3} # Getting the numerical part '002', '001'
# To preserve the leading pad '0's, retaining the decimal representation
# using printf and appending '10#' notation. '-v' for verbose only (can
# be removed)
mv -v "$file" tet"$(printf %04d "$((10#$filename + 1))")".txt
done < <(find . -maxdepth 1 -mindepth 1 -name "tet*.txt" -type f -print0)
答案 1 :(得分:1)
如果你的文件名遵循给定的模式,你可以这样做:
for file in `ls | egrep '^[[:digit:]]+.txt$' | sort -r`
do
mv $file `printf %04d $(expr ${file%.*} + 1)`.txt
done
修改强>
对于前缀为tet
的文件名,您可以像这样修改上面的脚本:
for file in `ls | egrep '^tet[[:digit:]]+.txt$' | sort -r`
do
filename=${file%.*}
mv $file tet`printf %04d $(expr ${filename:3} + 1)`.txt
done
只是出于好奇,如果一些bash专家知道一种避免临时变量的方法,我将不胜感激filename