LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-06-2014, 10:10 AM   #1
cropper
LQ Newbie
 
Registered: Aug 2014
Posts: 8

Rep: Reputation: Disabled
Can do I create a folder called dicom in 3 separate folders using a for-loop


I have a folder with 3 folders inside and 3 more folders inside the first level as follows:

folder1/0
folder2/0
folder3/0

I need to create a folder called dicom in each of the folders called 0.

So that I end up with:

folder1/0/dicom
folder2/0/dicom
folder3/0/dicom

I tried:

for i in `ls --color=none *`; do mkdir $i/0/dicom; done

Unfortunately, it's not going so well...

[admin@paczilla] {testfolder} for i in `ls -l`; do mkdir $i/0/dicom; done
mkdir: cannot create directory `total/0/dicom': No such file or directory
mkdir: cannot create directory `12/0/dicom': No such file or directory
mkdir: cannot create directory `drwxrwxr-x/0/dicom': No such file or directory
mkdir: cannot create directory `3/0/dicom': No such file or directory
mkdir: cannot create directory `admin/0/dicom': No such file or directory
mkdir: cannot create directory `admin/0/dicom': No such file or directory


Ideally, I'd like a for-loop I can run from the cli as opposed to saving it in a bash script.

Any ideas?
 
Old 08-06-2014, 10:57 AM   #2
Spatior
Member
 
Registered: Jun 2009
Location: México
Distribution: Suse, Debian based, CentOs
Posts: 48

Rep: Reputation: 10
Hi Cropper and welcome.

Well your command fails becuse ls -l shows you many columns that contains acl, owner, groups, date and something else, so you will have to filter the output like this

Code:
for i in `ls -l | grep '^d' | awk -f " " '{print $8}'`; do mkdir $i/0/dicom; done
Just check which is the correct position where the folder name appears, it colud be 9 depending on the way your output is being formated.

Last edited by Spatior; 08-06-2014 at 10:59 AM. Reason: single qoutes on AWK where wrong, changed position
 
Old 08-06-2014, 04:42 PM   #3
jefro
Moderator
 
Registered: Mar 2008
Posts: 15,696

Rep: Reputation: 2265Reputation: 2265Reputation: 2265Reputation: 2265Reputation: 2265Reputation: 2265Reputation: 2265Reputation: 2265Reputation: 2265Reputation: 2265Reputation: 2265
Take your (i) result by using the part starting point of the script for ls -l (not even sure you need that other than to list by line(not long))

Then put the i result into your desired command. mkdir $i/0/dicom

What do you get?
 
Old 08-06-2014, 06:11 PM   #4
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,654

Rep: Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255
Try using "mkdir -p ..." This will create any missing intermediate directories.
 
Old 08-07-2014, 02:53 PM   #5
cropper
LQ Newbie
 
Registered: Aug 2014
Posts: 8

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Spatior View Post
Hi Cropper and welcome.

Well your command fails becuse ls -l shows you many columns that contains acl, owner, groups, date and something else, so you will have to filter the output like this

Code:
for i in `ls -l | grep '^d' | awk -f " " '{print $8}'`; do mkdir $i/0/dicom; done
Just check which is the correct position where the folder name appears, it colud be 9 depending on the way your output is being formated.

Thanks Spatior for the recommendation. It seems logical. So here is what my ls output looks like:

[admin@thehost] {testfolder} ls -lrt
total 12
drwxrwxr-x 3 admin admin 4096 Aug 6 10:06 folder1
drwxrwxr-x 3 admin admin 4096 Aug 6 10:06 folder2
drwxrwxr-x 3 admin admin 4096 Aug 6 10:06 folder3

Based on that, I tried {print $10} but got 2 different results:

First, if I use awk -f, I get an error:


[admin@thehost] {testfolder} for i in `ls -l | grep '^d' | awk -f " " '{print $10}'`; do mkdir $i/0/dicom; done
awk: fatal: can't open source file ` ' for reading (No such file or directory)


Then if I try awk -F, I don't get an error but I don't get my dicom folders either:


[admin@thehost] {testfolder} for i in `ls -l | grep '^d' | awk -F " " '{print $10}'`; do mkdir $i/0/dicom; done
[admin@thehost] {testfolder} ls -lR
.:
total 12
drwxrwxr-x 3 admin admin 4096 Aug 6 10:06 folder1
drwxrwxr-x 3 admin admin 4096 Aug 6 10:06 folder2
drwxrwxr-x 3 admin admin 4096 Aug 6 10:06 folder3

./folder1:
total 4
drwxrwxr-x 2 admin admin 4096 Aug 6 10:06 0

./folder1/0:
total 0

./folder2:
total 4
drwxrwxr-x 2 admin admin 4096 Aug 6 10:06 0

./folder2/0:
total 0

./folder3:
total 4
drwxrwxr-x 2 admin admin 4096 Aug 6 10:06 0

./folder3/0:
total 0


I tried different column numbers 8, 9, 10, 11 but none of them seem to do the trick.
 
