Linux provides more detailed memory information beyond what the basic free
command shows. In this step, you will explore additional sources of memory information and create a comprehensive memory summary.
Exploring /proc/meminfo
The /proc/meminfo
file contains detailed memory information about your system. Let's examine it:
head -20 /proc/meminfo
This file contains dozens of memory-related values, including:
MemTotal
: Total usable RAM
MemFree
: Free memory
MemAvailable
: Available memory
Buffers
: Memory used by kernel buffers
Cached
: Memory used for file caching
SwapTotal
: Total swap space
SwapFree
: Free swap space
Let's extract some key information from this file:
grep -E "MemTotal|MemFree|MemAvailable|Buffers|Cached|SwapTotal|SwapFree" /proc/meminfo > ~/project/memory_data/meminfo_excerpt.txt
View the extracted information:
cat ~/project/memory_data/meminfo_excerpt.txt
Creating a Comprehensive Memory Summary
Now, let's create a script that generates a comprehensive memory summary report. Create a new script file:
nano ~/project/memory_data/create_summary.sh
Add the following content:
#!/bin/bash
## Set output file with timestamp
output_file=~/project/memory_data/memory_summary_$(date +%Y%m%d_%H%M%S).txt
## Create header
echo "LINUX MEMORY SUMMARY REPORT" > $output_file
echo "===========================" >> $output_file
echo "Date: $(date)" >> $output_file
echo "" >> $output_file
## Basic memory statistics
echo "BASIC MEMORY STATISTICS:" >> $output_file
free -h >> $output_file
echo "" >> $output_file
## Detailed memory information
echo "DETAILED MEMORY INFORMATION:" >> $output_file
echo "Total RAM: $(grep MemTotal /proc/meminfo | awk '{print $2 " " $3}')" >> $output_file
echo "Free RAM: $(grep MemFree /proc/meminfo | awk '{print $2 " " $3}')" >> $output_file
echo "Available RAM: $(grep MemAvailable /proc/meminfo | awk '{print $2 " " $3}')" >> $output_file
echo "Buffer memory: $(grep Buffers /proc/meminfo | awk '{print $2 " " $3}')" >> $output_file
echo "Cache memory: $(grep "^Cached:" /proc/meminfo | awk '{print $2 " " $3}')" >> $output_file
echo "" >> $output_file
## Swap information
echo "SWAP INFORMATION:" >> $output_file
echo "Total Swap: $(grep SwapTotal /proc/meminfo | awk '{print $2 " " $3}')" >> $output_file
echo "Free Swap: $(grep SwapFree /proc/meminfo | awk '{print $2 " " $3}')" >> $output_file
echo "" >> $output_file
## Memory usage percentage calculation
total_mem=$(grep MemTotal /proc/meminfo | awk '{print $2}')
used_mem=$(grep MemTotal /proc/meminfo | awk '{print $2}')
used_mem=$((used_mem - $(grep MemFree /proc/meminfo | awk '{print $2}')))
used_mem=$((used_mem - $(grep Buffers /proc/meminfo | awk '{print $2}')))
used_mem=$((used_mem - $(grep "^Cached:" /proc/meminfo | awk '{print $2}')))
mem_percentage=$((used_mem * 100 / total_mem))
echo "MEMORY USAGE SUMMARY:" >> $output_file
echo "Memory usage percentage: ${mem_percentage}%" >> $output_file
echo "" >> $output_file
echo "Memory summary report generated at $output_file"
Save the file by pressing Ctrl+O
, then Enter
, and exit with Ctrl+X
.
Make the script executable:
chmod +x ~/project/memory_data/create_summary.sh
Run the script to generate the summary report:
~/project/memory_data/create_summary.sh
After the script completes, view the generated summary report:
cat ~/project/memory_data/memory_summary_*
This comprehensive report gives you a detailed view of your system's memory status by combining data from multiple sources.
Creating a Simple Memory Status Command
Finally, let's create a simple one-line command that shows the current memory status in a concise format:
echo "Memory status: $(free -h | grep Mem | awk '{print "Total:"$2, "Used:"$3, "Free:"$4, "Available:"$7}')" > ~/project/memory_data/memory_status.txt
View the memory status:
cat ~/project/memory_data/memory_status.txt
This command extracts the most important information from the free
command output and presents it in a compact format.