LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Can do I create a folder called dicom in 3 separate folders using a for-loop (https://www.linuxquestions.org/questions/linux-newbie-8/can-do-i-create-a-folder-called-dicom-in-3-separate-folders-using-a-for-loop-4175513718/)

cropper 08-06-2014 09:10 AM

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?

Spatior 08-06-2014 09:57 AM

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.

jefro 08-06-2014 03:42 PM

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?

jpollard 08-06-2014 05:11 PM

Try using "mkdir -p ..." This will create any missing intermediate directories.

cropper 08-07-2014 01:53 PM

Quote:

Originally Posted by Spatior (Post 5216252)
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.

cropper 08-07-2014 01:57 PM

Quote:

Originally Posted by jefro (Post 5216440)
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?

cropper 08-07-2014 02:01 PM

Quote:

Originally Posted by jpollard (Post 5216484)
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

cropper 08-07-2014 02:04 PM

>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.

cropper 08-07-2014 02:07 PM

Quote:

Originally Posted by cropper (Post 5217063)
>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.

Spatior 08-07-2014 04:16 PM

Quote:

Originally Posted by cropper (Post 5217055)
[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.

jpollard 08-07-2014 04:50 PM

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" \;

cropper 08-08-2014 06:42 AM

Quote:

Originally Posted by Spatior (Post 5217125)
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.

cropper 08-08-2014 06:49 AM

Quote:

Originally Posted by jpollard (Post 5217154)
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:


All times are GMT -5. The time now is 03:43 AM.