How to create a long list of directories with mkdir?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to create a long list of directories with mkdir?
# 8  
Old 11-22-2017
Code:
xargs mkdir < inputfile

works very well when text in file is
Code:
ms_ww_546 ms_rrL_99999 ms_nnn_67_756675

and not
Code:
ms_ww_546
ms_rrL_99999
ms_nnn_67_756675

Code:
mkdir $(paste -sd' ' file)

returns an error:
Code:
bash syntax error near unexpected token `('

Thanks a lot you both!
# 9  
Old 11-22-2017
That's why it's always good to post OS and shell versions. Check your man bash for "command substitution".
# 10  
Old 11-22-2017
Quote:
Originally Posted by setub
Code:
xargs mkdir < inputfile

works very well when text in file is
Code:
ms_ww_546 ms_rrL_99999 ms_nnn_67_756675

and not
Code:
ms_ww_546
ms_rrL_99999
ms_nnn_67_756675

It works perfectly fine with both. Try it.
# 11  
Old 11-22-2017
Hi setub,

Welcome to the forum and the beautiful world of shell scripting. Smilie
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.
# 12  
Old 11-22-2017
Quote:
Originally Posted by setub
Code:
xargs mkdir < inputfile

works very well when text in file is
Code:
ms_ww_546 ms_rrL_99999 ms_nnn_67_756675

and not
Code:
ms_ww_546
ms_rrL_99999
ms_nnn_67_756675

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:

Code:
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
# 13  
Old 11-23-2017
To bakunin and corona688:

My result when text in file is
Code:
ms_ww_546
ms_rrL_99999
ms_nnn_67_756675

is

Moderator's Comments:
Mod Comment Neo: here is the code in this attachment - this is spam which should be deleted


Code:
[IMG]https://www.unix.com/C:\Users\Fejoz\Desktop\ttt.jpg[/IMG]

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:

Moderator's Comments:
Mod Comment Neo: deleted post of the spam below ...


Code:
[IMG]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
Code:
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! Smilie
# 14  
Old 11-23-2017
Quote:
Originally Posted by setub
To bakunin and corona688:

My result when text in file is
Code:
ms_ww_546
ms_rrL_99999
ms_nnn_67_756675

is

Image

I hope you can see the picture. There is like a "whitespace character" after 2 of the 3 created directories.
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:

Code:
tr -d '\r' < inputfile > outputfile

inputfile and outputfile must not be the same.

Last edited by Corona688; 11-23-2017 at 11:30 AM..
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Switching between directories and mkdir/copy dir/file

I was trying to copy the files inside the path /home/user/check/Q1/dir/folder1/expected/n/a1.out1 and a1.out2 and a1.out3 to /home/user/check/Q2/dir/folder1/expected/n/ if n directory is not present at Q2/dir/folder1/expected/ then directory should be created first. And, script follow the... (5 Replies)
Discussion started by: Mannu2525
5 Replies

2. Shell Programming and Scripting

Copy of "How to create a long list of directories with mkdir?"

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... (0 Replies)
Discussion started by: setub
0 Replies

3. Shell Programming and Scripting

Scan directories and create a list of files

Gents, Please can you help. I want to create a list which contends the complete patch of the location of some directories with the size of each file. need to select only .txt file In this case I am try to find the subdirectories tp1 and tp2 and create the output list. jd175-1 tp1... (3 Replies)
Discussion started by: jiam912
3 Replies

4. Shell Programming and Scripting

Make directories containing subdirs using mkdir

Hello everybody, I wonder if there's a way to make a directory that contains multiple subdirectories. For example, if I want to make /home/student under the current working directory, how do I do it? Can I do it using a single mkdir command or do I have make home first, cd into it and then make... (1 Reply)
Discussion started by: Yongfeng
1 Replies

5. UNIX for Dummies Questions & Answers

Mkdir a/b/c # where a/b does not exists. is it possible to create it ?

Is is possible to create the directories in following manner. for example my home dir is empty and i want to create dir a/b/c mkdir a/b/c # where a/b does not exists. (5 Replies)
Discussion started by: anandgodse
5 Replies

6. Shell Programming and Scripting

Need Help Moving Long List Of Files Into Directories

I am very new to BASH and I am having difficulties moving a long list of image files into similarly named directories. I've been trying to come with a script all night and no luck. Here is what my list of files looks like: DSC_0059_01.jpg DSC_0059_02.jpg DSC_0059_03.jpg DSC_0059_04.jpg... (5 Replies)
Discussion started by: jowens1138
5 Replies

7. Shell Programming and Scripting

Create unique tar archives from a list of directories

I'm looking to archive a client directory from a CIFS share There are multiple directories that will be stored in a text file and I'm looking to create an individual tar archive of each folder in the directory. I've tried a number of commands to no avail. Here's what I would like. ... (2 Replies)
Discussion started by: Steelysteel
2 Replies

8. UNIX and Linux Applications

mkdir: cannot create directory

Hi, I have network mount on two servers. One server I can create any directories without any issues, other server with the similar mount, I am not able to create directories starting with number! Creation, name start with a number: $ mkdir 1212 mkdir: cannot create directory `1212': No such... (12 Replies)
Discussion started by: ./hari.sh
12 Replies

9. AIX

mkdir: A file or path name is too long

Hi, I have an AIX machine. I am trying to create a directory from within the script, but the message being shown is "mkdir: 0653-358 Cannot create directory 'xxx': A file or path name is too long" I am not giving any 'long' pathname to mkdir. The commands being issued within the script are: ... (0 Replies)
Discussion started by: greenhorn007
0 Replies
Login or Register to Ask a Question