fstat

system managementBSD/Unix
The fstat command is one of the most frequently used commands in Linux/Unix-like operating systems. fstat Display status information about open files

Quick Reference

Command Name:

fstat

Category:

system management

Platform:

BSD/Unix

Basic Usage:

fstat [options] [arguments]

Common Use Cases

    Syntax

    fstat [-f filename] [-M core] [-N system] [-p pid] [-u user] [file ...]

    Options

    Option Description
    -f filename Show only files with the specified filename
    -M core Extract values from the specified core file
    -N system Extract name list from the specified system file
    -p pid Show only files opened by the specified process ID
    -u user Show only files opened by the specified user
    file Show only processes that have the specified file open

    Examples

    How to Use These Examples

    The examples below show common ways to use the fstat command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    fstat
    Display information about all open files.
    fstat -p 1234
    Show information about open files for process with PID 1234.
    fstat /dev/sda1
    Display information about processes that have /dev/sda1 open. # Advanced Examples Advanced fstat -u username Show files opened by a specific user. fstat -f /var/log/syslog Find processes that have /var/log/syslog open. fstat | grep socket Filter output to show only socket files. fstat | grep TCP Show only TCP sockets. fstat -p 1234 | grep VREG Show only regular files opened by process 1234.

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The fstat command displays status information about open files in a system. It provides a detailed view of which processes have which files open, along with information about file types, access modes, and other attributes. Key features of fstat: 1. File Usage Tracking: fstat shows which processes are accessing specific files, helping identify processes that might be preventing a file from being modified or deleted. 2. Process File Enumeration: It can list all files that a specific process has open, useful for debugging and security analysis. 3. User Activity Monitoring: With the -u option, fstat can show all files opened by a specific user, helpful for system administration and security monitoring. 4. Detailed Information: For each open file, fstat typically shows the process ID, user ID, file descriptor, file type, device, inode number, access mode, and file name. 5. Various File Types: fstat can display information about regular files, directories, block and character devices, sockets, pipes, and other file types in the system. 6. Network Connections: It can show details about open network connections, including socket types, protocols, and address information. 7. System Debugging: fstat is particularly useful for system debugging, such as identifying processes that have deleted files open but are still holding file descriptors. fstat is commonly found in BSD-derived systems (like FreeBSD and macOS). On Linux systems, similar functionality is provided by the lsof (list open files) command, which has become more widely used across Unix-like systems.

    Related Commands

    These commands are frequently used alongside fstat or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the fstat command works in different scenarios.

    $ fstat
    View All Commands