简体   繁体   中英

BASH Script for creating multiple directories, moving files, and then renaming said files

I am trying to make a bash script to create directories with the same name as each file in a given directory, then move said files to their respective directories, and then rename the files.

Basically - a quantum chemistry program that I use requires that the input files be named "ZMAT". So, if I have multiple jobs, I currently need to manually create directories, and then move the ZMAT files into them (can only run one job per folder).

When I run my code, I get "binary operator expected". I am not sure what this means. Some help please.

Here is what I have so far:

#!/bin/bash

if [ -e *.ZMAT ]; 
    then
        echo "CFOUR Job Detected"
        for INPFILE in *.ZMAT; do
            BASENAME=$(basename $INPFILE )
            INPFILE=$BASENAME.ZMAT
            OUTFILE=$BASENAME.out
            XYZFILE=$BASENAME.xyz
            ERRORFILE=$BASENAME.slu
            
            if [ ! -e $ERRORFILE ];
                then
                    # Create folder in scratch directory with the basename
                    mkdir /scratch/CFOUR/$BASENAME
                    # Move the file to its directory
                    mv -f $INPFILE /scratch/CFOUR/$BASENAME
                    # cd to the new directory
                    cd /scratch/CFOUR/$BASENAME
                    # Change the file name to just ZMAT
                    mv -f $INPFILE ZMAT
                    echo "Submitting CFOUR Job"
                    # Submit to scheduler
                    #RUN_COMMAND="sbatch -J $BASENAME _CFOUR_MRCC_SLURM.SUB"
                    #eval $RUN_COMMAND
                else
                    echo "Error File Detected - Not Submitting Job"
            fi
        done
fi

An alternative would be to create symlinks to the original files.

As you said before, each ZMAT symlink would need to be in its own directory.

The upside is that the original data doesn't move, so less risk of breaking it, but the tool you want to use should read the symlinks as if they are the files it is looking for.

This one-liner creates an out directory in the current folder that you could subsequently move wherever you want it. You could easily create it where you do want it by replacing "out" with whatever absolute path you wanted

for i in *.ZMAT; do mkdir -p out/$i ; ln -s $PWD/$i out/$i/ZMAT ; done

I believe I have solved my problem. Here is the new script, which appears to be working fine. Any input is welcome though!

#!/bin/bash

SUBDIR=$(pwd)

for i in *.ZMAT; do
    BASENAME=$(basename $i .ZMAT)
    INPFILE=$BASENAME.ZMAT
    OUTFILE=$BASENAME.out
    XYZFILE=$BASENAME.xyz
    ERRORFILE=$BASENAME.slu
    
    if [ ! -e $ERRORFILE ];
        then
            mkdir /scratch/CFOUR/$BASENAME # Create Scratch Folder
            cp $INPFILE /scratch/cdc/CFOUR/$BASENAME # Move Input to Scratch
            cd /scratch/CFOUR/$BASENAME #cd to Scratch Folder
            mv -f $INPFILE ZMAT # Change Input Name
            echo "Submitting CFOUR Job"
            # Submit to scheduler
            #RUN_COMMAND="sbatch -J $BASENAME _CFOUR_MRCC_SLURM.SUB"
            #eval $RUN_COMMAND
            cd $SUBDIR #Go back to SUBDIR
        else
            echo "Error File Already Exists"
    fi
done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM