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

  1. Notes
    Usage:
    file file ...

    The command file can be used to determine the file type and its character encoding.
  2. Examples
    • Display file type:
      $ echo "Hello ..." > 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   10  file1
      -rw-rw-r-- 1    1  file2
      -rw-rw-r-- 1    0  file3
      drwxrwxr-x 2 4096  folder1
      -rw-rw-r-- 2   10  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/lib/jvm/java-11-openjdk-amd64/bin/java
      /usr/lib/jvm/java-11-openjdk-amd64/bin/java: application/x-sharedlib; charset=binary

      $ file /usr/lib/jvm/java-11-openjdk-amd64/bin/java
      /usr/lib/jvm/java-11-openjdk-amd64/bin/java: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=0bae0045bc544e2d1ef0c59e0d7951e880e7aacd, stripped
  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