DISCLAIMER: I am using Mac OSX. The bash script is in a different directory from the files that require processing. From here my struggle begins…

HELP 1: I have multiple files each housed in a subdirectory of a parent directory that needs to be fed as input files for my bioinformatics tool. I found that the for loop would work for this.

for file in ~/Desktop/parent\ folder/*/*.txt;
do exTool INPUT= ${file##*/} OUTPUT= ${file%.txt}_exd.txt 

I am hoping to input the file (by cutting out the folder listing attached to $file) and then output a file, removing '.txt' and adding _exd.txt. The error I get is that the input is carrying the folder list and specifically the whitespace 'parent\ folder' is causing the wrong input info.

HELP 2: As an extension of the first case, if I want to output the file into the same subdirectory as the input, do I have to add a special command?

HELP 3: If I there are 2 files within each subdirectory (input_1.txt and input_2.txt) and I would like to input both into a single command. How would the script look if nested in a for loop?

for file in ~/Desktop/parent\ folder/*/*.txt;
do bioTool INPUT1= ${file%.txt}_1.txt INPUT2= ${file%.txt}_2.txt OUTPUT= ${file%%.*}

For the output, notice that I am only needing the base filename as the tool will append a number at the end of the name to denote 1st and 2nd file output and the extension. For some reason, I feel like the section after 'in' in the for loop is being done incorrectly. It was a knee-jerk solution for having the script in a directory different from the files.

Any help will be greatly appreciated, upvoted, liked, accepted, favorited. Thank you very much for your help.

closed as too broad by muru, Jeff Schaller, Stephen Kitt, cuonglm, Jakuje Feb 3 '16 at 15:15

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    One question per post, please. Move the unrelated stuff like your supervisor's script to another post. – muru Feb 3 '16 at 13:30
  • Sorry, my earlier question (which I just deleted) didn't make sense because I misunderstood the path handling. – Hauke Laging Feb 3 '16 at 14:06
  • Related, or the same? unix.stackexchange.com/questions/259602/… – roaima Feb 3 '16 at 14:35
  • @roaima Related but I was told to make a new post as the questions did not relate. – ozarka Feb 3 '16 at 14:50


Due to the whitespace you need quoting:

INPUT= "${file##*/}" OUTPUT= "${file%.txt}"_exd.txt

It seems a bit strange, though, that there is a space between INPUT= and the file name.


If you have a file input_1.txt then ${file%.txt}_1.txt becomes input_1_1.txt.

I would match the exact file name:

for file in ~/Desktop/parent\ folder/*/input_1.txt; do
    bioTool INPUT1= "${file}" INPUT2= "${file%_1.txt}_2.txt" OUTPUT= "${file%%.*}"


That is no shell stuff at all.

changing into subdirectories

for file in ~/Desktop/parent\ folder/*/input_1.txt; do
    pushd "$subdir" &>/dev/null
    bioTool INPUT1= "${file}" INPUT2= "${file%_1.txt}_2.txt" OUTPUT= "${file%%.*}"
    popd &>/dev/null

Not the answer you're looking for? Browse other questions tagged or ask your own question.