简体   繁体   中英

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}' cat list.txt | cut -f1 | awk 'NR==1 {print}' cat list.txt | cut -f1 | awk 'NR==1 {print}' gives me the name of the individual directory I want (ie 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 .

Try this:

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

or

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

If everything looks okay remove echo .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM