FILE - Determine file type
file filename
file file1 file2 file3
file *
file -b filename
file --mime-type filename
file -i filename
File type detection
file document.txt
file /bin/ls
file photo.jpg
file archive.tar.gz
file script.sh
MIME types
file --mime-type file.pdf
file --mime file.txt
file -b --mime-type image.png
Recursive file search
find . -type f -exec file {} \; | grep JPEG
find . -type f -exec file --mime-type {} \; | cut -d: -f2 | sort | uniq -c
STAT - File statistics
stat filename
stat -c '%n %s' filename
stat -c '%U %G' filename
stat -c '%x %y %z' filename
Stat format specifiers
%a
%A
%b
%B
%d
%D
%f
%F
%g
%G
%h
%i
%n
%N
%o
%s
%u
%U
%x
%y
%z
Practical stat examples
stat -c '%s' file.txt
stat -c '%a' file.txt
stat -c '%U' file.txt
stat -c '%y' file.txt
stat -c '%i' file.txt
File age
stat -c '%Y' file.txt
NOW=$(date +%s)
MTIME=$(stat -c '%Y' file.txt)
AGE=$((NOW - MTIME))
echo "File is $AGE seconds old"
DAYS=$((AGE / 86400))
echo "File is $DAYS days old"
Compare files
if [ $(stat -c '%i' file1) -eq $(stat -c '%i' file2) ]; then
echo "Same file (hard link)"
fi
if [ $(stat -c '%Y' file1) -gt $(stat -c '%Y' file2) ]; then
echo "file1 is newer"
fi
File type checking in scripts
#!/bin/bash
FILE=$1
TYPE=$(file -b --mime-type "$FILE")
case "$TYPE" in
text/*)
echo "Text file"
cat "$FILE"
;;
image/*)
echo "Image file"
;;
application/pdf)
echo "PDF document"
;;
*)
echo "Unknown type: $TYPE"
;;
esac
Verify file integrity
file image.jpg
Find files by type
find . -type f -exec file --mime-type {} \; | grep image
find . -type f -exec file --mime-type {} \; | grep 'application/pdf'
find . -type f -exec file --mime-type {} \; | grep 'text/'
File command debugging
file -v filename
file -m /path/to/magic filename
file --help | grep "magic"
Stat for directories
stat dirname/
stat -c '%s' dirname/
du -sh dirname/
Permission analysis
PERMS=$(stat -c '%a' file.txt)
if [ $((PERMS & 0111)) -ne 0 ]; then
echo "Executable"
fi
if [ $((PERMS & 0002)) -ne 0 ]; then
echo "World-writable (dangerous!)"
fi
Time comparisons
if [ $(stat -c '%Y' file1) -gt $(stat -c '%Y' file2) ]; then
echo "file1 is newer"
else
echo "file2 is newer"
fi
find . -type f -mtime -1 -exec stat -c '%n %y' {} \;
Complete file information
#!/bin/bash
FILE=$1
echo "=== File Information: $FILE ==="
echo "Type: $(file -b "$FILE")"
echo "MIME: $(file -b --mime-type "$FILE")"
echo "Size: $(stat -c '%s' "$FILE") bytes"
echo "Permissions: $(stat -c '%A (%a)' "$FILE")"
echo "Owner: $(stat -c '%U:%G' "$FILE")"
echo "Inode: $(stat -c '%i' "$FILE")"
echo "Links: $(stat -c '%h' "$FILE")"
echo "Modified: $(stat -c '%y' "$FILE")"
echo "Accessed: $(stat -c '%x' "$FILE")"
Batch operations
for file in *; do
echo "$file: $(file -b --mime-type "$file")"
done > file-types.txt
find . -type f -size +100M -exec sh -c '
echo "Large file: $1 ($(file -b "$1"))"
' _ {} \;
macOS differences
if stat -c '%Y' file 2>/dev/null; then
MTIME=$(stat -c '%Y' file)
else
MTIME=$(stat -f '%m' file)
fi
Quick reference
file filename
file -b filename
file --mime-type filename
file -i filename
stat filename
stat -c '%s' file
stat -c '%a' file
stat -c '%U' file
stat -c '%y' file
stat -c '%i' file
file -b --mime-type file
stat -c '%s' file
stat -c '%Y' file