I want to run a command line tool that takes a file, manipulates it and exports a new file. I can use this command just fine for a single file, but would like to make it run through an entire folder of files of a specified extension. I'd like to name the newly created file based on the old file name, but change the first few letters of the file name to something I specify. Like this:

command newfile_same oldfile_same

Thanks for your help!

  • 1
    What command is it exactly? Perhaps it has already an option to do such batch operations? If I understood you correctly, the input and output names looks like *_same where "same" is literal? – Lekensteyn Feb 21 '12 at 20:06
  • the command is: ogr2ogr -clipsrc clipping_polygon.shp output.shp input.shp . I'd like the output to use the input file name, but prefix it with whatever I decide (e.g. input is "file.shp" and the output becomes "prefix_file.shp" . Thank you. – Paul Feb 22 '12 at 19:51

If you want to change all files in a directory, which have a special extension, you can use another for-Loop:

for FILE in *.extension; do
  command "newfile_$FILE" "$FILE";
| improve this answer | |

You can use a for loop:

for FILE in "file1" "file 2" "another file"; do
  command "newfile_$FILE" "$FILE";
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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