Home / Alt manpages / codepage(1)

  • codepage(1)
  • User command
  • linux

Extract a DOS Codepage Safely with codepage

An old MSDOS .cpi file can bundle a dozen codepages together, and codepage is the small kbd tool that extracts them one at a time. Examples use codepage from kbd 2.6.4-2ubuntu2 on Ubuntu.

Allow about ten minutes. You need a shell, a readable .cpi file and enough free space for the extracted output. This is normally unprivileged: do not reach for sudo just because the file came from a system package, and write results somewhere you own.

1. Confirm the installed command

Check the binary and package before trusting the examples below; 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
  • -l lists the codepages embedded in the file.
  • -L prints file header information.
  • -a extracts every codepage.
  • -c treats the input as one codepage rather than a CPI collection.
  • A three-digit number selects a single codepage.

Checkpoint

You should have a readable input path before going further. Do not invent 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 know 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 it and prints metadata at all. If it says it cannot open the file, check the path and permissions first:

$ ls -l -- "$CPI_FILE"
$ file -- "$CPI_FILE"

This output is an inventory aid, not proof that every embedded record is usable. Extraction is the next check.

3. List the available codepages

-l lists everything in the CPI file without writing anything to disk:

$ codepage -l "$CPI_FILE"
# codepage numbers printed here; depends on the file

Pick an exact three-digit number from that output. Common DOS encodings include 437 and 850, but do not guess: it has to actually be present in your file. If the list is empty or the file will not parse, keep the original and go find a valid CPI file rather than forcing an extraction.

Tip

The interface is slightly inconsistent between documentation and practice. The manual's synopsis shows an option or number before the file, but the extraction example puts the file first: codepage ega.cpi 850. Follow that documented extraction form and verify the output afterwards.

4. Extract one codepage into a new file

Redirection is what creates the output file here, since codepage writes to standard output:

$ 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

Warning

If $OUTPUT_FILE already exists, > truncates it before codepage even runs, an easy way to lose a known-good result. Back it up or write to a new name instead:

$ 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 removal is irreversible. If extraction fails instead, delete the incomplete .new file and keep the original output.

5. Extract every codepage when required

Use -a when you genuinely need the whole collection, in an empty directory you chose on purpose:

$ mkdir -p -- extracted-codepages
$ cd extracted-codepages
$ codepage -a "$CPI_FILE"
$ find . -maxdepth 1 -type f -print

The manual promises every codepage gets extracted, but does not specify a portable naming scheme for the resulting files, so inspect the directory rather than scripting against guessed names.

$ cd ..
$ find extracted-codepages -maxdepth 1 -type f -print
$ rm -r -- extracted-codepages

Warning

That last removal deletes the whole directory. Inspect the files first, and never run it blindly in a directory that holds other work.

6. Handle failures without guessing

codepage has no documented long option for help or version output: on this build, codepage --help is treated as a filename and reported as unopenable. Use the manual and a package query 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 may use a format this old extractor cannot read: check with file, compare the number against codepage -l, and keep the source while you investigate. The command can return a zero shell status even while printing an "Unable to open file" message, so read the visible diagnostics and check the output file, do not trust $? alone.

Done means

  • Package confirmed. The installed kbd package and command path match what you expect.
  • Source untouched. The CPI file stayed readable and unchanged throughout.
  • Codepage verified present. You checked the header or list and picked a number that is actually in the file.
  • Output is real. The extracted file is non-empty and has a name you chose on purpose.
  • Bulk extraction contained. A dedicated directory held the results, and cleanup did not remove anything valuable.