简体   繁体   English

复制具有空文件的目录树

[英]Copy directory tree with empty files

I am looking to create a copy of a folder tree with empty files in it. 我正在寻找创建一个带有空文件的文件夹树的副本。 The files in the source folders are not empty but I want the files in the destination folders to be empty. 源文件夹中的文件不为空,但我希望目标文件夹中的文件为空。 (I don't think I can just use soft/hard link because I am on Mac OS X , right?) It's to keep track of the content of many external hard drives with a lot of larges video files in them. (我不认为只能使用软/硬链接,因为我在Mac OS X上 ,对吗?)这是为了跟踪其中包含大量视频文件的许多外部硬盘驱动器的内容。

I found rsync -a -f"+ */" -f"- *" source/ destination/ to copy the folders but I didn't find a way to create the empty files (I tried a tcsh foreach but it failed because there are spaces in folders name). 我发现rsync -a -f"+ */" -f"- *" source/ destination/复制文件夹,但是我没有找到创建空文件的方法(我尝试了一个tcsh foreach但是失败了,因为那里是文件夹名称中的空格)。

Thanks for your help! 谢谢你的帮助!

Building on the answer from ursusd8, here's a recursive solution that will work in bash version 4, if you have that installed... 如果您安装了ursusd8的答案,那么这是一个递归解决方案,它将在bash版本4中工作...

shopt -s globstar
cd /path/to/source/
for f in **/*; do
  mkdir -p "/path/to/target/${f%/*}"   # make the containing directory if required
  touch "/path/to/target/$f"           # make a zero-length file
done

This runs the mkdir command many more times than required, but it's quick. 这会比所需次数多运行mkdir命令,但速度很快。 Note that bash 4 may not be available on your system unless you've explicitly installed it using MacPorts or homebrew or the like. 请注意,除非您已使用MacPorts自制软件等明确安装了bash 4,否则它在您的系统上可能不可用。

A longer alternative would be to separate the steps of creating your directory tree and touching the files: 一个更长的替代方法是分离创建目录树和触摸文件的步骤:

cd /path/to/source/
find . -type d -exec mkdir -p /path/to/target/{} \;
find . -type f -exec touch /path/to/target/{} \;

Note that this two-stage find solution uses no bashisms, and can likely be run in any shell. 请注意,此两阶段find解决方案不使用Bashisms,并且可以在任何Shell中运行。

Here is a solution, which can even handle uncommon filenames correctly (like filenames with a newline inside etc.) 这是一个解决方案,它甚至可以正确处理不常见的文件名(例如内部带有换行符的文件名等)。

It will first create the directory tree and then create empty files. 它将首先创建目录树,然后创建空文件。 additional external commands other than find , mkdir and touch are required. 除了findmkdirtouch还需要其他外部命令。

#!/bin/bash

# IMPORTANT: SOURCE and DESTINATION have to be absolute pathes
# To fix the missing parameter -printf of find in mac os x,
# i used a technique to reproduce the functionality.
# But now it IS IMPORTANT that both of these variables contain an absolute path!

SOURCE="/home/you/source_dir/"
DESTINATION="/where/ever/destination_dir/"

#first 'copy' the directory tree
while
    IFS="" read -r -d '' -u 9 dir
do
    #remove the $SOURCE part
    dir="${dir#${SOURCE}}"

    mkdir "${DESTINATION}/${dir}"
done 9< <(find "${SOURCE}" -type d -print0)

#then create empty files inside the new tree
while
    IFS="" read -r -d '' -u 9 file
do
    #remove the $SOURCE part
    file="${file#${SOURCE}}"

    touch "${DESTINATION}/${file}"
done 9< <(find "${SOURCE}" -type f -print0)

EDIT: Removed usage of -printf of find tool, as it seemingly didn't exists in mac os x - used bash parameter substitution to recreate the behavior. 编辑:删除了find工具-printf的用法,因为它似乎在Mac OS X中不存在-使用bash参数替换来重新创建行为。

You can try using touch in for loop: 您可以尝试使用touch in for循环:

cd /PATH/TO/SOURCE/FOLDER/
for f in *; 
 do touch /PATH/TO/DEST/FOLDER/$f; 
done

EDIT: The code above will give you all files and folders in /SOURCE/FOLDER , but will not be recursive . 编辑:上面的代码将为您提供/SOURCE/FOLDER所有文件和文件/SOURCE/FOLDER ,但不会递归 To create empty files only for source files and to create a directory tree(all the subfolders), you can use: 要仅为源文件创建空文件并创建目录树(所有子文件夹),可以使用:

FILES=$(find /PATH/TO/SOURCE/FOLDER/ -type f) 
for f in $FILES; 
do touch /PATH/TO/DEST/FOLDER/$f;
done

I hope it helps! 希望对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM