[ACCEPTED]-mkdir command for a list of filenames paths-ubuntu

Accepted answer
Score: 12

This can be done in native bash syntax without 3 any calls to external binaries:

while read line; do mkdir -p "${line%/*}"; done < infile

Or perhaps 2 with a just a single call to mkdir if you have 1 bash 4.x

mapfile -t arr < infile; mkdir -p "${arr[@]%/*}"
Score: 2

How about...

for p in $(xargs < somefile.txt);
    mkdir -p $(dirname ${p})


Score: 2
xargs -n 1 dirname <somefile.txt | xargs mkdir -p


Score: 0

It can be done without loop also (provided 1 input file not huge):

mkdir -p $(perl -pe 's#/(?!.*/).*$##' file.txt)
Score: 0

If you have file "file1" with filenames 3 you could try this oneliner:

cat file1 |xargs -I {} dirname "{}"| sort -u | xargs -I{} mkdir -p "{}"

Use of:

xargs -I{} mkdir -p "{}"

ensures 2 that even path names with spaces will be 1 created

Score: 0

Using a perl one-liner and File::Path qw(make_path):

perl -MFile::Path=make_path -lne 'make_path $_' dirlist.txt


More Related questions