Bash Script: How to remove temp file when terminated from keyboard?
by ddenial from LinuxQuestions.org on (#53CFD)
Hello All
I have written a bash script that encodes all video files in a directory to h265 (hevc) codec to reduce storage file size. First, the script encodes the video file to a temp file, and then when encoding is successful, it renames that temp file to the original file and finally removes that temp file.
But if I press Ctrl+C in-between, I want to remove that temp file. I have specified trap command at the beginning of the script. The temp file is created later.
How do achieve that? Is it even possible?
For some reason, I'm unable to copy and paste the script, so I've attached full script for reference.
Also, this is my first big script. I've concentrated only on logic rather than performance. I really appreciate pointing to any flaws in it.
Thanks.
Attached Files


I have written a bash script that encodes all video files in a directory to h265 (hevc) codec to reduce storage file size. First, the script encodes the video file to a temp file, and then when encoding is successful, it renames that temp file to the original file and finally removes that temp file.
But if I press Ctrl+C in-between, I want to remove that temp file. I have specified trap command at the beginning of the script. The temp file is created later.
How do achieve that? Is it even possible?
For some reason, I'm unable to copy and paste the script, so I've attached full script for reference.
Also, this is my first big script. I've concentrated only on logic rather than performance. I really appreciate pointing to any flaws in it.
Thanks.
Attached Files
![]() | hevc.txt (6.1 KB) |