简体   繁体   English

重命名bash脚本中的一堆文件

[英]Rename bunch of files in bash script

I have some files with names which contains date, but this date part is in wrong format, and I need to change it. 我有一些名称包含日期的文件,但是此日期部分的格式错误,我需要更改它。 Example files: 示例文件:

foo-1-21-01-16.tar
foo-1-22-01-16.tar
foobar-1-21-01-16.tar
foobar-1-22-01-16.tar

Result that I want to have should look like this: 我想要的结果应如下所示:

foo-1-16-01-21.tar
foo-1-16-01-22.tar
foobar-1-16-01-21.tar
foobar-1-16-01-22.tar

So, what I want to do is to change place of "day" part with place of "year" part. 因此,我想做的是将“天”部分的位置更改为“年”部分的位置。 But I can't figure out how to do this. 但是我不知道该怎么做。 There is more than a few files, so I can't do this manually. 文件不止几个,因此我无法手动执行此操作。

You would suggest you use AWK. 您建议您使用AWK。 Example: 例:

  echo "foo-1-21-01-16.tar" | awk -F'[-.]' '{printf "bar-%s-%s-%s-%s.%s", $2, $5, $4, $3, $6 }'

will result in bar-1-16-01-21.tar . 将导致bar-1-16-01-21.tar The -F parameter tells awk to split each line of input on - or . -F参数告诉awk将输入的每一行分割为-. . After the split, $1 = foo , $5 = year , etc. use print to recompose a filename with each field in another order. 分割后, $1 = foo$5 = year ,等等。使用print来以另一个顺序重新组成每个字段的文件名。

From a bash script , iterate through your files, use the above command to create the new filename, then use mv to rename the file. bash脚本中 ,遍历文件,使用上述命令创建新文件名,然后使用mv重命名文件。 Tell me if you need a code example. 告诉我您是否需要代码示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM