Linux – Using “touch” to create directories – create “shadow” copy of directories recursively

bashlinux

  1. In the "A" directory:
    find . -type f > a.txt
  2. In the "B" directory:
    cat a.txt | while read FILENAMES; do touch "$FILENAMES"; done

Result: Step 2 "creates the files" (I mean only with the same filename, but with 0 Byte size) but if there are subdirs in the "A" directory, then step 2 can't create the files in the subdir, because there are no directories in it.

Question: Is there a way, that "touch" can create directories?

Best Answer

quick shot:

while read FILENAME
do
  mkdir -p $(dirname "$FILENAME") && touch "$FILENAME"
done < a.txt

Be aware of special chars (whitespaces, ...) in file-/pathnames and so on ...