【发布时间】:2019-11-04 14:56:09
【问题描述】:
我的代码从用户那里获取两个参数。我的目标是找到用户提供的文件夹(第一个参数)并制作尽可能多的副本(第二个参数)。截至目前,我的代码递归地打印出所有子目录和子目录中的文件......我的代码还复制了用户指定的文件次数。但是,这是我的问题,我的复制功能只适用于文件而不是文件夹......我想知道如何修改我的复制功能(或代码的任何其他部分),以便它递归地复制指定的文件夹及其所有原创内容
我知道代码底部的 cp 函数需要更改为 -r ./sourceFolder ./destFolder 但考虑到传入的参数,我不知道该怎么做。毕竟,目标文件夹名称将只是原始文件夹,其末尾有一个递增的数字。
#!/bin/bash
read -p "Your folder name is $1 and the number of copies is $2. Press Y for yes N for no " -n 1 -r
if ! [[ $REPLY =~ ^[Yy]$ ]]
then
echo
echo "Rerun script and pass in the folder and how many copies yo would like (e.g folder 4)"
exit
fi
echo
echo "-------------COPY FILE----------------"
FOLDER=$1
for f IN $folder
do
if [ ! -f "$FOLDER" ]
then
echo "folder "$FOLDER" does not exist"
exit 1
fi
done
DIR="."
function list_files()
{
cd $1
echo; echo "$(pwd)":; #Display Directory name
for i in *
do
if test -d "$i" #if dictionary
then
list_files "$i" #recursively list files
cd ..
else
echo "$i"; #Display File name
fi
done
}
j=$2
for i in "$*"
do
DIR=$1
list_files "$DIR"
for ((i=1; i<J; i++))
do
cp "$1" "$1$i"; #copies the file i amount of times, and creates new files with names that increment by 1
done
shift 1
done
【问题讨论】:
-
你试过
if test -d "$1"吗? -
是...错误状态 cp: cannot stat 'folder' - no such file or directory found... 而且我很确定我传入的文件夹存在