Skip to content

Linux Investigating File Contents

🔍 Investigating File Contents in Linux

1. file — Identify File Type

The file command inspects a file and tells you what type it is, without relying solely on the file extension.

Usage:

Terminal window
file filename

Example:

Terminal window
$ file notes.txt
notes.txt: ASCII text
$ file /bin/ls
/bin/ls: ELF 64-bit LSB executable, x86-64

What you learn:

  • Whether a file is text, binary, executable, archive, image, etc.
  • Helpful for understanding what tools to use to inspect the file further.

2. head — View the Beginning of a File

The head command shows the first lines of a file. Default is 10 lines, but you can adjust it.

Usage:

Terminal window
head filename
head -n 20 filename # show first 20 lines

Example:

Terminal window
$ head notes.txt
This is the first line of the file.
This is the second line.
...

What you learn:

  • Quickly inspect the start of a text file or log file.
  • Useful to check headers or file metadata in plain-text files.

3. tail — View the End of a File

The tail command shows the last lines of a file. Very useful for log files.

Usage:

Terminal window
tail filename
tail -n 50 filename # last 50 lines
tail -f filename # follow live updates

Example:

Terminal window
$ tail /var/log/syslog
Sep 7 12:34:45 kernel: ...

What you learn:

  • Monitor logs in real-time (-f).
  • Quickly see recent activity in large files.

4. objdump — Inspect Binary Executables

objdump is a tool for examining binary files and executables. You can look at headers, disassembly, or raw content.

Usage:

Terminal window
objdump -d filename # disassemble executable
objdump -x filename # show all headers

Example:

Terminal window
$ objdump -d /bin/ls | head -n 10

What you learn:

  • CPU instructions in binaries (-d).
  • Section headers, symbols, and other metadata (-x).
  • Useful for reverse engineering or debugging compiled programs.

5. od — Dump File in Octal/Hex/Binary

od stands for octal dump. It can display file contents in various number formats.

Usage:

Terminal window
od -c filename # show ASCII characters
od -x filename # show hexadecimal
od -b filename # show octal bytes

Example:

Terminal window
$ od -c notes.txt
0000000 T h i s i s t e x t \n

What you learn:

  • Low-level view of file contents.
  • See hidden characters, binary data, or encoding issues.

6. Other Useful Tools

  • strings → extracts readable ASCII strings from binaries:
Terminal window
strings /bin/ls
  • xxd → creates a hex dump and reverse:
Terminal window
xxd filename
xxd -r filename.hex # convert hex back to file
  • less → interactively scroll through files:
Terminal window
less filename
  • cat → concatenate and print the full file:
Terminal window
cat filename

🔑 Summary

CommandPurpose
fileIdentify file type
headView first lines
tailView last lines or follow logs
objdumpInspect executables/binaries
odView raw data in octal/hex/binary
stringsExtract printable strings from binary
xxdHex dump and reverse
lessBrowse large files interactively
catPrint entire file
These commands together allow you to explore any file type, from plain text to compiled binaries, giving both high-level and low-level views.