UNIX File System: An Overview

A file in UNIX is essentially a collection of data, whether it be text, binary, or structured information. The UNIX file system provides various commands for examining and managing files. This post covers some fundamental commands used to inspect and navigate files efficiently.

Viewing File Content with od

The od (octal dump) command displays a file’s contents in different formats, which is useful for inspecting non-text files. Some commonly used options include:

  • -c: Interprets bytes as characters.
  • -b: Prints bytes as octal numbers.
  • No option: Dumps the file in 16-bit words (default).

Example:

$ od -c example.txt   # Show file content as characters
$ od -b example.txt   # Show file content in octal format
$ od example.txt      # Default output (16-bit words)

Identifying File Types with file

The file command determines a file’s type by inspecting its contents rather than relying on extensions.

Example:

$ file example.txt
example.txt: ASCII text
$ file script.sh
script.sh: Bourne-Again shell script, UTF-8 Unicode text
$ file binary_file
binary_file: ELF 64-bit LSB executable, x86-64

Checking Disk Usage with du

The du (disk usage) command reports the disk space used by files and directories.

  • du: Shows disk usage of directories.
  • du -a: Includes files along with directories.
  • du -h: Displays sizes in a human-readable format.
  • du -a | grep filename: Filters output for a specific file.

Example:

$ du             # Show disk usage of directories
$ du -a          # Show disk usage of all files and directories
$ du -h          # Display sizes in human-readable format (e.g., KB, MB, GB)
$ du -a | grep example.txt  # Search for a specific file's usage

Understanding UNIX Directories

A UNIX directory consists of 16-byte chunks:

  • The first two bytes point to the administrative information.
  • The last 14 bytes contain the file name, padded with ASCII null characters (NUL).

Understanding this structure helps in low-level file system debugging and development.

Finding Files with find

The find command searches for files based on criteria like name, type, size, and modification time.

Basic usage:

$ find . -name "example.txt"    # Find a file named 'example.txt' in the current directory
$ find /home -type f -size +10M  # Find files larger than 10MB in /home
$ find /var/log -mtime -7         # Find files modified in the last 7 days in /var/log

Additional Useful Commands

  • ls -l: Lists files with detailed information.
  • stat filename: Displays detailed file metadata.
  • df -h: Shows available disk space in a human-readable format.

With these commands, managing and analyzing files in UNIX becomes efficient and insightful. Mastering them will help streamline system operations and troubleshooting.

Unknown's avatar

Author: Abhilash

Hi, I’m Abhilash! A seasoned web developer with 15 years of experience specializing in Ruby and Ruby on Rails. Since 2010, I’ve built scalable, robust web applications and worked with frameworks like Angular, Sinatra, Laravel, Node.js, Vue and React. Passionate about clean, maintainable code and continuous learning, I share insights, tutorials, and experiences here. Let’s explore the ever-evolving world of web development together!

Leave a comment