Shell脚本,用于根据父文件夹名称重命名文件

时间:2014-06-12 22:34:41

标签: macos bash shell unix

我开始使用Automator Droplet,但是对于大量文件来说它有点过于笨重。我认为shell脚本可能会更好用,但我不确定如何继续...

我想要的是重命名大量文件,用父文件夹名替换部分文件名,同时保留文件的序列化及其文件扩展名。

这是文件夹/文件结构以及我想要转换为

的内容
/Path/to/Folder/XYZ_123_028_V001_F01A_9G
|____XYZ_123_028_V001.000000.abc
|____XYZ_123_028_V001.000001.abc
|____XYZ_123_028_V001.000002.abc
...
|____XYZ_123_028_V001.000099.abc
|____XYZ_123_028_V001.000100.abc

/Path/to/Folder/XYZ_123_028_V001_F01A_9G
|____XYZ_123_028_V001_F01A_9G.000000.abc
|____XYZ_123_028_V001_F01A_9G.000001.abc
|____XYZ_123_028_V001_F01A_9G.000002.abc
...
|____XYZ_123_028_V001_F01A_9G.000099.abc
|____XYZ_123_028_V001_F01A_9G.000100.abc

由于

1 个答案:

答案 0 :(得分:0)

[jenkins@sb12 XYZ_123_028_V001_F01A_9G]$ pwd
/jenkins/do_no_delete_AKS_data/XYZ_123_028_V001_F01A_9G
[jenkins@sb12 XYZ_123_028_V001_F01A_9G]$ for i in {1..10}; do touch XYZ_123_028_V001.00000$i.abc ; done
[jenkins@sb12 XYZ_123_028_V001_F01A_9G]$ ls -l
total 0
-rw-r--r-- 1 jenkins dmadmin 0 Jun 12 18:55 XYZ_123_028_V001.0000010.abc
-rw-r--r-- 1 jenkins dmadmin 0 Jun 12 18:55 XYZ_123_028_V001.000001.abc
-rw-r--r-- 1 jenkins dmadmin 0 Jun 12 18:55 XYZ_123_028_V001.000002.abc
-rw-r--r-- 1 jenkins dmadmin 0 Jun 12 18:55 XYZ_123_028_V001.000003.abc
-rw-r--r-- 1 jenkins dmadmin 0 Jun 12 18:55 XYZ_123_028_V001.000004.abc
-rw-r--r-- 1 jenkins dmadmin 0 Jun 12 18:55 XYZ_123_028_V001.000005.abc
-rw-r--r-- 1 jenkins dmadmin 0 Jun 12 18:55 XYZ_123_028_V001.000006.abc
-rw-r--r-- 1 jenkins dmadmin 0 Jun 12 18:55 XYZ_123_028_V001.000007.abc
-rw-r--r-- 1 jenkins dmadmin 0 Jun 12 18:55 XYZ_123_028_V001.000008.abc
-rw-r--r-- 1 jenkins dmadmin 0 Jun 12 18:55 XYZ_123_028_V001.000009.abc
[jenkins@sb12 XYZ_123_028_V001_F01A_9G]$
[jenkins@sb12 XYZ_123_028_V001_F01A_9G]$ dir=$(basename `pwd`); grab=${dir##*_V001}; for f in `ls `; do echo -e "\nFilename = $f"; echo -e "Moving $f -to- ${f%%.*}$grab.${f#*.}\n"; mv $f "${f%%.*}$grab.${f#*.}"; done
Filename = XYZ_123_028_V001.0000010.abc
Moving XYZ_123_028_V001.0000010.abc -to- XYZ_123_028_V001_F01A_9G.0000010.abc

Filename = XYZ_123_028_V001.000001.abc
Moving XYZ_123_028_V001.000001.abc -to- XYZ_123_028_V001_F01A_9G.000001.abc


Filename = XYZ_123_028_V001.000002.abc
Moving XYZ_123_028_V001.000002.abc -to- XYZ_123_028_V001_F01A_9G.000002.abc


Filename = XYZ_123_028_V001.000003.abc
Moving XYZ_123_028_V001.000003.abc -to- XYZ_123_028_V001_F01A_9G.000003.abc


Filename = XYZ_123_028_V001.000004.abc
Moving XYZ_123_028_V001.000004.abc -to- XYZ_123_028_V001_F01A_9G.000004.abc


Filename = XYZ_123_028_V001.000005.abc
Moving XYZ_123_028_V001.000005.abc -to- XYZ_123_028_V001_F01A_9G.000005.abc


Filename = XYZ_123_028_V001.000006.abc
Moving XYZ_123_028_V001.000006.abc -to- XYZ_123_028_V001_F01A_9G.000006.abc


Filename = XYZ_123_028_V001.000007.abc
Moving XYZ_123_028_V001.000007.abc -to- XYZ_123_028_V001_F01A_9G.000007.abc


Filename = XYZ_123_028_V001.000008.abc
Moving XYZ_123_028_V001.000008.abc -to- XYZ_123_028_V001_F01A_9G.000008.abc


Filename = XYZ_123_028_V001.000009.abc
Moving XYZ_123_028_V001.000009.abc -to- XYZ_123_028_V001_F01A_9G.000009.abc

[jenkins@sb12 XYZ_123_028_V001_F01A_9G]$
[jenkins@sb12 XYZ_123_028_V001_F01A_9G]$ ls -l
total 0
-rw-r--r-- 1 jenkins dmadmin 0 Jun 12 18:55 XYZ_123_028_V001_F01A_9G.0000010.abc
-rw-r--r-- 1 jenkins dmadmin 0 Jun 12 18:55 XYZ_123_028_V001_F01A_9G.000001.abc
-rw-r--r-- 1 jenkins dmadmin 0 Jun 12 18:55 XYZ_123_028_V001_F01A_9G.000002.abc
-rw-r--r-- 1 jenkins dmadmin 0 Jun 12 18:55 XYZ_123_028_V001_F01A_9G.000003.abc
-rw-r--r-- 1 jenkins dmadmin 0 Jun 12 18:55 XYZ_123_028_V001_F01A_9G.000004.abc
-rw-r--r-- 1 jenkins dmadmin 0 Jun 12 18:55 XYZ_123_028_V001_F01A_9G.000005.abc
-rw-r--r-- 1 jenkins dmadmin 0 Jun 12 18:55 XYZ_123_028_V001_F01A_9G.000006.abc
-rw-r--r-- 1 jenkins dmadmin 0 Jun 12 18:55 XYZ_123_028_V001_F01A_9G.000007.abc
-rw-r--r-- 1 jenkins dmadmin 0 Jun 12 18:55 XYZ_123_028_V001_F01A_9G.000008.abc
-rw-r--r-- 1 jenkins dmadmin 0 Jun 12 18:55 XYZ_123_028_V001_F01A_9G.000009.abc
[jenkins@sb12 XYZ_123_028_V001_F01A_9G]$