diff --git a/pub/lib/commands.cpp b/pub/lib/commands.cpp
index 91738c0bfe1c4b8bf9fbc5db689c72001aaed64f..22bb2dc553aecc7b77dfdc8e956ab0495e44f327 100644
--- a/pub/lib/commands.cpp
+++ b/pub/lib/commands.cpp
@@ -363,9 +363,9 @@ bool frida_command(string cmd)
         static string fname = "";
         bool append = cmd == "gfa";
         if (!append) {
-            string defaultname = triv::next_tmp_file(
+            fs::path defaultname = triv::next_tmp_file(
                 CNode::eval("psdir")->to_s() + (ps_dict == "" ? "l" : "L") + "%i" + ext);
-            defaultname = fs::path(defaultname).stem();
+            defaultname.replace_extension("");
             fname = sask("Graphic file name (without extension "+ext+")", defaultname);
             if (fname=="")
                 return false;
@@ -375,7 +375,7 @@ bool frida_command(string cmd)
             fname = sask("Graphic file name", fname);
         if (fname != "")
             SPloWin::instance()->current()->write_postscript(
-                append, fname, ps_outdir, ps_head, ps_cont, ps_dict);
+                append, triv::wordexp_unique(fname), ps_outdir, ps_head, ps_cont, ps_dict);
     } else if (cmd == "gw") {
         SPloWin::instance()->display_list();
     } else if (sscanf(cmd.c_str(), "g%i", &i) == 1) {
diff --git a/pub/trivia/file_ops.cpp b/pub/trivia/file_ops.cpp
index 7f138e4202cf69fbc3fdbd64b70a2b348389fb3c..75df87744414ed7a29843d78f01b7a2bd8e8d943 100644
--- a/pub/trivia/file_ops.cpp
+++ b/pub/trivia/file_ops.cpp
@@ -129,7 +129,7 @@ string triv::next_tmp_file(const string& path_format)
     const int tmpmax = 1024;
     for (int i = 0; i < tmpmax; ++i) {
         string fname = str(format(path_format) % i);
-        if (!triv::file_exists(fname))
+        if (!triv::file_exists(triv::wordexp_unique(fname)))
             return fname;
         if (i == tmpmax / 2)
             cerr << "WARNING: there more than " << i << " files of form '" << path_format