Linux free command options


The free command in Linux is used to display information about the system's memory usage, including RAM and swap space. It has several options that allow you to customize the output format and the level of detail. Here's a breakdown of the commonly used options with examples and their output.

Basic Syntax:

free [options]

1. -h (Human-readable format):

  • Purpose: Displays the memory usage in a human-readable format, automatically adjusting the unit (KB, MB, GB, etc.) depending on the size of the memory.

Example:

free -h

Output:

total used free shared buff/cache available Mem: 8.0Gi 1.7Gi 3.8Gi 234Mi 2.4Gi 5.6Gi Swap: 2.0Gi 0B 2.0Gi
  • Gi (Gibibytes) and Mi (Mebibytes) are used for clarity, and the memory values are shown in the most suitable unit.

2. -m (Show in Megabytes):

  • Purpose: Displays the memory usage in megabytes (MB) instead of the default kilobytes (KB).

Example:

free -m

Output:

total used free shared buff/cache available Mem: 8192 1732 3840 240 2432 5732 Swap: 2048 0 2048
  • Memory values are displayed in megabytes (MB), making it easier to read when working with larger systems.

3. -g (Show in Gigabytes):

  • Purpose: Displays the memory usage in gigabytes (GB).

Example:

free -g

Output:

total used free shared buff/cache available Mem: 8 2 4 0 2 6 Swap: 2 0 2
  • This output uses gigabytes (GB) for a higher-level view of memory usage.

4. -t (Show total memory):

  • Purpose: Displays a summary line that includes the total memory (physical + swap). This line adds up the physical memory and swap space, providing a more comprehensive total memory usage.

Example:

free -t

Output:

total used free shared buff/cache available Mem: 8192 1732 3840 240 2432 5732 Swap: 2048 0 2048 Total: 10240 1732 5888 240 4864 5732
  • The Total: line at the bottom adds together the memory from both the physical RAM (Mem:) and swap (Swap:).

5. -c <count> (Repeat output count times):

  • Purpose: Repeats the free command output a specified number of times (useful for monitoring memory usage over time).

Example:

free -c 5

Output:

total used free shared buff/cache available Mem: 8192 1732 3840 240 2432 5732 Swap: 2048 0 2048 total used free shared buff/cache available Mem: 8192 1732 3840 240 2432 5732 Swap: 2048 0 2048 total used free shared buff/cache available Mem: 8192 1732 3840 240 2432 5732 Swap: 2048 0 2048 total used free shared buff/cache available Mem: 8192 1732 3840 240 2432 5732 Swap: 2048 0 2048 total used free shared buff/cache available Mem: 8192 1732 3840 240 2432 5732 Swap: 2048 0 2048
  • The free command will display the memory usage five times (or however many times you specify) with the same data.

6. -s <seconds> (Set delay between updates):

  • Purpose: Displays memory usage repeatedly with a specified delay between each update in seconds.

Example:

free -s 5

Output (example):

total used free shared buff/cache available Mem: 8192 1732 3840 240 2432 5732 Swap: 2048 0 2048 total used free shared buff/cache available Mem: 8192 1732 3840 240 2432 5732 Swap: 2048 0 2048
  • This shows the memory usage every 5 seconds.

7. -w <delay> (Set update delay for seconds in batch mode):

  • Purpose: Set the delay in seconds between updates when used in batch mode (-b).

Example:

free -b -w 1

Output:

total used free shared buff/cache available Mem: 8192 1732 3840 240 2432 5732 Swap: 2048 0 2048 total used free shared buff/cache available Mem: 8192 1732 3840 240 2432 5732 Swap: 2048 0 2048
  • This will show the memory usage with a 1-second delay between each update, but in batch mode, which is not interactive.

8. -V (Version):

  • Purpose: Display the version of the free command.

Example:

free -V

Output:

free from procps-ng 3.3.15
  • This will display the version of the free command installed on your system.

Summary of Common Options:

  1. -h: Human-readable format (e.g., GB, MB, KB).
  2. -m: Display memory in megabytes (MB).
  3. -g: Display memory in gigabytes (GB).
  4. -t: Show total memory (including swap).
  5. -c <count>: Repeat output for a specified number of times.
  6. -s <seconds>: Delay between updates in seconds (repeated output).
  7. -w <delay>: Set update delay for batch mode.
  8. -V: Show the version of the free command.

The free command provides an easy-to-understand snapshot of system memory and swap usage, and the options help tailor the output to your needs, whether you prefer more detailed, readable formats or a summary of the memory usage over time.