简体   繁体   English

BASH 创建多个目录,移动文件,然后重命名所述文件的脚本

[英]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.我正在尝试制作 bash 脚本来创建与给定目录中的每个文件同名的目录,然后将所述文件移动到各自的目录,然后重命名文件。

Basically - a quantum chemistry program that I use requires that the input files be named "ZMAT".基本上 - 我使用的量子化学程序要求输入文件命名为“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).所以,如果我有多个作业,我目前需要手动创建目录,然后将 ZMAT 文件移动到其中(每个文件夹只能运行一个作业)。

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.正如您之前所说,每个 ZMAT 符号链接都需要位于其自己的目录中。

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.这个单线在当前文件夹中创建了一个 out 目录,您可以随后将其移动到任何您想要的位置。 You could easily create it where you do want it by replacing "out" with whatever absolute path you wanted您可以通过将“out”替换为您想要的任何绝对路径来轻松地在您想要的位置创建它

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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