Copiar la estructura de carpetas (archivos sans) de una ubicación a otra


Quiero crear un clon de la estructura de nuestro servidor de archivos multi-terabyte. Sé que cp parents parents puede mover un archivo y su estructura padre, pero ¿hay alguna manera de copiar la estructura de directorios intacta?

Quiero copiar a un sistema linux y nuestro servidor de archivos es CIFS montado allí.

Author: r00fus, 2010-11-02

12 answers

Podrías hacer algo como:

find . -type d >dirs.txt

Para crear la lista de directorios, entonces

xargs mkdir -p <dirs.txt

Para crear los directorios en el destino.

 124
Author: Greg Hewgill,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-11-01 23:37:12
cd /path/to/directories &&
find . -type d -exec mkdir -p -- /path/to/backup/{} \;
 59
Author: amphetamachine,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-11-01 23:39:26

Aquí hay una solución simple usando rsync:

rsync -av -f"+ */" -f"- *" "$source" "$target"
  • una línea
  • no hay problemas con los espacios
  • conservar los permisos

Encontré esta solución allí

 14
Author: Gildas,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-08-30 12:07:23

No sé si está buscando una solución en Linux. Si es así, puedes probar esto:

$ mkdir destdir
$ cd sourcedir
$ find . -type d | cpio -pdvm destdir
 5
Author: zerodin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-09-23 16:04:33

Esto copia los atributos de directorios y archivos, pero no los datos de archivos:

cp -R --attributes-only SOURCE DEST

Luego puede eliminar los atributos de los archivos si no está interesado en ellos:

find DEST -type f -exec rm {} \;
 4
Author: toliveira,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-12-27 17:15:42

Esto funciona:

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

Simplemente reemplace SOURCE_DIR y DEST_DIR.

 2
Author: mijhael3000,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-12-04 08:47:21

La siguiente solución funcionó bien para mí en varios entornos:

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

find "$sourceDir" -type d | sed -e "s?$sourceDir?$targetDir?" | xargs mkdir -p
 1
Author: yaccob,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-07-22 16:07:51

Sustitúyase target_dir y source_dir por los valores apropiados:

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

Probado en OSX+Ubuntu.

 1
Author: luissquall,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-08-30 13:40:33

Esto resuelve incluso el problema con los espacios en blanco:

En el directorio original/fuente:

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

Luego recrearlo en el dir recién creado:

mkdir -p <../<SOURCEDIR>/dirs2.txt
 1
Author: Thim,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-12-18 13:32:37

Si puede obtener acceso desde una máquina Windows, puede usar xcopy con /T y / E para copiar solo la estructura de carpetas (la /E incluye carpetas vacías)

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

[EDITAR!]

Este usa rsync para recrear la estructura de directorios pero sin los archivos. http://psung.blogspot.com/2008/05/copying-directory-trees-with-rsync.html

En realidad podría ser mejor:)

 0
Author: dotalchemy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-11-01 23:39:26

Un script python de Sergiy Kolodyazhnyy publicado en Copiar solo carpetas no archivos?:

#!/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)) 

O desde el shell:

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

PARA su información:

 0
Author: Franck Dernoncourt,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-08-08 23:13:20

Aquí hay una solución en php que:

  • copia los directorios (no recursivamente, solo un nivel)
  • conserva los permisos
  • a diferencia de la solución rsync, es rápido incluso con directorios que contienen miles de archivos, ya que ni siquiera entra en las carpetas
  • no tiene problemas con los espacios
  • debe ser fácil de leer y ajustar

Crea un archivo como syncDirs.php con este contenido:

<?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());
}

Ejecutarlo como usuario que tiene suficiente derechos:

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

 0
Author: Christopher K.,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-07-25 18:02:22