How to create a long list of directories with mkdir?

Hi...

Thanks to read this...

I want to use mkdir to create many directories listed in a text file, let's say.

How do I do this?

Sorry for this maybe very basic question :slight_smile:

care to share a sample content of the "text file"?

Yes :slight_smile:
Not a big list, here, but just for you to understand...

ms_ww_546
ms_rrL_99999
ms_nnn_67_756675

Okay, so for just three entries or many more, a simple loop should suffice.

Question: Are the underscores part of the name, or are they markers for the next directory, i.e. do you want 1_2_3 as you have in the file or 1/2/3?

What have you tried to use as a loop so far? It would be better for you to try and we can make suggestions or corrections, so you can support it/re-use it.

Kind regards,
Robin

Underscores are part of the name.
I didn't try anything :cool:
My idea was to call the content of the file as arguments of the mkdir function. But maybe your loop idea is better.
Don't know how to do it.
Should I read "Bash for dummies"?

For a not too long list, and no whitespace in the directory names, how about

mkdir $(paste -sd' ' file)

How about:

xargs mkdir < inputfile
1 Like
xargs mkdir < inputfile

works very well when text in file is

ms_ww_546 ms_rrL_99999 ms_nnn_67_756675

and not

ms_ww_546
ms_rrL_99999
ms_nnn_67_756675
mkdir $(paste -sd' ' file)

returns an error:

bash syntax error near unexpected token `('

Thanks a lot you both!

That's why it's always good to post OS and shell versions. Check your man bash for "command substitution".

It works perfectly fine with both. Try it.

Hi setub,

Welcome to the forum and the beautiful world of shell scripting. :slight_smile:
It is always a nice idea, though, to try and provide a sample snippet from your code so that our fellow advisers and coaches can get a better understanding. Plus, if there are any more enhancements or better versions for your code, they can suggest that as well.

Do let us know if your problem was solved.

Cheers,
Adwait.

That is rather astonishing, because for me that works.

Anyway, you could always fall back to a basic loop. Not quite as elegant, but does the work:

while read DIR ; do
     mkdir "$DIR"
     # mkdir "/some/start/dir/${DIR}"    # eventually this instead
done < /your/input/file

Notice, though, that this will fail if you have several names on one line, because they will be treated as ONE name. A line of "one two three" will not create three directories "one", "two", "three" but one directory "one two three" (yes, with the blanks as part of the name).

I hope this helps.

bakunin

To bakunin and corona688:

My result when text in file is

ms_ww_546
ms_rrL_99999
ms_nnn_67_756675

is

https://www.unix.com/C:\Users\Fejoz\Desktop\ttt.jpg

I hope you can see the picture. There is like a "whitespace character" after 2 of the 3 created directories.

---------- Post updated at 03:47 AM ---------- Previous update was at 03:44 AM ----------

This is the picture:

https://www.unix.com/data:image/png;base64,iVBORw .... (spam)

---------- Post updated at 03:55 AM ---------- Previous update was at 03:47 AM ----------

Sorry that the picture does not appear.
Anyway, the first text option

ms_ww_546 ms_rrL_99999 ms_nnn_67_756675

is ok for me. I can manage to get the text like this.
Very interested also in the loop option, but I think I need to learn the language first before being able to use it.
Thanks to all! :slight_smile:

The internet does not work that way. Sending someone else a link to "c:\users\fejoz\desktop\ttt.jpg" does not make them see your computer. You can't dump it in 'image:' contents either, not on a bbpost, only a webpage. Look for the 'attachment' button next time you want to send an image here.

But given a c:\ link, I can guess what that "whitespace character" was. You've been editing scripts and data files in Microsoft Notepad, which fills them with garbage: carriage returns. Windows ends lines on \r\n, Linux just uses \n, so the \r becomes garbage at the end of every line. xargs did exactly what you told it to. So will most other methods unless you explicitly exclude carriage returns from names somehow. Garbage in, garbage out.

To remove carriage returns from your files:

tr -d '\r' < inputfile > outputfile

inputfile and outputfile must not be the same.

1 Like