Shell脚本将目录中的所有文件复制到指定文件夹
我是shell脚本新手,我正在试图找到一种编写脚本的方法,该脚本将当前目录中的所有文件复制到从.txt文件指定的目录中,如果有匹配的名称,它会将当前日期以文件名的形式添加到被复制的文件名中,以防止覆盖 有人能帮我吗 我看到有人在想一些关于Shell脚本将目录中的所有文件复制到指定文件夹,shell,unix,loops,sh,cp,Shell,Unix,Loops,Sh,Cp,我是shell脚本新手,我正在试图找到一种编写脚本的方法,该脚本将当前目录中的所有文件复制到从.txt文件指定的目录中,如果有匹配的名称,它会将当前日期以文件名的形式添加到被复制的文件名中,以防止覆盖 有人能帮我吗 我看到有人在想一些关于 #!/bin/bash source=$pwd #I dont know wheter this actually makes sense I just want to #say that my s
#!/bin/bash
source=$pwd #I dont know wheter this actually makes sense I just want to
#say that my source directory is the one that I am in right now
destination=$1 #As I said I want to read the destination off of the .txt file
for i in $source #I just pseudo coded this part because I didn't figure it out.
do
if(file name exists)
then
copy by changing name
else
copy
fi
done
问题是我不知道如何检查名称是否存在,同时复制和重命名
谢谢我想这就是你想要的:
#!/bin/bash
dir=$(cat a.txt)
for i in $(ls -l|grep -v "^[dt]"|awk '{print $9}')
do
cp $i $dir/$i"_"$(date +%Y%m%d%H%M%S)
done
我假设a.txt只包含目标目录的名称。如果还有其他条目,您应该在第一条语句中添加一些过滤器(使用grep或awk)
注意:我用全日制邮票(yyyymmddhhmms)代替了你的yyyymmddmms,因为它看起来不符合逻辑 我想这就是你想要的:
#!/bin/bash
dir=$(cat a.txt)
for i in $(ls -l|grep -v "^[dt]"|awk '{print $9}')
do
cp $i $dir/$i"_"$(date +%Y%m%d%H%M%S)
done
我假设a.txt只包含目标目录的名称。如果还有其他条目,您应该在第一条语句中添加一些过滤器(使用grep或awk)
注意:我用全日制邮票(yyyymmddhhmms)代替了你的yyyymmddmms,因为它看起来不符合逻辑 这个怎么样?我假设目标目录位于 文件new_dir.txt
#!/bin/bash
new_dir=$(cat new_dir.txt)
now=$(date +"%Y%m%d%M%S")
if [ ! -d $new_dir ]; then
echo "$new_dir doesn't exist" >&2
exit 1
fi
ls | while read ls_entry
do
if [ ! -f $ls_entry ]; then
continue
fi
if [ -f $new_dir/$ls_entry ]; then
cp $ls_entry $new_dir/$ls_entry\_$now
else
cp $ls_entry $new_dir/$ls_entry
fi
done
这个怎么样?我假设目标目录位于 文件new_dir.txt
#!/bin/bash
new_dir=$(cat new_dir.txt)
now=$(date +"%Y%m%d%M%S")
if [ ! -d $new_dir ]; then
echo "$new_dir doesn't exist" >&2
exit 1
fi
ls | while read ls_entry
do
if [ ! -f $ls_entry ]; then
continue
fi
if [ -f $new_dir/$ls_entry ]; then
cp $ls_entry $new_dir/$ls_entry\_$now
else
cp $ls_entry $new_dir/$ls_entry
fi
done
我想把.txt文件作为输入传递给程序,但我不想硬编码。你知道怎么做吗?@user2591144将行“new_dir=$(cat new_dir.txt)”替换为“new_dir=$(cat$1)”。我想将.txt文件作为输入传递给程序,但不想硬编码。你知道怎么做吗?@user2591144将行“new_dir=$(cat new_dir.txt)”替换为“new_dir=$(cat$1)”。