Trying to write a MacOS automator script to make incrementing folders based off of scanned input - macos

New to bash scripting, and I'm stuck. Within a static directory I'm trying to create a folder '001_scanned name' and within that directory create 6 more subfolders. I'm able to do it all brutishly with this code:
cd ~/deej/Test/Capture
mkdir "$1"
cd "$1"
mkdir "$1_1"
mkdir "$1_2"
mkdir "$1_3"
mkdir "$1_4"
mkdir "$1_5"
mkdir "$1_6"
Ugly, but works for now.
$1 is the scanned name and I was manually appending the prefix of the file names with "001, 002, etc.". Is there an easy way to do this within an Automator prompt since I'll be unable to keep the last variable stored in the code?

If you use the -p option with mkdir it will create intermediate directories as needed so you can do both commands at once. The seq function can create zero-padded integers:
for n in $(seq -f "%03g" 1 10);
do mkdir -p ${i}/${i}_${n};
done;
You can test using echo instead of mkdir -p. Below I had i set to 015...
015/015_001
015/015_002
015/015_003
015/015_004
015/015_005
015/015_006
015/015_007
015/015_008
015/015_009
015/015_010

Related

Batch create folders from TXT file... with subfolders in them

I have a TXT file with 1,000 lines of product numbers I need to make into 1,000 folders with two subfolders in each named IMAGE & SPEC. I want to run it in Automator or from Terminal on my mac but cannot find the answer ANYWHERE! Any help out there?
In Terminal.app, type “cd “ (without the quotes but make sure there is a space after cd). Then drag the folder from Finder (the folder where you want the 1000 new folders created in) Into Terminal.app. This will change the working directory to the directory to where the new folders will be created.
If your products.txt file is located in the same directory as the 1000 new folders will be created, enter this following line of code into the Terminal window and be sure to change the name “products.txt” to your actual file name.
while read line; do mkdir -p "$line/IMAGE"; mkdir -p "$line/SPEC"; done < products.txt
If your products.txt file is located elsewhere, then use this line of code instead.
while read line; do mkdir -p "$line/IMAGE"; mkdir -p "$line/SPEC"; done <
then drag the “products.txt” file from Finder Into Terminal.app. Then it should look something like this (assuming the “products.txt” file was on you Desktop)
while read line; do mkdir -p "$line/IMAGE"; mkdir -p "$line/SPEC"; done < /Users/YourShortName/Desktop/products.txt
you can use pure shellscript for this task like below:
while read line;
do
mkdir -p "$line/IMAGE";
mkdir -p "$line/SPEC";
done < products.txt

Selectively create folders based on names in bash script

