我在目录中有很多tiff
个文件,必须重命名,但是有数百个,所以它很麻烦。文件看起来像这样:
basefilename_0002_-0.0.mrc
basefilename_0003_3.0.mrc
basefilename_0004_-3.0.mrc
basefilename_0005_-6.0.mrc
basefilename_0006_6.0.mrc
etc...
我需要做的就是更改文件名的中间部分,以便将前41个.mrc
文件更改为:
basefilename_0001_-0.0.mrc
basefilename_0001_3.0.mrc
basefilename_0001_-3.0.mrc
basefilename_0001_-6.0.mrc
basefilename_0001_6.0.mrc
etc.
和第二批41 .mrc
个文件:
basefilename_0043_-0.0.mrc
basefilename_0044_3.0.mrc
basefilename_0045_-3.0.mrc
basefilename_0046_-6.0.mrc
basefilename_0047_6.0.mrc
将重命名为
basefilename_0002_-0.0.mrc
basefilename_0002_3.0.mrc
basefilename_0002_-3.0.mrc
basefilename_0002_-6.0.mrc
basefilename_0002_6.0.mrc
etc.
所以基本上我必须在"basefilename_"
之后和下一个"_"
之前进行解析并重命名,以便数字不是上升而只是0001.但是我有数百个,我需要确保每个mrc
个文件在basefile
名称之后和下一个描述之前都是相同的数字。
答案 0 :(得分:1)
实际上,您可以使用bash本身提供的本机工具执行所需的操作,而无需依赖任何需要生成单独的子shell的单独实用程序。 Bash提供了参数扩展和子字符串替换,您可以使用它用所需的新文本替换_????_
之间的文本(例如0001
,...)。
Bash还提供了printf -v var
,使您可以灵活地man 3 print
进行格式化,同时可以将格式化后的输出保存在var
中。因此,例如,如果我有一个值1
,我想将其格式化为0001
并将其存储在变量blkno
中,这很简单,就是printf -v "%04d" '1'
。 / p>
将其与一个简单的计数器结合,然后使用提供的bash modulo
运算符,您可以执行以下操作:
#!/bin/bash
ext=${1:-mrc} ## extension of files to select
declare -i blksz=${2:-41} cnt=0 blk=1 ## files per-block, counters
printf -v blkno "%04d" $blk ## format 1st blk as 0001
for i in *.$ext; do ## loop over each file with extension
## test output showing what would be moved, to new name
printf "mv %-28s %s\n" "$i" "${i/_*_/_${blkno}_}"
## mv "$i" "${i/_*_/_${blkno}_}" ## (uncomment for actual move)
(((cnt+1) % blksz == 0)) && { ## check if blksz output
((blk++)) ## increment blk number
printf -v blkno "%04d" $blk ## format as 4-digit w/leading zeros
}
((cnt++)) ## increment count
done
请注意,脚本以循环扩展的文件扩展名(默认为.mrc
)和默认情况下每个块41
中要包含的文件数为第一个参数。
示例输入文件
我没有您的确切文件,因此我生成了类似的内容,例如带有循环和touch
basefilename_0002_-0.0.mrc
basefilename_0003_3.0.mrc
basefilename_0004_-3.0.mrc
basefilename_0005_6.0.mrc
basefilename_0006_-6.0.mrc
basefilename_0007_9.0.mrc
basefilename_0008_-9.0.mrc
basefilename_0009_12.0.mrc
basefilename_0010_-12.0.mrc
basefilename_0011_15.0.mrc
basefilename_0012_-15.0.mrc
basefilename_0013_18.0.mrc
basefilename_0014_-18.0.mrc
basefilename_0015_21.0.mrc
basefilename_0016_-21.0.mrc
basefilename_0017_24.0.mrc
basefilename_0018_-24.0.mrc
basefilename_0019_27.0.mrc
basefilename_0020_-27.0.mrc
basefilename_0021_30.0.mrc
basefilename_0022_-30.0.mrc
basefilename_0023_33.0.mrc
basefilename_0024_-33.0.mrc
basefilename_0025_36.0.mrc
basefilename_0026_-36.0.mrc
basefilename_0027_39.0.mrc
basefilename_0028_-39.0.mrc
basefilename_0029_42.0.mrc
basefilename_0030_-42.0.mrc
basefilename_0031_45.0.mrc
basefilename_0032_-45.0.mrc
basefilename_0033_48.0.mrc
basefilename_0034_-48.0.mrc
basefilename_0035_51.0.mrc
basefilename_0036_-51.0.mrc
basefilename_0037_54.0.mrc
basefilename_0038_-54.0.mrc
basefilename_0039_57.0.mrc
basefilename_0040_-57.0.mrc
basefilename_0041_60.0.mrc
basefilename_0042_-60.0.mrc
basefilename_0043_0.0.mrc
basefilename_0044_-0.0.mrc
basefilename_0045_3.0.mrc
basefilename_0046_-3.0.mrc
basefilename_0047_6.0.mrc
basefilename_0048_-6.0.mrc
basefilename_0049_9.0.mrc
basefilename_0050_-9.0.mrc
basefilename_0051_12.0.mrc
basefilename_0052_-12.0.mrc
basefilename_0053_15.0.mrc
basefilename_0054_-15.0.mrc
basefilename_0055_18.0.mrc
basefilename_0056_-18.0.mrc
basefilename_0057_21.0.mrc
basefilename_0058_-21.0.mrc
basefilename_0059_24.0.mrc
basefilename_0060_-24.0.mrc
basefilename_0061_27.0.mrc
basefilename_0062_-27.0.mrc
basefilename_0063_30.0.mrc
basefilename_0064_-30.0.mrc
basefilename_0065_33.0.mrc
basefilename_0066_-33.0.mrc
basefilename_0067_36.0.mrc
basefilename_0068_-36.0.mrc
basefilename_0069_39.0.mrc
basefilename_0070_-39.0.mrc
basefilename_0071_42.0.mrc
basefilename_0072_-42.0.mrc
basefilename_0073_45.0.mrc
basefilename_0074_-45.0.mrc
basefilename_0075_48.0.mrc
basefilename_0076_-48.0.mrc
basefilename_0077_51.0.mrc
basefilename_0078_-51.0.mrc
basefilename_0079_54.0.mrc
basefilename_0080_-54.0.mrc
basefilename_0081_57.0.mrc
basefilename_0082_-57.0.mrc
basefilename_0083_60.0.mrc
basefilename_0084_-60.0.mrc
basefilename_0085_0.0.mrc
basefilename_0086_-0.0.mrc
basefilename_0087_3.0.mrc
basefilename_0088_-3.0.mrc
basefilename_0089_6.0.mrc
basefilename_0090_-6.0.mrc
basefilename_0091_9.0.mrc
basefilename_0092_-9.0.mrc
basefilename_0093_12.0.mrc
basefilename_0094_-12.0.mrc
basefilename_0095_15.0.mrc
basefilename_0096_-15.0.mrc
basefilename_0097_18.0.mrc
basefilename_0098_-18.0.mrc
basefilename_0099_21.0.mrc
basefilename_0100_-21.0.mrc
使用/输出示例
注释:注释了实际移动mv
行,以允许您测试脚本并根据需要进行调整,然后执行实际移动。如果满意,请取消注释以mv
开头的行。
脚本输出已移动的文件,文件的原始文件名和新文件名,例如
mv basefilename_0002_-0.0.mrc basefilename_0001_-0.0.mrc
mv basefilename_0003_3.0.mrc basefilename_0001_3.0.mrc
mv basefilename_0004_-3.0.mrc basefilename_0001_-3.0.mrc
mv basefilename_0005_6.0.mrc basefilename_0001_6.0.mrc
mv basefilename_0006_-6.0.mrc basefilename_0001_-6.0.mrc
mv basefilename_0007_9.0.mrc basefilename_0001_9.0.mrc
mv basefilename_0008_-9.0.mrc basefilename_0001_-9.0.mrc
mv basefilename_0009_12.0.mrc basefilename_0001_12.0.mrc
mv basefilename_0010_-12.0.mrc basefilename_0001_-12.0.mrc
mv basefilename_0011_15.0.mrc basefilename_0001_15.0.mrc
mv basefilename_0012_-15.0.mrc basefilename_0001_-15.0.mrc
mv basefilename_0013_18.0.mrc basefilename_0001_18.0.mrc
mv basefilename_0014_-18.0.mrc basefilename_0001_-18.0.mrc
mv basefilename_0015_21.0.mrc basefilename_0001_21.0.mrc
mv basefilename_0016_-21.0.mrc basefilename_0001_-21.0.mrc
mv basefilename_0017_24.0.mrc basefilename_0001_24.0.mrc
mv basefilename_0018_-24.0.mrc basefilename_0001_-24.0.mrc
mv basefilename_0019_27.0.mrc basefilename_0001_27.0.mrc
mv basefilename_0020_-27.0.mrc basefilename_0001_-27.0.mrc
mv basefilename_0021_30.0.mrc basefilename_0001_30.0.mrc
mv basefilename_0022_-30.0.mrc basefilename_0001_-30.0.mrc
mv basefilename_0023_33.0.mrc basefilename_0001_33.0.mrc
mv basefilename_0024_-33.0.mrc basefilename_0001_-33.0.mrc
mv basefilename_0025_36.0.mrc basefilename_0001_36.0.mrc
mv basefilename_0026_-36.0.mrc basefilename_0001_-36.0.mrc
mv basefilename_0027_39.0.mrc basefilename_0001_39.0.mrc
mv basefilename_0028_-39.0.mrc basefilename_0001_-39.0.mrc
mv basefilename_0029_42.0.mrc basefilename_0001_42.0.mrc
mv basefilename_0030_-42.0.mrc basefilename_0001_-42.0.mrc
mv basefilename_0031_45.0.mrc basefilename_0001_45.0.mrc
mv basefilename_0032_-45.0.mrc basefilename_0001_-45.0.mrc
mv basefilename_0033_48.0.mrc basefilename_0001_48.0.mrc
mv basefilename_0034_-48.0.mrc basefilename_0001_-48.0.mrc
mv basefilename_0035_51.0.mrc basefilename_0001_51.0.mrc
mv basefilename_0036_-51.0.mrc basefilename_0001_-51.0.mrc
mv basefilename_0037_54.0.mrc basefilename_0001_54.0.mrc
mv basefilename_0038_-54.0.mrc basefilename_0001_-54.0.mrc
mv basefilename_0039_57.0.mrc basefilename_0001_57.0.mrc
mv basefilename_0040_-57.0.mrc basefilename_0001_-57.0.mrc
mv basefilename_0041_60.0.mrc basefilename_0001_60.0.mrc
mv basefilename_0042_-60.0.mrc basefilename_0001_-60.0.mrc
mv basefilename_0043_0.0.mrc basefilename_0002_0.0.mrc
mv basefilename_0044_-0.0.mrc basefilename_0002_-0.0.mrc
mv basefilename_0045_3.0.mrc basefilename_0002_3.0.mrc
mv basefilename_0046_-3.0.mrc basefilename_0002_-3.0.mrc
mv basefilename_0047_6.0.mrc basefilename_0002_6.0.mrc
mv basefilename_0048_-6.0.mrc basefilename_0002_-6.0.mrc
mv basefilename_0049_9.0.mrc basefilename_0002_9.0.mrc
mv basefilename_0050_-9.0.mrc basefilename_0002_-9.0.mrc
mv basefilename_0051_12.0.mrc basefilename_0002_12.0.mrc
mv basefilename_0052_-12.0.mrc basefilename_0002_-12.0.mrc
mv basefilename_0053_15.0.mrc basefilename_0002_15.0.mrc
mv basefilename_0054_-15.0.mrc basefilename_0002_-15.0.mrc
mv basefilename_0055_18.0.mrc basefilename_0002_18.0.mrc
mv basefilename_0056_-18.0.mrc basefilename_0002_-18.0.mrc
mv basefilename_0057_21.0.mrc basefilename_0002_21.0.mrc
mv basefilename_0058_-21.0.mrc basefilename_0002_-21.0.mrc
mv basefilename_0059_24.0.mrc basefilename_0002_24.0.mrc
mv basefilename_0060_-24.0.mrc basefilename_0002_-24.0.mrc
mv basefilename_0061_27.0.mrc basefilename_0002_27.0.mrc
mv basefilename_0062_-27.0.mrc basefilename_0002_-27.0.mrc
mv basefilename_0063_30.0.mrc basefilename_0002_30.0.mrc
mv basefilename_0064_-30.0.mrc basefilename_0002_-30.0.mrc
mv basefilename_0065_33.0.mrc basefilename_0002_33.0.mrc
mv basefilename_0066_-33.0.mrc basefilename_0002_-33.0.mrc
mv basefilename_0067_36.0.mrc basefilename_0002_36.0.mrc
mv basefilename_0068_-36.0.mrc basefilename_0002_-36.0.mrc
mv basefilename_0069_39.0.mrc basefilename_0002_39.0.mrc
mv basefilename_0070_-39.0.mrc basefilename_0002_-39.0.mrc
mv basefilename_0071_42.0.mrc basefilename_0002_42.0.mrc
mv basefilename_0072_-42.0.mrc basefilename_0002_-42.0.mrc
mv basefilename_0073_45.0.mrc basefilename_0002_45.0.mrc
mv basefilename_0074_-45.0.mrc basefilename_0002_-45.0.mrc
mv basefilename_0075_48.0.mrc basefilename_0002_48.0.mrc
mv basefilename_0076_-48.0.mrc basefilename_0002_-48.0.mrc
mv basefilename_0077_51.0.mrc basefilename_0002_51.0.mrc
mv basefilename_0078_-51.0.mrc basefilename_0002_-51.0.mrc
mv basefilename_0079_54.0.mrc basefilename_0002_54.0.mrc
mv basefilename_0080_-54.0.mrc basefilename_0002_-54.0.mrc
mv basefilename_0081_57.0.mrc basefilename_0002_57.0.mrc
mv basefilename_0082_-57.0.mrc basefilename_0002_-57.0.mrc
mv basefilename_0083_60.0.mrc basefilename_0002_60.0.mrc
mv basefilename_0084_-60.0.mrc basefilename_0003_-60.0.mrc
mv basefilename_0085_0.0.mrc basefilename_0003_0.0.mrc
mv basefilename_0086_-0.0.mrc basefilename_0003_-0.0.mrc
mv basefilename_0087_3.0.mrc basefilename_0003_3.0.mrc
mv basefilename_0088_-3.0.mrc basefilename_0003_-3.0.mrc
mv basefilename_0089_6.0.mrc basefilename_0003_6.0.mrc
mv basefilename_0090_-6.0.mrc basefilename_0003_-6.0.mrc
mv basefilename_0091_9.0.mrc basefilename_0003_9.0.mrc
mv basefilename_0092_-9.0.mrc basefilename_0003_-9.0.mrc
mv basefilename_0093_12.0.mrc basefilename_0003_12.0.mrc
mv basefilename_0094_-12.0.mrc basefilename_0003_-12.0.mrc
mv basefilename_0095_15.0.mrc basefilename_0003_15.0.mrc
mv basefilename_0096_-15.0.mrc basefilename_0003_-15.0.mrc
mv basefilename_0097_18.0.mrc basefilename_0003_18.0.mrc
mv basefilename_0098_-18.0.mrc basefilename_0003_-18.0.mrc
mv basefilename_0099_21.0.mrc basefilename_0003_21.0.mrc
mv basefilename_0100_-21.0.mrc basefilename_0003_-21.0.mrc
仔细检查一下,如果还有其他问题,请告诉我。
答案 1 :(得分:0)
我确信有比下面的shell脚本更好的方法来获得你想要的东西,但是假设文件按需要排序,那么类似下面的东西应该有效:
event
上面的#!/bin/bash
set -e
count=1
index=1
for p in *.mrc; do
if expr $count == 42 > /dev/null; then
index=`expr $index + 1`
count=1
else
count=`expr $count + 1`
fi
mv $p `echo $p | sed -e "s/\(.*_\)\([0-9]*\)\(_.*\)/\1000${index}\3/"`
done
命令会将文件名sed
分成三个在转义的括号对$p
之间找到的部分:
\( ... \)
),保留在foo_
,\1
,其中000${index}
扩展为${index}
第一组41个文件,1
第二组,等等。 ,和2
),保留在_3.0.mrc
这不是一个非常强大的实现,因为如果\3
变得大于00023
,你最终会得到像$index
这样的中心数字,但我认为你可以实现自己的实现。
您也可以使用Bash字符串操作内置代替9
。请参阅Section 10.1 Manipulating Strings中的Advanced Bash-Scripting Guide。