going through directories in bash within for loops - bash

I have a directory like this figure:
I have written a bash script that first goes to steady subdirectory, reads each folder inside it and performs tasks. Then it goes to transient subdirectory, loops over folders there and needs to map results through an openFOAM command that requires similar subfolder from other subdirectory (like mapping from steady/1 to transient/1). Both steady and transient folders are under same parent directory.
Basically, I want to find a way in the second for-loop to map from
"scale/steady/1" to "scale/transient/1" and the same for other subfolder (i.e. 2).
I know that I have to replace "1" in the second for loop with a varible .
Would some body assist me how I can do so?
for d in steady/*/
do
echo "$d"
(cd "$d" && touch steady.log&& simpleFoam > steady.log)
done
for i in transient/*/
do
echo "$i"
(cd $i && touch transient.log && mapFields ../../steady/1 -consistent)
done

for steady_folder in steady/*
do
transient_folder="${steady_folder/steady/transient}"
echo "$steady_folder"
.....
echo "$transient_folder"
.....
done

Related

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.

How to make folders for individual files within a directory via bash script?

So I've got a movie collection that's dumped into a single folder (I know, bad practice in retrospect.) I want to organize things a bit so I can use Radarr to grab all the appropriate metadata, but I need all the individual files in their own folders. I created the script below to try and automate the process a bit, but I get the following error.
Script
#! /bin/bash
for f in /the/path/to/files/* ;
do
[[ -d $f ]] && continue
mkdir "${f%.*}"
mv "$f" "${f%.*}"
done
EDIT
So I've now run the script through Shellcheck.net per the suggestion of Benjamin W. It doesn't throw any errors according to the site, though I still get the same errors when I try running the command.
EDIT 2*
No errors now, but the script does nothing when executed.
Assignments are evaluated only once, and not whenever the variable being assigned to is used, which I think is what your script assumes.
You could use a loop like this:
for f in /path/to/all/the/movie/files/*; do
mkdir "${f%.*}"
mv "$f" "${f%.*}"
done
This uses parameter expansion instead of cut to get rid of the file extension.

Nested for loops for directory in bash script

for d in dirlist;
do
cd $d
echo $PWD "primul"
for p in *.HEIC;
do
echo $PWD "din al doilea"
if [ -e "COPY ${p%.*}.jpg" ]; then
echo "COPY ${p%.*}.jpg exists"
else
convert $p "COPY ${p%.*}.jpg"
echo "COPY ${p%.*}.jpg created"
fi
done
done
I have written a script to convert all files that are .HEIC to .jpg. Now I have a structure of folders that is
/main/2019/01
/main/2019/03
/main/2018/12
/main/2018/11
/main/2018/10
/main/2018/09
And I want to run the second for in each directory.
What I get when I run the commands below is only a list of directories and a mention that there are no files to convert.
How do I change the directory using the first for and run the second for in that directory?
cd - changes to the previous directory. That one solution to put it after the first done (second for cycle).
pushd and popd using stack to handle directory changes
check out: https://www.gnu.org/software/bash/manual/html_node/Directory-Stack-Builtins.html

Bash - Moving files from subdirectories

I am relatively new to bash scripting.
I need to create a script that will loop through a series of directories, go into subdirectories with a certain name, and then move their file contents into a common folder for all of the files.
My code so far is this:
#!/bin/bash
#used to gather usable pdb files
mkdir -p usable_pdbFiles
#loop through directories in "pdb" folder
for pdbDirectory in */
do
#go into usable_* directory
for innerDirectory in usable_*/
do
if [ -d "$innerDirectory" ] ; then
for file in *.ent
do
mv $file ../../usable_pdbFiles
done < $file
fi
done < $innerDirectory
done
exit 0
Currently I get
usable_Gather.sh: line 7: $innerDirectory: ambiguous redirect
when I try and run the script.
Any help would be appreciated!
The redirections < $innerDirectory and < $file are invalid and this is causing the problem. You don't need to use a loop for this, you can instead rely on the shell's filename expansion and use mv directly:
mkdir -p usable_pdbFiles
mv */usable_*/*.ent usable_pdbFiles
Bear in mind that this solution, and the loop based one that you are working on, will overwrite files with the same name in the destination directory.

Bash If then that reads a list in a file condition

Here is the condition:
I have a file with all packages installed.
I have a folder with all kinds of other packages, but they include all of the ones in the list, plus more.
I need a bash script that will read the file and check a folder for packages that don't exist in the list then remove them, they are not needed, but keep the packages that are on the list in that folder.
Or perhaps the bash should read folder then if packages in the folder aren't on the list them rm -f that or those packages.
I am familiar with writing if then conditional statements, I just don't know how to do if making the items in the list a variable or variables (in a loop).
thanks!
I would move the packages on the list to a new folder, delete the original folder, and move the temporary folder back:
DIR=directory-name
mkdir "$DIR-tmp"
while read pkgname; do
if [[ -f "$DIR/$pkgname" ]]; then
mv "$DIR/$pkgname" "$DIR-tmp"
fi
done < package-list.txt
# Confirm $DIR-tmp has the files you want first!
rm -rf "$DIR"
mv "$DIR-tmp" "$DIR"
I think you want something like this:
for file in $(ls folder) ; do
grep -E "$file" install-list-file >/dev/null || \
echo $file
done > rm-list
vi rm-list # view file to ensure correct
rm $(<rm_list)
There are ways to make this faster (using parameter substitution to avoid fork/exec's), but I recommend avoiding fancy shell stuff [${file##*/}] until you've got the basics down. Also, this script basically translates the description into a script and is not intended to be much more than a guide on how to approach the problem.

Resources