1

How to Make Directories with the Same Parent Name?

In my library, I have the following layout:

DataFolder
   PATIENT A
          FOLDER1
          FOLDER2
          FOLDER# 
   PATIENT B
          FOLDER1
          FOLDER2
          FOLDER# 

In each of the Folders I want to Make a subfolder with the Patient name like xxPatient A etc... then within THOSE folders, I want to make two more folders named "TypeX" and "Typez"

How can I do this? This is what I have so far...


cd /mnt/d/DataFolder
for i in * 
do
cd ${i}

for j in FOLDER*
do 
cd ${j}

mkdir 


cd ../
done 

cd ../
done 

This is what I want in the end:


DataFolder
   PATIENT A
      FOLDER1
        xxPATIENTA
          TypeX
          TypeZ
      FOLDER2
        xxPATIENTA
          TypeX
          TypeZ
   PATIENT B
      FOLDER1
        xxPATIENTB
          TypeX
          TypeZ
      FOLDER2
        xxPATIENTB
          TypeX
          TypeZ
      FOLDER# 
        xxPATIENTB
          TypeX
          TypeZ
6
  • It's not quite clear what you want. You mentioned you want to create folders named "TypeX" and "Typez". However, your example shows "FOLDER1", "FOLDER2", etc. Are you saying you want to create something ~inside~ FOLDER1, and FOLDER2, and FOLDER3, etc... ?
    – PJ Singh
    Mar 30 at 21:18
  • I updated the question, thank you PJ Singh Mar 30 at 21:25
  • One more question. Do the directories immediately under DataFolder (i.e. PATIENT A, etc.) already exist, or do you need to create those as well? If so, do the directories named "FOLDER1", "FOLDER2", "FOLDER#" already exist, or do you need to create those?
    – PJ Singh
    Mar 30 at 21:29
  • Thank you and yes, DataFolder, Patient A, and Folder # all already exist Mar 30 at 21:45
  • Would it suit you to have a "template" folder with all the sub-directories but no foles, and to just copy that folder, renaming it each time? Mar 30 at 21:49
2

Starting with a structure like this...

DataFolder
├── PATIENT A
│   ├── FOLDER1
│   └── FOLDER2
└── PATIENT B
    ├── FOLDER1
    ├── FOLDER2
    └── FOLDER3

Use the following bash script...

SAVEIFS=$IFS; IFS=$(echo -en "\n\b");
for patient_name in `ls -1 DataFolder`; do
    echo "Processing ${patient_name}"
    for sub_folder_name in `ls -1 DataFolder/${patient_name}`; do
        echo "    Processing ${sub_folder_name}"
        mkdir -p "DataFolder/${patient_name}/${sub_folder_name}/xx${patient_name}/"{"TypeX","TypeY"}
    done
done
IFS=$SAVEIFS

...to create a folder structure like this...

DataFolder
├── PATIENT A
│   ├── FOLDER1
│   │   └── xxPATIENT A
│   │       ├── TypeX
│   │       └── TypeY
│   └── FOLDER2
│       └── xxPATIENT A
│           ├── TypeX
│           └── TypeY
└── PATIENT B
    ├── FOLDER1
    │   └── xxPATIENT B
    │       ├── TypeX
    │       └── TypeY
    ├── FOLDER2
    │   └── xxPATIENT B
    │       ├── TypeX
    │       └── TypeY
    └── FOLDER3
        └── xxPATIENT B
            ├── TypeX
            └── TypeY

Explanation:

  • There are two loops in this script.
  • The first loop lists all the existing patient folders (PATIENT A, PATIENT B, etc.) in DataFolder.
  • The second loop lists all of the existing folders (FOLDER1, FOLDER2, FOLDER#) within each patient folder.
  • The mkdir command creates the folder structure you want. It uses the -p flag to create all parent folders in the supplied path argument.
  • At the end of the path argument to the mkdir command is {"TypeX","TypeY"}. This tells bash that you want to create two folders, named "TypeX" and "TypeY" at the same location.
  • Before starting the script, you change the IFS (Internal Field Separator) to only break lines on new lines, not spaces. This is important because you have spaces in your file names (for example "PATIENT A"), and you want to loop through each file name, not each word in each file name.
  • You also save a copy of the current IFS so you can restore it after the script is finished.
0
while IFS=/ read -d '' -r a b c; do
    mkdir -p "$a/$b/$c/xx${b// /}"/Type{X,Y}
done < <(shopt -s failglob; printf %s\\0 DataFolder/*/*)
1
  • This would be much more useful if you added an explanation what each part accomplishes.
    – guntbert
    Apr 7 at 16:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.