klex52s
klex52s

Reputation: 437

For Loop Over Files to Perform ncdump?

Can't seem to find a solid similar question. In basic terms, I'm just trying to perform an action to multiple files but I struggle with for loops. I want to ncdump multiple files at once and store the output in separate files. This is what I have so far.

#!/bin/bash

date='20160503'

dump1Dir=/server1/applications/VAL/gran_files

cd $dump1Dir
filelist=`ls *s$date*`

for i in $filelist do
ncdump $filelist > dumpfile[i]
done

Upvotes: 0

Views: 573

Answers (2)

adesilva
adesilva

Reputation: 146

This should work as well (assuming source file has .nc extension, and output is using .cdf)

cd $dump1Dir
ls -1 *s$date*.nc | while read file; do ncdump "$file" > "${file%.nc}.cdf"; done

Upvotes: 0

rici
rici

Reputation: 241701

I think that what you want is something like:

for file in *s$date*.nc; do ncdump "$file" > "${file%.nc}.cdf"; done

but that includes a bunch of assumptions.

What it will do is:

  • Find all files whose name ends with the extension .nc, and includes an s followed by the value of the variable $date
  • For each such file, use ncdump to create a file with the same name, changing the extension from nc to cdf

Upvotes: 4

Related Questions