So I am often guilty of running cat on an executable file that's a binary file and my terminal usually makes some weird noises and isn't happy. Is there some accepted naming convention for giving an an extension to binary/executable encoded file?

I have an executable file (the output of go build -o /tmp/api.exe . and I as I just mentioned I just named it .exe but I am wondering if there is way to check a file before I cat it to see if it's utf8 or whateer.

  • 4
    "if there is way to check a file". Do you mean like file?
    – kaylum
    Mar 2 '20 at 0:09
  • idk, just check a file if it's cattable before i cat it Mar 2 '20 at 0:33
  • 2
    That's what I'm asking you. If you read the file man page I have linked does it do what you need it to? Generally speaking it can tell you whether a file is text, executable or binary.
    – kaylum
    Mar 2 '20 at 0:35
  • What do you mean by "hex encoded"? Hex isn't (usually) an encoding. Why should running cat on a UTF8-encoded file be a problem? Do you mean you want to check to see if a file is non-text?
    – roaima
    Mar 3 '20 at 15:19

The standard naming practice for executables is to give them the name of the command they’re supposed to implement: ls, cat... There is no provision for extensions which end up ignored from the command line.

To check what a file contains before feeding it to cat, run file on it:

$ file /bin/ls
/bin/ls: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=b6b1291d0cead046ed0fa5734037fa87a579adee, for GNU/Linux 3.2.0, stripped, too many notes (256)

$ file /bin/zgrep
/bin/zgrep: a /usr/bin/sh script, ASCII text executable

This tells me that cat /bin/zgrep won’t do anything strange to my terminal (it doesn’t even contain escape sequences, which are identified separately by file).

I much prefer using less in general: it will warn about binary files before showing them, and won’t mess up the terminal in any case. It can also be configured to behave like cat for short files (see the -F option).

As mosvy points out, you can make cat safe to use on binaries by adding the -v option, which replaces non-printable characters with visible representations (^ and M- prefixes). (Rob Pike famously considered that this option is harmful — not because of its effects on the terminal, but because of its effect on command style.)

  • Regarding best practice: Can you tell if uppercase letters are disallowed by any convention?
    – Daniel W.
    Mar 1 '21 at 18:03
  • 1
    @Daniel see this Q&A. Mar 1 '21 at 19:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.