我有几个文件夹,不同的图像共享文件名,文件夹结构如下:
/parent/folder001/img001.jpg /parent/folder001/img002.jpg /parent/folder002/img001.jpg /parent/folder002/img002.jpg /parent/folder003/img001.jpg /parent/folder003/img002.jpg ...
/newfolder/folder001_img001.jpg /newfolder/folder001_img002.jpg /newfolder/folder002_img001.jpg /newfolder/folder002_img002.jpg /newfolder/folder003_img001.jpg /newfolder/folder003_img002.jpg ...
(如果newfolder不是父文件夹的子文件夹,那可能会更好,因为这可能最终导致非常奇怪的递归.)
理想情况下,我希望能够重用该脚本来“更新”新文件夹,因为我可能需要在以后添加更多包含文件夹的图像.
如何使用shell脚本完成此操作?
解决方法
这有点乏味但会做:
#!/bin/bash parent=/parent newfolder=/newfolder mkdir "$newfolder" for folder in "$parent"/*; do if [[ -d "$folder" ]]; then foldername="${folder##*/}" for file in "$parent"/"$foldername"/*; do filename="${file##*/}" newfilename="$foldername"_"$filename" cp "$file" "$newfolder"/"$newfilename" done fi done
将父路径设置为父变量,将newfolder路径设置为newfolder变量.