File type

2 snippets across 2 stacks — Bash & Linux, Regular Expressions

SHBash & Linux

Identify File Type

SH · File Content
syntax
file [options] file...
example
file mystery_attachment
file -i database.dump
output
mystery_attachment: PDF document, version 1.7
database.dump: application/octet-stream; charset=binary

Note file examines the file's content (magic bytes), not the extension. -i outputs MIME type. Useful when you receive a file with no extension or a misleading one.

RXRegular Expressions

File Extension Extraction

RX · Common Patterns
syntax
/\.([a-zA-Z0-9]+)$/
example
JS:  'report-final.v2.pdf'.match(/\.([a-zA-Z0-9]+)$/)[1]
Py:  re.search(r'\.([a-zA-Z0-9]+)$', 'archive.tar.gz').group(1)
output
JS: "pdf"
Py: "gz"

Note Captures only the last extension. For double extensions like .tar.gz, match /\.tar\.gz$/ explicitly or extract both with /\.([a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)*)$/. Alternatively, use path.extname (Node) or os.path.splitext (Python).