简体   繁体   English

[Unix] [Shell脚本]将所有文件从一个文件夹复制到另一个文件夹

[英][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.

相关问题 Shell脚本重命名并将文件从一个文件夹移动到另一个文件夹 - Shell script rename and move files from one folder to the other UNIX Shell将文件从一个文件夹移动到另一个文件夹 - UNIX shell to move files from one folder to another folder Shell脚本复制所有文件和子目录-带有空格的文件夹名称 - Shell Script Copy All Files and Subdirectories - Folder Names With Spaces 一个shell脚本来复制一个文件夹及其所有子文件夹,但请注意它是文件 - a shell script to copy a folder and all it's subfolders, but noit it's files Shell脚本将目录中的所有文件复制到指定的文件夹 - Shell Script to copy all files in a directory to a specified folder 用于将文件夹名称复制并添加到来自多个子目录的文件的 Shell 脚本 - Shell script to copy and prepend folder name to files from multiple subdirectories Shell 脚本:将带有通配符 (*) 的文件从一个文件夹复制到另一个文件夹 - Shell Script: Copying files with wildcard (*) to from one folder to another folder 复制shell脚本中除一个文件以外的所有文件 - Copy all files excluding one file in shell script 如何将多个文件复制到不同的文件夹中删除每个文件上的一些字符 | unix korn 外壳 - How to copy multiple files to a different folder removing some characters on each one | unix korn shell UNIX Shell脚本读取文件,复制文件和删除文件 - UNIX shell script to read files, copy files and delete files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM