Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using "touch" to create directories? [closed]

Tags:

linux

bash

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

3) Result: the 2) "creates the files" [i mean only with the same filename, but with 0 Byte size] ok. But if there are subdirs in the "A" directory, then the 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?

like image 944
LanceBaynes Avatar asked Jan 17 '11 09:01

LanceBaynes


1 Answers

Since find outputs one file per line:

cat a.txt | while read file; do
    if [[ "$file" = */* ]]; then
        mkdir -p "${file%/*}";
    fi;

    touch "$file";
done

EDIT:

This would be slightly more efficient if the directories where created in a separate step:

cat a.txt | grep / | sed 's|/[^/]*$||' | sort -u | xargs -d $'\n' mkdir -p

cat a.txt | while read file; do
    touch "$file";
done

And, no, touch cannot create directories on its own.

like image 157
thkala Avatar answered Nov 10 '22 11:11

thkala