your programing

한 위치에서 다른 위치로 폴더 구조 (sans 파일) 복사

lovepro 2020. 10. 6. 18:49
반응형

한 위치에서 다른 위치로 폴더 구조 (sans 파일) 복사


멀티 테라 바이트 파일 서버 구조의 복제본을 만들고 싶습니다. cp --parents가 파일을 이동할 수 있고 부모 구조라는 것을 알고 있지만 디렉터리 구조를 그대로 복사 할 수있는 방법이 있습니까?

Linux 시스템에 복사하고 싶습니다. 파일 서버가 CIFS에 마운트되어 있습니다.


다음과 같이 할 수 있습니다.

find . -type d >dirs.txt

디렉토리 목록을 생성 한 다음

xargs mkdir -p <dirs.txt

대상에 디렉터리를 만듭니다.


cd /path/to/directories &&
find . -type d -exec mkdir -p -- /path/to/backup/{} \;

다음은 rsync를 사용하는 간단한 솔루션입니다.

rsync -av -f"+ */" -f"- *" "$source" "$target"
  • 한 줄
  • 공백 문제 없음
  • 권한 유지

이 솔루션을 찾았습니다.


Linux에서 솔루션을 찾고 있는지 모르겠습니다. 그렇다면 다음을 시도해 볼 수 있습니다.

$ mkdir destdir
$ cd sourcedir
$ find . -type d | cpio -pdvm destdir

이렇게하면 디렉토리 및 파일 속성이 복사되지만 파일 데이터는 복사되지 않습니다.

cp -R --attributes-only SOURCE DEST

그런 다음 관심이없는 경우 파일 속성을 삭제할 수 있습니다.

find DEST -type f -exec rm {} \;

이것은 작동합니다 :

find ./<SOURCE_DIR>/ -type d | sed 's/\.\/<SOURCE_DIR>//g' | xargs -I {} mkdir -p <DEST_DIR>"/{}"

SOURCE_DIR 및 DEST_DIR을 바꾸십시오.


이것은 공백 문제도 해결합니다.

원본 / 소스 디렉토리에서 :

find . -type d -exec echo "'{}'" \; > dirs2.txt

그런 다음 새로 만든 디렉토리에서 다시 만듭니다.

mkdir -p <../<SOURCEDIR>/dirs2.txt

다음 솔루션은 다양한 환경에서 저에게 잘 맞았습니다.

sourceDir="some/directory"
targetDir="any/other/directory"

find "$sourceDir" -type d | sed -e "s?$sourceDir?$targetDir?" | xargs mkdir -p

대체 target_dirsource_dir적절한 값 :

cd target_dir && (cd source_dir; find . -type d ! -name .) | xargs -i mkdir -p "{}"

OSX + Ubuntu에서 테스트되었습니다.


Sergiy Kolodyazhnyy의 Python 스크립트는 Copy only folders not files? :

#!/usr/bin/env python
import os,sys
dirs=[ r for r,s,f in os.walk(".") if r != "."]
for i in dirs:
    os.makedirs(os.path.join(sys.argv[1],i)) 

또는 쉘에서 :

python -c 'import os,sys;dirs=[ r for r,s,f in os.walk(".") if r != "."];[os.makedirs(os.path.join(sys.argv[1],i)) for i in dirs]' ~/new_destination

참고 :


또 다른 접근 방식은 tree강력한 옵션을 기반으로 디렉토리 트리를 매우 편리하게 탐색하는를 사용하는 것입니다. 디렉토리 전용, 빈 디렉토리 제외, 패턴이있는 이름 제외, 패턴이있는 이름 만 포함 등의 옵션이 있습니다. 체크 아웃man tree

Advantage: you can edit or review the list, or if you do a lot of scripting and create a batch of empty directories frequently

Approach: create a list of directories using tree, use that list as an arguments input to mkdir

tree -dfi --noreport > some_dir_file.txt

-dfi lists only directories, prints full path for each name, makes tree not print the indentation lines,

--noreport Omits printing of the file and directory report at the end of the tree listing, just to make the output file not contain any fluff

Then go to the destination where you want the empty directories and execute

xargs mkdir < some_dir_file.txt

If you can get access from a Windows machine, you can use xcopy with /T and /E to copy just the folder structure (the /E includes empty folders)

http://ss64.com/nt/xcopy.html

[EDIT!]

This one uses rsync to recreate the directory structure but without the files. http://psung.blogspot.com/2008/05/copying-directory-trees-with-rsync.html

Might actually be better :)


Here is a solution in php that:

  • copies the directories (not recursively, only one level)
  • preserves permissions
  • unlike the rsync solution, is fast even with directories containing thousands of files as it does not even go into the folders
  • has no problems with spaces
  • should be easy to read and adjust

Create a file like syncDirs.php with this content:

<?php
foreach (new DirectoryIterator($argv[1]) as $f) {
    if($f->isDot() || !$f->isDir()) continue;
        mkdir($argv[2].'/'.$f->getFilename(), $f->getPerms());
        chown($argv[2].'/'.$f->getFilename(), $f->getOwner());
        chgrp($argv[2].'/'.$f->getFilename(), $f->getGroup());
}

Run it as user that has enough rights:

sudo php syncDirs.php /var/source /var/destination

참고URL : https://stackoverflow.com/questions/4073969/copy-folder-structure-sans-files-from-one-location-to-another

반응형