Extract a DOS Codepage Safely with codepage
You will finish with a small, repeatable workflow for inspecting an MSDOS CPI file and extracting either one embedded codepage or every codepage it contains. The examples use codepage from kbd 2.6.4-2ubuntu2, installed on Ubuntu here.
The route
Jump straight to the step you need, or tick off Done means at the end.
Allow about ten minutes. You need a shell, a readable .cpi file, and enough free space for the extracted output. This is normally an unprivileged operation. Do not use sudo merely because the file came from a system package. Use it only if ordinary read permission genuinely prevents access, and write the result somewhere you own.
1. Confirm the installed command
Check the binary and package before relying on examples. These commands only read local metadata:
$ command -v codepage
/usr/bin/codepage
$ dpkg-query -W -f='${Package} ${Version}\n' kbd
kbd 2.6.4-2ubuntu2
The local manual describes codepage as an extractor for MSDOS codepage files. Its option names are short: -l lists embedded codepages, -L prints file header information, -a extracts all codepages, and -c treats the input as one codepage rather than a CPI collection. A three-digit number selects one codepage.
Checkpoint: you should have a readable input path. Replace the placeholder below; do not create a file with an arbitrary extension and assume it is a valid CPI file:
$ CPI_FILE='/path/to/ega.cpi'
$ test -r "$CPI_FILE" && echo 'input is readable'
input is readable
2. Inspect the CPI header
Start with -L when you need to understand what the file contains. It is a read-only inspection:
$ codepage -L "$CPI_FILE"
header information printed by codepage
The exact header text depends on the file. The useful result is that the command opens the file and prints its metadata. If it says that it cannot open the file, check the path and permissions first:
$ ls -l -- "$CPI_FILE"
$ file -- "$CPI_FILE"
Do not treat the command's output as proof that every embedded record is usable. The header is an inventory aid; extraction is the next check.
3. List the available codepages
Use -l to list all codepages contained in the CPI file without writing extracted files:
$ codepage -l "$CPI_FILE"
codepage numbers printed here depend on the file
Choose an exact three-digit number from that output. Common DOS encodings include 437 and 850, but do not guess: the number must be present in your file. If the list is empty or the command cannot parse the input, keep the original file and find a valid CPI file rather than forcing an extraction.
One slightly confusing detail is the interface. The manual's synopsis shows an option or number followed by a file, while its extraction example places the file before the number: codepage ega.cpi 850. Follow the documented extraction form for a numbered selection and verify the output afterwards.
4. Extract one codepage into a new file
Make a destination in your current working directory, then run the documented form with the selected three-digit codepage. The redirection is deliberate: it keeps the source CPI unchanged and gives the extracted file a clear name.
$ CODEPAGE_NUMBER='850'
$ OUTPUT_FILE="codepage-${CODEPAGE_NUMBER}.cp"
$ codepage "$CPI_FILE" "$CODEPAGE_NUMBER" > "$OUTPUT_FILE"
$ test -s "$OUTPUT_FILE" && echo "created $OUTPUT_FILE"
created codepage-850.cp
The command writes the extracted codepage to standard output, so shell redirection is what creates the destination file. If your installed file already exists, > truncates it before codepage runs. That is an avoidable destructive mistake. Select a new name, or make a backup before replacing a known-good result:
$ cp --preserve=all -- "$OUTPUT_FILE" "${OUTPUT_FILE}.bak"
$ codepage "$CPI_FILE" "$CODEPAGE_NUMBER" > "${OUTPUT_FILE}.new"
$ test -s "${OUTPUT_FILE}.new" && mv -- "${OUTPUT_FILE}.new" "$OUTPUT_FILE"
$ rm -- "${OUTPUT_FILE}.bak"
Only remove the backup after checking the replacement. That final removal is irreversible. If extraction fails, remove the incomplete .new file instead and retain the original output.
5. Extract every codepage when required
Use -a when you genuinely need the whole collection:
$ mkdir -p -- extracted-codepages
$ cd extracted-codepages
$ codepage -a "$CPI_FILE"
$ find . -maxdepth 1 -type f -print
The manual promises extraction of all codepages, but does not specify a portable naming scheme for the generated files. Inspect the directory rather than scripting against guessed names. The command may create several files in the directory where it runs, so choose an empty working directory. To undo this example, stop and inspect the files first, then remove only the directory you created if it contains nothing you need:
$ cd ..
$ find extracted-codepages -maxdepth 1 -type f -print
$ rm -r -- extracted-codepages
Do not run that removal blindly in a directory containing other work.
6. Handle failures without guessing
codepage has no documented long option for help or version output. On this installed build, codepage --help is treated as an input filename and reports that it cannot open it. Use the manual and package query for the interface and version instead.
A missing or unreadable file is usually a path or permission problem. A valid path that produces no useful listing may not be a CPI file, or it may use a format this old extractor cannot read. Check with file, compare the selected number with codepage -l, and preserve the source while investigating. The installed command can return a zero shell status even when it prints an "Unable to open file" message, so visible diagnostics and output-file checks matter more than $? alone.
Done means
- You confirmed the installed kbd package and the local command path.
- The source CPI file is readable and remains unchanged.
- You inspected the header or list and selected a codepage actually present in the file.
- Your extracted file is non-empty and has a destination name you chose deliberately.
- You used a separate directory for bulk extraction and did not remove anything valuable during cleanup.