Looping through directories and running .txt files into a program

Posted on 2011-10-24
Last Modified: 2013-12-26

I currently have .txt files saved in different folders in one directory.

I'm now trying to create a shell script which will run through my directory, go through all the folders and if there is a text file containing the word "FASTA" (all capitals) in the directory, it will run the following code:

formatdb -i input_textfilenameFASTA.txt -n output_nameDATABASE

The program turns the textfile into a database and creates three files: .phr , .psq and .pin

Therefore, say the shell script opens the first folder and comes across a text file with FASTA in the name:


therefore, the script would do the following:

formatdb -i file1FASTA.txt -n file1DATABASE

it would then put the database files in the same directory as the text file. If there are any more text files in the directory, it would then do the same with these. If there are no more files in the directory it would then move on to the next directory.

Would it be possible to achieve this through Shell scripting?


Question by:StephenMcGowan
    LVL 31

    Expert Comment

    So there are multiple parts to your problem.

    There is a command line parsing.  I am not sure you know how to use getopts or not.  That would take care of that part like -i -n.

    Suppose you have file in a directory and you cd to that directory then you can iterate through those files like

    for file in *
        if [[ $file =~ pin$ ]]
             echo "$file with .pin extension"
    LVL 48

    Accepted Solution


    for file in $(find /some/dir -name "*FASTA.txt")
       echo "Processing $file"
       formatdb -i $file -n ${file/FASTA.txt/DATABASE}

    Open in new window


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now