Search code examples
linuxcommand-linedata-transfercpio

linux cpio to copy directory structure and file names?


I want to copy a directory structure from a remote machine to a local machine. I want the file names too but not the content of the file.

Presently I did this in the remote machine:

find . -type d -print | cpio -oO dirs.cpio

then copied the dirs.cpio file to local machine, and ran the command after going to directory where I want the structure replicated:

cpio -iI dirs.cpio

So this creates the directory structure I want including subdirectories, but does not copies the file names. I want the directory structure and file names but not their content.

How can I get the file names too??


Solution

  • It's easier without cpio. On the source:

    find . -exec ls -Fd {} + > stuff
    

    This makes a file listing all directories (with trailing slashes thanks to ls -F) and files.

    On the destination:

    ./makestuff < stuff
    

    Where makestuff is this script:

    while read name; do
      if [ "${name:${#name}-1}" = "/" ]; then
        mkdir -p "$name"
      else
        touch "$name"
      fi
    done