Software⏱️ 4 min read📅 2026-06-15

How to Fix: how to avoid undefinedfilename error when piping a2ps output to ps2pdf

Convert text file to PDF using a2ps and ps2pdf, avoid undefinedfilename error.

Quick Answer: Specify the output filename with the -o option in ps2pdf command.

The undefinedfilename error occurs when the file specified in the command does not exist or is not accessible. This issue affects users who try to convert text files to PDF using the a2ps and ps2pdf commands. It can be frustrating, especially for those who are new to Linux or scripting.

To resolve this issue, we will explore two primary fix methods that can help avoid the undefinedfilename error when piping a2ps output to ps2pdf.

🔍 Why This Happens

  • The first main reason why this error happens is due to the file not existing in the specified location. This can occur if the file has been deleted or renamed, or if it was never created in the first place. To avoid this issue, ensure that the file exists and is accessible before running the conversion command.
  • Another alternative reason for this error is due to a permissions issue. If the user running the script does not have permission to access the file, the conversion will fail with an undefinedfilename error.

🚀 How to Resolve This Issue

Specify the Output File Using the `-o` Option

  1. Step 1: To avoid the undefinedfilename error, specify the output file using the `-o` option. For example, replace `exc.pdf` with `output.pdf`: `a2ps -M a4 -B -1 -o - exc.cpp | ps2pdf -sPAPERSIZE=a4 -o output.pdf -`. This will ensure that the output file is created in the correct location and avoids the undefinedfilename error.
  2. Step 2: Alternatively, you can also use the `-o` option to specify a different output directory. For example: `a2ps -M a4 -B -1 -o /path/to/output/ exc.cpp | ps2pdf -sPAPERSIZE=a4 -o output.pdf -`. This will create the output file in the specified directory instead of the current working directory.
  3. Step 3: By specifying the output file using the `-o` option, you can avoid the undefinedfilename error and ensure that your script runs successfully.

Use a Temporary File

  1. Step 1: Another way to resolve the undefinedfilename error is to use a temporary file. You can create a temporary file using the `mktemp` command, for example: `a2ps -M a4 -B -1 -o /tmp/temp.pdf exc.cpp | ps2pdf -sPAPERSIZE=a4 -o /tmp/temp.pdf -`. This will create a temporary file in the `/tmp` directory that can be used as the output file.
  2. Step 2: Once the conversion is complete, you can remove the temporary file using the `rm` command: `rm /tmp/temp.pdf`. This approach ensures that the script runs successfully even if the output file does not exist or cannot be accessed.

💡 Conclusion

By following these two primary fix methods, you can avoid the undefinedfilename error when piping a2ps output to ps2pdf. Remember to specify the output file using the `-o` option or use a temporary file to ensure that your script runs successfully and produces the desired output.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions