简体   繁体   English

从列表创建目录

[英]Creating directories from a list

I have a list (list.txt) 我有一个清单(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}' cat list.txt | cut -f1 | awk 'NR==1 {print}' gives me the name of the individual directory I want (ie E001) cat list.txt | cut -f1 | awk 'NR==1 {print}'给我我想要的单个目录的名称(即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 . 理想情况下,我希望获得一堆在脚本运行的文件夹中创建的空文件夹(听起来正确吗?),该文件夹使用list.txt中每一行的第一列进行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 . 如果一切正常,请删除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. 这一次从文件中读取一行,将dirname的值设置为第一个字段,将others设置为每行的其余部分,然后使用给定的目录名运行mkdir

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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