-2

I use gcc-compiler at Ubuntu 18.04. To compile and run a program I use command:

gcc program.c -o program.exe && ./program.exe

So, how can I automatically delete this program.exe file after program finishes work?

The Impaler
  • 45,731
  • 9
  • 39
  • 76
elo
  • 489
  • 3
  • 13

2 Answers2

2

That should do the trick:

gcc program.c -o program.exe && ./program.exe && rm program.exe

Please nothing that usually binary programs in Linux doesn't have the .exe extension (but nothing prevents you from putting it, it's a just a little misleading).

Your rm could be some shell alias. Then you could force the real rm program to run by giving its (standard) path /bin/rm (instead of just rm in the command line above).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
blue112
  • 52,634
  • 3
  • 45
  • 54
-1

On Linux and Unix systems, an executable is allowed to remove itself when running (using remove(3) or unlink(2)). I gave an example here. But this is not common practice (and could confuse your user).

So you might code your C program with an explicit unlink doing that. This is unusual, and you should document that you are doing so.

Most executables are somehow permanent and are expected to be reusable a lot of times. You need to document why you don't want such a common behavior (and explain precisely why, when and how should the executable be deleted).

And I recommend compiling your executable with all warnings and debug info, so with gcc -Wall -g program.c -o program (later you could ask for optimization by adding -O2). This would ease debugging.

Of course, if you just want to remove an executable after it finishes running completely, you can simply use rm(1) (as explained by blue112's answer). You should explain why (and when) you want to do so (running rm program just after execution of ./program is unusual).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Why the downvote? The OP asks how to delete a program after it finishes its work, and I am explaining that such a program could delete itself (even if that is unusual). – Basile Starynkevitch Sep 07 '18 at 18:59