0

I have an alias for a python script that basically converts some files into another format, I want to be able to remove some intermediate format that this script generates as a post process after running the python script.

My alias to my python script looks like this:

alias convert='python /filepath/convert.py'

I run it like this within the directory containing the files I want to convert:

convert *.RW2

The script converts all .RW2 files into .tiff files but generates an intermediate .dng file which I don't need to keep, it's a temp file.

How would I simply append a delete to the alias? If I modify the alias it seems to interfere with the arguments for the .py script, for example this does not work:

alias convert='python /filepath/convert.py && rm *.dng'

  • It seems cleaner and more maintainable to edit your python script to handle the deletion. The script likely has a variable for the correct filename, so you needn't rely upon wildcards. – user535733 Jul 09 '19 at 15:44
  • I didn't write the original python script and I would need to brush up on my python in order to do that. Thanks for your suggestion, it makes sense. – ejnewman Jul 09 '19 at 20:48

1 Answers1

3

Probably the "cleanest" way to solve this is to modify the convert.py script to clean up after itself.

An alias is a simple text replacement macro - so your example will expand to something like

python /filepath/convert.py && rm *.dng file1.RW2 file2.RW2 file3.RW2 ...

which is obviously going to be incorrect. For anything more complex than a simple text substitution you should use a shell function instead ex.

convert() {
  python /filepath/convert.py "$@" && rm -- *.dng
}

or perhaps

convert() {
  python /filepath/convert.py "$@"
  for f; do 
    rm -- "${f%.*}.dng"
  done
}

(although you may want to choose a different name since there's already an ImageMagick command with the name convert).

See also Aliases - able to pass arguments?

steeldriver
  • 136,215
  • 21
  • 243
  • 336
  • +1 and thank you for mentioning the ImageMagick command. I'd like to beat them for chosing such a general term for such a special command. – PerlDuck Jul 09 '19 at 13:10
  • 1
    @PerlDuck not quite as much as whoever named the script utility ;) – steeldriver Jul 09 '19 at 13:27
  • @steedriver this worked a treat - thanks so much. I need to understand the syntax better so I can learn this. Any suggested reading would be very welcome. – ejnewman Jul 09 '19 at 15:04