MTI TEK
  • Home
  • About
  • LLMs
  • Docker
  • Kubernetes
  • Java
  • All Resources
Linux-Ubuntu | file -- display file type
  1. Notes
  2. Examples
  3. Command Help (man file)

  1. Notes
    Usage:
    file [options] file ...
    
    The command file can be used to determine the file type and its character encoding. It examines files and attempts to classify them by analyzing their contents, magic numbers, and other characteristics rather than relying solely on file extensions.
  2. Examples
    • Display file type:
      $ echo "Hello World" > file1
      $ ln -s file1 sfile1
      $ ln file1 hfile1
      $ echo "" > file2
      $ touch file3
      $ echo '#!/bin/bash' > script1.sh; chmod 755 script1.sh
      $ mkdir folder1
      
      $ ls -l ./
      -rw-rw-r-- 2   12  file1
      -rw-rw-r-- 1    1  file2
      -rw-rw-r-- 1    0  file3
      drwxrwxr-x 2 4096  folder1
      -rw-rw-r-- 2   12  hfile1
      -rwxr-xr-x 1   12  script1.sh
      lrwxrwxrwx 1    5  sfile1 -> file1
      
      $ file ./*
      ./file1:      ASCII text
      ./file2:      very short file (no magic)
      ./file3:      empty
      ./folder1:    directory
      ./hfile1:     ASCII text
      ./script1.sh: Bourne-Again shell script, ASCII text executable
      ./sfile1:     symbolic link to file1
      
      You can use the option -i to display MIME type and encoding of the file:
      $ file -i ./*
      ./file1:      text/plain; charset=us-ascii
      ./file2:      application/octet-stream; charset=binary
      ./file3:      inode/x-empty; charset=binary
      ./folder1:    inode/directory; charset=binary
      ./hfile1:     text/plain; charset=us-ascii
      ./script1.sh: text/x-shellscript; charset=us-ascii
      ./sfile1:     inode/symlink; charset=binary
      
      You can also use the command file to display the file type of an executable binary file (display the OS information and the libraries the executable requires):
      $ file -i /usr/bin/ls
      /usr/bin/ls: application/x-pie-executable; charset=binary
      
      $ file /usr/bin/ls
      /usr/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]=3eca7e3905b37d48cf0a88b576faa7b95cc3097b, for GNU/Linux 3.2.0, stripped
      
      Additional useful options:
      $ file -b rag-prompt.py
      Python script, Unicode text, UTF-8 text executable
      
      $ file --mime-type rag-prompt.py
      rag-prompt.py: text/x-script.python
      
      $ file --mime-encoding rag-prompt.py
      rag-prompt.py: utf-8
      
      For compressed files, use the -z option to look inside:
      $ echo "Hello World" > test.txt
      $ gzip test.txt
      $ file test.txt.gz
      test.txt.gz: gzip compressed data, was "test.txt", last modified: Thu Jul 17 17:54:07 2025, from Unix, original size modulo 2^32 12
      
      $ file -z test.txt.gz
      test.txt.gz: ASCII text (gzip compressed data, was "test.txt", last modified: Thu Jul 17 17:54:07 2025, from Unix)
      
  3. Command Help (man file)
    The following options can be used:
        --help                 display this help and exit
    -v, --version              output version information and exit
    -m, --magic-file LIST      use LIST as a colon-separated list of magic number files
    -z, --uncompress           try to look inside compressed files
    -Z, --uncompress-noreport  only print the contents of compressed files
    -b, --brief                do not prepend filenames to output lines
    -c, --checking-printout    print the parsed form of the magic file, use in conjunction with -m to debug a new magic file before installing it
    -e, --exclude TEST         exclude TEST from the list of test to be performed for file.
                               Valid tests are: apptype, ascii, cdf, compress, elf, encoding, soft, tar, text, tokens
    -f, --files-from FILE      read the filenames to be examined from FILE
    -F, --separator STRING     use string as separator instead of `:'
    -i, --mime                 output MIME type strings (--mime-type and --mime-encoding)
        --apple                output the Apple CREATOR/TYPE
        --extension            output a slash-separated list of extensions
        --mime-type            output the MIME type
        --mime-encoding        output the MIME encoding
    -k, --keep-going           don't stop at the first match
    -l, --list                 list magic strength
    -L, --dereference          follow symlinks (default if POSIXLY_CORRECT is set)
    -h, --no-dereference       don't follow symlinks (default if POSIXLY_CORRECT is not set) (default)
    -n, --no-buffer            do not buffer output
    -N, --no-pad               do not pad output
    -0, --print0               terminate filenames with ASCII NUL
    -p, --preserve-date        preserve access times on files
    -P, --parameter            set file engine parameter limits
                                 indir        15 recursion limit for indirection
                                 name         30 use limit for name/use magic
                                 elf_notes   256 max ELF notes processed
                                 elf_phnum   128 max ELF prog sections processed
                                 elf_shnum 32768 max ELF sections processed
    -r, --raw                  don't translate unprintable chars to \ooo
    -s, --special-files        treat special (block/char devices) files as ordinary ones
    -C, --compile              compile file specified by -m
    -d, --debug                print debugging messages
    
© 2025 mtitek