[英]Shell script to extract all zip files in a directory and its sub directory, and copy the extracted files into another folder with same hierarchy
[英]Shell Script to copy all files in a directory to a specified folder
我是shell脚本的新手,我试图想办法编写一个脚本,将当前目录中的所有文件复制到.txt文件指定的目录中,如果有匹配的名称,则添加当前日期以FileName_YYYYMMDDmmss的形式写入要复制的文件的名称以防止覆盖。
有人可以帮我吗?
我看到了一些想法
#!/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)。
注意:我使用全时戳(YYYYMMDDHHmmss)代替你的YYYYMMDDmmss,因为它似乎不合逻辑。
这个怎么样? 我假设目标目录在文件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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.