Turtle
Turtle

Reputation: 163

Creating directories from a list

I have a list (list.txt)

E001    Apples    Good stuff
E002    Pears    Nicely done
E091    Grapes    Bleah

I wanted to create directories using the first column so I tried

#!/bin/sh
for num in $(seq 1 3) #Because the list is long
do
cat list.txt | cut -f1 | awk 'NR==$num{print}' > idx.tmp
idx=$(idx.tmp)
mkdir $idx
rm idx.tmp
done

Not only is this conceivably bulky, it doesn't work - even though cat list.txt | cut -f1 | awk 'NR==1 {print}' gives me the name of the individual directory I want (i.e. E001)

Ideally I wish to obtain a bunch of empty folders created within the folder which the script ran (does that sound right?) named using the first column of each row in list.txt.

Upvotes: 2

Views: 7804

Answers (2)

Cyrus
Cyrus

Reputation: 88999

Try this:

cut -d " " -f 1 file | xargs echo mkdir

or

awk '{print $1}' file | xargs echo mkdir

If everything looks okay remove echo.

Upvotes: 5

chepner
chepner

Reputation: 532418

A simple loop suffices:

while read dirname others; do
    mkdir "$dirname"
done < list.txt

This reads a line at a time from the file, setting the value of dirname to the first field and others to the remaining portion for each line, and runs mkdir with the given directory name.

Upvotes: 9

Related Questions