maximusdooku maximusdooku - 1 year ago 66
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.


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"


    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


How can I do this?

Answer Source

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/{} \;


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.


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download