我如何将这些命令总结或浓缩为单个命令以保留其功能?
cp 1/*.jpg ../../5folds_all/augmented_images/0/train/001
cp 1/*.jpg ../../5folds_all/augmented_images/1/train/001
cp 1/*.jpg ../../5folds_all/augmented_images/2/train/001
cp 1/*.jpg ../../5folds_all/augmented_images/3/train/001
cp 1/*.jpg ../../5folds_all/augmented_images/4/train/001
cp 2/*.jpg ../../5folds_all/augmented_images/5/train/002
cp: target ‘../../5folds_all/augmented_images/5/train/002’ is not a directory
cp 2/*.jpg ../../5folds_all/augmented_images/0/train/002
cp 2/*.jpg ../../5folds_all/augmented_images/1/train/002
cp 2/*.jpg ../../5folds_all/augmented_images/2/train/002
cp 2/*.jpg ../../5folds_all/augmented_images/3/train/002
cp 2/*.jpg ../../5folds_all/augmented_images/4/train/002
cp 3/*.jpg ../../5folds_all/augmented_images/4/train/003
cp 3/*.jpg ../../5folds_all/augmented_images/3/train/003
cp 3/*.jpg ../../5folds_all/augmented_images/2/train/003
cp 3/*.jpg ../../5folds_all/augmented_images/1/train/003
cp 3/*.jpg ../../5folds_all/augmented_images/0/train/003
cp 4/*.jpg ../../5folds_all/augmented_images/4/train/004
cp: cannot stat ‘4/*.jpg’: No such file or directory
cp 5/*.jpg ../../5folds_all/augmented_images/0/train/005
cp 5/*.jpg ../../5folds_all/augmented_images/1/train/005
cp 5/*.jpg ../../5folds_all/augmented_images/2/train/005
cp 5/*.jpg ../../5folds_all/augmented_images/3/train/005
cp 5/*.jpg ../../5folds_all/augmented_images/4/train/005
cp 6/*.jpg ../../5folds_all/augmented_images/4/train/006
cp: cannot stat ‘6/*.jpg’: No such file or directory
cp 7/*.jpg ../../5folds_all/augmented_images/4/train/007
cp 7/*.jpg ../../5folds_all/augmented_images/3/train/007
cp 7/*.jpg ../../5folds_all/augmented_images/2/train/007
cp 7/*.jpg ../../5folds_all/augmented_images/1/train/007
cp 7/*.jpg ../../5folds_all/augmented_images/0/train/007
cp 8/*.jpg ../../5folds_all/augmented_images/0/train/008
cp 8/*.jpg ../../5folds_all/augmented_images/1/train/008
cp 8/*.jpg ../../5folds_all/augmented_images/2/train/008
cp 8/*.jpg ../../5folds_all/augmented_images/3/train/008
cp 8/*.jpg ../../5folds_all/augmented_images/4/train/008
cp 9/*.jpg ../../5folds_all/augmented_images/4/train/009
cp 9/*.jpg ../../5folds_all/augmented_images/3/train/009
cp 9/*.jpg ../../5folds_all/augmented_images/2/train/009
cp 9/*.jpg ../../5folds_all/augmented_images/1/train/009
cp 9/*.jpg ../../5folds_all/augmented_images/0/train/009
cp 10/*.jpg ../../5folds_all/augmented_images/0/train/010
cp: cannot stat ‘10/*.jpg’: No such file or directory
cp 11/*.jpg ../../5folds_all/augmented_images/0/train/011
cp: cannot stat ‘11/*.jpg’: No such file or directory
cp 12/*.jpg ../../5folds_all/augmented_images/0/train/012
cp 12/*.jpg ../../5folds_all/augmented_images/1/train/012
cp 12/*.jpg ../../5folds_all/augmented_images/2/train/012
cp 12/*.jpg ../../5folds_all/augmented_images/3/train/012
cp 12/*.jpg ../../5folds_all/augmented_images/4/train/012
cp 13/*.jpg ../../5folds_all/augmented_images/4/train/013
cp 13/*.jpg ../../5folds_all/augmented_images/3/train/013
cp 13/*.jpg ../../5folds_all/augmented_images/2/train/013
cp 13/*.jpg ../../5folds_all/augmented_images/1/train/013
cp 13/*.jpg ../../5folds_all/augmented_images/0/train/013
cp 14/*.jpg ../../5folds_all/augmented_images/0/train/014
cp 14/*.jpg ../../5folds_all/augmented_images/1/train/014
cp 14/*.jpg ../../5folds_all/augmented_images/2/train/014
cp 14/*.jpg ../../5folds_all/augmented_images/3/train/014
cp 14/*.jpg ../../5folds_all/augmented_images/4/train/014
cp 15/*.jpg ../../5folds_all/augmented_images/4/train/015
cp 15/*.jpg ../../5folds_all/augmented_images/3/train/015
cp 15/*.jpg ../../5folds_all/augmented_images/2/train/015
cp 15/*.jpg ../../5folds_all/augmented_images/1/train/015
cp 15/*.jpg ../../5folds_all/augmented_images/0/train/015
cp 16/*.jpg ../../5folds_all/augmented_images/0/train/016
cp: cannot stat ‘16/*.jpg’: No such file or directory
答案1
您可以使用嵌套for
循环,外层循环用于源索引($i
),内层循环用于目标索引($n
):
for i in {1..16}; do
# Skip if no files found in $i/
ls $i/*.jpg &> /dev/null || continue
for n in {0..4}; do
target="../../5folds_all/augmented_images/${n}/train/$(printf "%03d" $i)"
# create target folder if not exists
mkdir -p "$target"
# copy to target folder
cp $i/*.jpg "$target"
done
done