简体   繁体   中英

Creating directories from list preserving whitespaces

I have list of names in a file that I need to create directories from. The list looks like

Ada Lovelace
Jean Bartik
Leah Culver

I need the folders to be the exact same, preserving the whitespace(s). But with

awk '{print $0}' myfile | xargs mkdir

I create separate folders for each word

Ada
Lovelace
Jean
Bartik
Leah
Culver

Same happens with

 awk '{print $1 " " $2}' myfile | xargs mkdir

Where is the error?

Using gnu xargs you can use -d option to set delimiter as \\n only. This way you can avoid awk also.

xargs -d '\n' mkdir -p < file

If you don't have gnu xargs then you can use tr to convert all \\n to \\0 first:

tr '\n' '\0' < file | xargs -0 mkdir

@birgit:尝试:完全基于您提供的示例Input_file。

awk -vs1="\"" 'BEGIN{printf "mkdir  ";}{printf("%s%s%s ",s1,$0,s1);} END{print ""}'   Input_file | sh
awk '{ system ( sprintf( "mkdir \"%s\"", $0)) }' YourFile
# OR
awk '{ print"mkdir "\"" $0 "\"" | "/bin/sh" }' YourFile
# OR for 1 subshell
awk '{ Cmd = sprintf( "%s%smkdir \"%s\"", Cmd, (NR==1?"":"\n"), $0) } END { system ( Cmd ) }' YourFile 
  • Last version is better due to creation of only 1 subshell.
  • If there are a huge amount of folder (shell parameter limitation), you could loop and create smaller command several times

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