I have several files called as follow:
dosulepin3D_CID_5284550.pdbqt
protriptyline3D_CID_4976.pdbqt
These are small molecules. Now I want to create a file in the Results folder for each one of these molecules, ignoring the 3D_CID_5284550.pdbqt they all have behind, and have folders called:
dosulepin
protriptyline
I want to do this with a for loop, since I'm also performing some functions with these files. This is what I have:
DIR="/home/roy/MolecularDocking/VirtualScreening/Dockings"
cd $DIR
for ligand in ligands/*; do
echo $ligand
mkdir "/home/roy/MolecularDocking/Results/$ligand"
done;
But this obviuosly creates folders with the full name.
Something like this.
DIR="/home/roy/MolecularDocking/VirtualScreening/Dockings"
cd "$DIR" || exit
for ligand in ligands/*.pdbqt; do
echo "$ligand"
echo mkdir -p "/home/roy/MolecularDocking/Results/${ligand%3D*}"
done
Remove the echo from the mkdir if you're satisfied with the output.
See Parameter Expansion for more details.

Unix Bash Script Create Directory parent and child method

I'm trying to create directory using if condition statement, while running script i am not able to find any expected result from the script, but when i am running manually only mkdir command alone its creating as we expected; here the sample code.
#!/bin/bash
dir_path=/tmp/opt/app/software/{A,B,C,D}
if [[ -d $dir_path ]]; then
mkdir -p /tmp/opt/app/software/{A,B,C,D}
fi
can you please advise, how we can create this..
dir_path is a "list" of directory paths due to the {} parameter expansion. If you write this out:
dir_path=/tmp/opt/app/software/A /tmp/opt/app/software/B /tmp/opt/app/software/C /tmp/opt/app/software/D
This is what's being used in the test of the if statement.
Either you want to iterate over the list of sub directories, or just pass them to mkdir. mkdir simply won't create the directory if it already exists.
Your mkdir command actually expands out to:
mkdir -p /tmp/opt/app/software/A /tmp/opt/app/software/B /tmp/opt/app/software/C /tmp/opt/app/software/D
If you want to itterate and still do a check (which while needless in this example can still be useful other times.)
# Declare the variable `dirs` to be an array and use
# parameter expansion to populate it
declare -a dirs=(/tmp/opt/app/software/{A,B,C,D});
# Iterate over the array of directory names
for dir in ${dirs[#]}; do
if [ ! -d "$dir" ]; then
# The directory does not exsist
mkdir -p "$dir"; # Make the directory
fi
done

How to create subfolders and files if not present inside a script without multiple mkdir?

What is a better way to create sub folders in a shell script? Instead of using the following method?
mkdir /var/log
mkdir /var/log/celery
mkdir /var/log/celery/stdout
mkdir /var/log/celery/stderr
touch /var/log/celery/stdout/stdout.log <<< I'm hoping the use this path create folder if doesn't exists....
touch /var/log/celery/stderr/stderr.log
mkdir has a -p flag that will create parent directories but touch will not create directories that do not exist.
That still cuts the above down to:
mkdir -p /var/log/celery/stdout /var/log/celery/stderr
touch /var/log/celery/stdout/stdout.log /var/log/celery/stderr/stderr.log
Which in a shell that supports brace expansion could even be:
mkdir -p /var/log/celery/{stdout,stderr}
touch /var/log/celery/{stdout/stdout.log,stderr/stderr.log}
And actually, if you have brace expansion but not mkdir -p you could do:
mkdir /var/log{,/celery{,/{stdout,stderr}}}
touch /var/log/celery/{stdout/stdout.log,stderr/stderr.log}
But there isn't any way to combine the mkdir and touch steps with standard tools that I'm aware of.
The -p option of mkdir will create the intermediate folders of the path if they don't exists (and of course, if you have the appropriate privileges):
mkdir -p /var/log/celery/stderr
To create the file, you can append the touch after the operator &&, so the touch operation only occurs if the directory either was created successfully or already exists:
mkdir -p /var/log/celery/stderr && touch "$_/stderr.log"
(Basically, the $_ will pass the dir path to the touch command)
UNTESTED:
$ needir () { mkdir -p $1; echo $1; }
$ touch $(needir /var/log/celery/stderr)/stderr.log
and put "needir" in your .profile, or better yet, in a function library on your path that you source when you login. you'd be surprised how often you'll be using it.

Extract folder names and create directory

I have to extract folder names (folder10 folder44 etc) using for loop and make directory using each folder names but I get file names (file1 file12) and I get an error while creating directory i.e "Directory cannot be created". I have to operate on .txt files in my pipeline so I can't skip /home/data/folder*/file* in for loop
How can I get folder names instead of file names
#!/bin/bash
$out_dir=/home/data/results
for file in /home/data/folder*/file*
do
txtFile=${file##*/}
id=${txtFile%.txt}
echo "mkdir -p $out_dir/"${id}""
done
Folders and File structure
/home/data/folder10/file1/file1.txt
/home/data/folder44/file12/file12.txt
/home/data/folder100/file3/file3.txt
/home/data/folder250/file4/file4.txt
/home/data/folder1245/file5/file5.txt
output which I get
mkdir -p /home/data/results/file1
mkdir -p /home/data/results/file12
mkdir -p /home/data/results/file3
expected output will be
mkdir -p /home/data/results/folder10
mkdir -p /home/data/results/folder44
mkdir -p /home/data/results/folder100
This this:
#!/bin/bash
out_dir=/home/data/results
for file in /home/data/folder*/file*.txt; do
folder=${file%/*}
mkdir -p "$out_dir/${folder##*/}"
done
Or this:
#!/bin/bash
out_dir=/home/data/results
for file in /home/data/folder*/file*/file*.txt; do
folder=${file%/*/*}
mkdir -p "$out_dir/${folder##*/}"
done

Resources