Feed on
Posts
Comments

a simple task you might want to do in a shell script is perform some operation on every file in a directory.

here’s the shell of code to do that:

#!/bin/sh

# iterate through all sequence files in this directory
for phy_file in `find $1 -name “*.phy”`
do
echo $phy_file;
done

here, i’m stepping through and printing out every file that ends with the extension “*.phy”.  i’m specifying the directory via a command-line argument ($1).  this code would be called in the following way:

$ your_script_name.sh /directory_to_search/


Bookmark and Share

if that was helpful ...

check out the other tips and tricks i've compiled on these pages. you might learn something else interesting!

2 Responses to “iterate through all the files in a directory”

  1. on 16 Jul 2012 at 6:54 am Rob Stewart

    Your command does more than you think. Using find(1) means that you’ll search any subdirectories, too, unless you specify search depth.

    What you really want is this simpler approach:

    for file in *.phy; do
    echo $file
    done

  2. on 16 Jul 2012 at 6:55 am Rob Stewart

    Oh, I didn’t include the $1 for the directory:

    for file in $1/*.phy; do
    echo $file
    done

Did I get this wrong? Let me know!

Trackback URI | Comments RSS