Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating directories from a list

Tags:

bash

shell

awk

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.

like image 626
Turtle Avatar asked Nov 16 '25 05:11

Turtle


1 Answers

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.

like image 196
chepner Avatar answered Nov 18 '25 20:11

chepner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!