Old 08-07-2014, 02:57 PM   #6
cropper
LQ Newbie
 
Registered: Aug 2014
Posts: 8

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by jefro View Post
Take your (i) result by using the part starting point of the script for ls -l (not even sure you need that other than to list by line(not long))

Then put the i result into your desired command. mkdir $i/0/dicom

What do you get?

Sorry, I'm not sure I understand your recommendation. Can you show me a sample command?
 
Old 08-07-2014, 03:01 PM   #7
cropper
LQ Newbie
 
Registered: Aug 2014
Posts: 8

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by jpollard View Post
Try using "mkdir -p ..." This will create any missing intermediate directories.
Well that came closer than what I ever got so far...but not quite. My output created the folders in duplicate (albeit with the dicom folder inside). But I'd like the command
to create the dicom folder inside each folder.

[admin@thehost] {testfolder} ls -lrt
total 28
drwxrwxr-x 3 admin admin 4096 Aug 6 10:06 folder1
drwxrwxr-x 3 admin admin 4096 Aug 6 10:06 folder2
drwxrwxr-x 3 admin admin 4096 Aug 6 10:06 folder3
drwxrwxr-x 3 admin admin 4096 Aug 7 14:56 folder1:
drwxrwxr-x 3 admin admin 4096 Aug 7 14:56 0
drwxrwxr-x 3 admin admin 4096 Aug 7 14:56 folder2:
drwxrwxr-x 3 admin admin 4096 Aug 7 14:56 folder3:


Those folders were created in addition to the existing folders. Although they do have the folder dicom inside each of them.
folder1:
folder2:
folder3:
O
 
Old 08-07-2014, 03:04 PM   #8
cropper
LQ Newbie
 
Registered: Aug 2014
Posts: 8

Original Poster
Rep: Reputation: Disabled
>I'd like the command to create the dicom folder inside each folder.

This should read:

I'd like the command to create the dicom folder inside each "existing" folder.

So currently, my structure is as follows:

[admin@thehost] {0} pwd
/home/admin/testfolder/folder1/0
/home/admin/testfolder/folder2/0
/home/admin/testfolder/folder3/0

The folder dicom has to be created inside each O folder.
 
Old 08-07-2014, 03:07 PM   #9
cropper
LQ Newbie
 
Registered: Aug 2014
Posts: 8

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by cropper View Post
>I'd like the command to create the dicom folder inside each folder.

This should read:

I'd like the command to create the dicom folder inside each "existing" folder.

So currently, my structure is as follows:

[admin@thehost] {0} pwd
/home/admin/testfolder/folder1/0
/home/admin/testfolder/folder2/0
/home/admin/testfolder/folder3/0

The folder dicom has to be created inside each O folder.

My command needs to be run from:

[admin@paczilla] {testfolder} pwd
/home/admin/testfolder

Naturally, the example I'm giving of folder1, folder2, folder3 is a simplification of my actual task. My actual task is made up of over 50 folders and inside each I have to create the folder dicom. Doing it manually is simply too long.
 
Old 08-07-2014, 05:16 PM   #10
Spatior
Member
 
Registered: Jun 2009
Location: México
Distribution: Suse, Debian based, CentOs
Posts: 48

Rep: Reputation: 10
Quote:
Originally Posted by cropper View Post
[admin@thehost] {testfolder} ls -lrt
total 12
drwxrwxr-x 3 admin admin 4096 Aug 6 10:06 folder1
drwxrwxr-x 3 admin admin 4096 Aug 6 10:06 folder2
drwxrwxr-x 3 admin admin 4096 Aug 6 10:06 folder3

Based on that, I tried {print $10} but got 2 different results:

First, if I use awk -f, I get an error:
I'm sorry, you are right, the comand should use the -F modificator for AWK and should use the -p for the MKDIR

As for the position of the folder should be 9, this si because the -F tell awk to use the next parameter as FIELD SEPARATOR (FS) ( i.e. blank space) and your output have 9 fileds if you use that FS.

so here it goes again

Code:
for i in `ls -l | grep '^d' | awk -F " " '{print $9}'`; do mkdir -p $i/0/dicom; done
now this is all assuming that you have a paretn folder (like the one you mention: /home/admin/testfolder/) and all the target folders are inside this parent. I hope my understaing is correct.
 
Old 08-07-2014, 05:50 PM   #11
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,654

