maximusdooku maximusdooku - 1 month ago 10
Linux Question

How can I execute a command and store the files in a folder using a shell script?

I have a large number of tif files.

a.tif
b.tif
axx.tif
auw.tif


I am trying to write a script that will


  1. Search for all .tif files

  2. Perform a gdal conversion

  3. Store a new file with the same file name under a subdirectory named "subdir"

    #!/bin/csh

    list - ls *.tif

    for file in list
    gdalwarp -t_srs EPSG:4269 -te -130 20 -60 55 -tr 0.01 -0.01 oldfilename.tif subdir/oldfilename.tif


    done



How can I do this?

Answer

Instead of using "list" to find the files, you can use the "find" command, here's how it's done:

find . -name "*.tif" -exec gdalwarp -t_srs EPSG:4269 -te -130 20 -60 55 -tr 0.01 -0.01 {} subdir/{} \;

Explanation:

The . stands for "current directory" the -name option search for files with names that match the given pattern.

The -exec option is used to run a command using the file "find" found. The {} stands for the "filename" that was found, and the command that has to be run needs to be followed by \;

I highly suggest you read the manual entry for find, as well as do some more research on the tools you have at your disposal when attempting to write a shell script.

Regards!

Comments