我想要将mv
具有特定扩展名的文件添加到新创建的目录中。该脚本会在所有新创建的文件夹中移动具有所有扩展名的文件,但这不是本意。相反,它应该将特定于扩展的文件移动到特定文件夹。我怎样才能做到这一点?
for i in 1 2 3
do
mkdir -p backup/ch0${i}
if [ $? -eq ]; then
echo "directory backup/ch0${i} created"
for j in c h sh
do
count=0
count=`expr $count + 1`
if [ $count==i ]; then
cp /home/owner/*.${j} backup/ch0${i}
if [ $count!=i ]; then
continue;
elif [ $? -ne 0 ]; then
break 2;
fi
done
else
echo "could not back up directory!!"
fi
done
答案1
这应该有效:
backup_extensions()
{
count=0
while [ -n "$1" ]
do
let count+=1
mkdir -p backup/ch0${count}
cp /home/owner/*.${1} backup/ch0${count} || return 2
shift
done
}
backup_extensions c h sh
答案2
我还没有验证这一点,但它应该可以完成这项工作。
backups=( 1:c 2:h 3:sh )
for set in "${backups[@]}"; do
IFS=":" read dir_bkup file_ext <<< "$set"
if ! mkdir -p "backup/ch0$dir_bkup"; then
echo "Could not create "backup/ch0$dir_bkup. Skipping"
continue
fi
cp "/home/owner/"*".$file_ext" "backup/$dir_bkup/"
done