Rep: Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255
You could simplify using find rather than ls (which is awkward in a number of ways- blanks in names, doesn't always list the files, special characters in names...

I think "find * -maxdepth 0 -type d " will list each directory name name on a line (the -maxdepth 0 prevents it from waling the directory tree, the -type d causes find to only list directory names.. when used with the find option '-exec mkdir -p "{}/0/dicom" ' it will execute mkdir on each directory where the {} is the name identified by find.

The full command should be
Code:
find * -maxdepth 0 -type d -exec mkdir -p "{}/0/dicom" \;
 
Old 08-08-2014, 07:42 AM   #12
cropper
LQ Newbie
 
Registered: Aug 2014
Posts: 8

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Spatior View Post
I'm sorry, you are right, the comand should use the -F modificator for AWK and should use the -p for the MKDIR

As for the position of the folder should be 9, this si because the -F tell awk to use the next parameter as FIELD SEPARATOR (FS) ( i.e. blank space) and your output have 9 fileds if you use that FS.

so here it goes again

Code:
for i in `ls -l | grep '^d' | awk -F " " '{print $9}'`; do mkdir -p $i/0/dicom; done
now this is all assuming that you have a paretn folder (like the one you mention: /home/admin/testfolder/) and all the target folders are inside this parent. I hope my understaing is correct.


Hi Spatior,
Yes your assumption is correct. My parent structure if from /home/admin and I'm running the command from inside testfolder.

The result of running the command is as follows:

[admin@paczilla] {testfolder1} for i in `ls -l | grep '^d' | awk -F " " '{print $9}'`; do mkdir -p $i/0/dicom; done
[admin@paczilla] {testfolder1} ls -lR
.:
total 24
drwxrwxr-x 3 admin admin 4096 Aug 8 07:35 ?[01;34mfolder2?[0m
drwxrwxr-x 3 admin admin 4096 Aug 8 07:35 ?[01;34mfolder3?[0m
drwxrwxr-x 3 admin admin 4096 Aug 8 07:35 ?[0m?[01;34mfolder1?[0m
drwxrwxr-x 3 admin admin 4096 Aug 7 15:23 folder1
drwxrwxr-x 3 admin admin 4096 Aug 7 15:23 folder2
drwxrwxr-x 3 admin admin 4096 Aug 7 15:23 folder3


A dicom folder gets created in the first 3 folders and folder1, folder2, folder3 gets ignored.
By the way, I've been researching this dilemma for several weeks. It's a challenging problem.
 
Old 08-08-2014, 07:49 AM   #13
cropper
LQ Newbie
 
Registered: Aug 2014
Posts: 8

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by jpollard View Post
You could simplify using find rather than ls (which is awkward in a number of ways- blanks in names, doesn't always list the files, special characters in names...

I think "find * -maxdepth 0 -type d " will list each directory name name on a line (the -maxdepth 0 prevents it from waling the directory tree, the -type d causes find to only list directory names.. when used with the find option '-exec mkdir -p "{}/0/dicom" ' it will execute mkdir on each directory where the {} is the name identified by find.

The full command should be
Code:
find * -maxdepth 0 -type d -exec mkdir -p "{}/0/dicom" \;


Hi jpollard:

Your command worked! I had tried several variations of the find command but hadn't taken into account the depth pararmeter. I assumed a for-loop was the only way. This method of achieving my objective will do just fine. A for-loop, in that case, will not be necessary.

Thank-you very to all who considered this problem. I appreciate your time.



[admin@paczilla] {testfolder1} find * -maxdepth 0 -type d -exec mkdir -p "{}/0/dicom" \;
[admin@paczilla] {testfolder1} ls -l
total 12
drwxrwxr-x 3 admin admin 4096 Aug 8 07:37 folder1
drwxrwxr-x 3 admin admin 4096 Aug 8 07:37 folder2
drwxrwxr-x 3 admin admin 4096 Aug 8 07:37 folder3
[admin@paczilla] {testfolder1} ls -lR
.:
total 12
drwxrwxr-x 3 admin admin 4096 Aug 8 07:37 folder1
drwxrwxr-x 3 admin admin 4096 Aug 8 07:37 folder2
drwxrwxr-x 3 admin admin 4096 Aug 8 07:37 folder3

./folder1:
total 4
drwxrwxr-x 3 admin admin 4096 Aug 8 07:43 0

./folder1/0:
total 4
drwxrwxr-x 2 admin admin 4096 Aug 8 07:43 dicom

./folder1/0/dicom:
total 0

./folder2:
total 4
drwxrwxr-x 3 admin admin 4096 Aug 8 07:43 0

./folder2/0:
total 4
drwxrwxr-x 2 admin admin 4096 Aug 8 07:43 dicom

./folder2/0/dicom:
total 0

./folder3:
total 4
drwxrwxr-x 3 admin admin 4096 Aug 8 07:43 0

./folder3/0:
total 4
drwxrwxr-x 2 admin admin 4096 Aug 8 07:43 dicom

./folder3/0/dicom:
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to create new folders on P2 & P3 and how to mount /home dir on a folder on P3? AnApproach2DigestLinux Linux - Newbie 2 12-07-2012 03:38 AM
bash script to create folders including making recursive folders.... linux-bandit Linux - Software 8 11-28-2009 02:50 AM
Cannot create folders with bash script called from php keyF Linux - Software 4 06-25-2006 11:58 AM
Samba can create new files and folders but access denied in any new folders k.king Linux - Networking 2 01-15-2006 07:14 AM


All times are GMT -5. The time now is 09:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration