[英][Unix][Shell Script] Copy all files from one folder to the other
I am trying to create a bash shell script for daily cronjob. 我正在尝试为日常cronjob创建一个bash shell脚本。
The script is to copy all files with txt extension (including the sub-directories) from the folder "fromDir" to the folder "toDir". 该脚本是将所有带有txt扩展名的文件(包括子目录)从文件夹“ fromDir”复制到文件夹“ toDir”。
It will also make the "toDir" directory if it doesnt exist. 如果“ toDir”目录不存在,它也会建立目录。
I am trying to use tar here to compress the files and decompress after it moved to speed up the time it spent during the transfer, the target folder should have exactly the same structure as the original one. 我试图在这里使用tar压缩文件并在移动后进行解压缩以加快传输过程中的时间,目标文件夹的结构应与原始文件夹完全相同。
#!/bin/bash -l
fromDir='c:/fromFolder/' ;
toDir='c:/toFolder/' ;
find $fromDir -maxdepth 3 -name '*.txt' -print |
tar zcvf - -T - |
mkdir -p -m 777 $toDir ;
cd $toDir && tar zxvf -
1st see my comment, then if you tar
don't support -C
argument (change to dir befor tar), you could try this: 首先看到我的评论,然后,如果您的
tar
不支持-C
参数(更改为dir befor tar),则可以尝试以下操作:
#!/bin/bash
fromDir='c:/fromFolder/' ;
toDir='c:/toFolder/' ;
mkdir -p -m 777 $toDir ;
find $fromDir -maxdepth 3 -name '*.txt' -print |
tar zcf - -T - | (
cd $toDir
tar zxvf -
)
In case you could use rsync you could try this: 如果您可以使用rsync ,则可以尝试以下操作:
rsync -av --include="*/" --include="*.txt" --exclude="*" ~/from/ ~/to/ --dry-run
This can be interpreted as for include every directory with *.txt
and exclude anything else (the order is important) 这可以解释为包含
*.txt
每个目录并排除其他任何内容(顺序很重要)
The option --dry-run
is just to print what will be done but no files will be copied. --dry-run
选项仅用于打印将要执行的操作,但不会复制任何文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.