Familiarization of basic Linux Commands- cpuinfo , meminfo, du and fdisk

 cpuinfo 

The cpuinfo command in Linux is not a standalone command but refers to a way of accessing detailed information about your system's CPU (Central Processing Unit). This information is crucial for understanding your processor's capabilities, such as its make, model, speed, and features.

How to Access CPU Information?

To view detailed information about the CPU, you typically read the contents of a special file called /proc/cpuinfo. The /proc directory is a virtual filesystem in Linux that provides system and process information.

You can access this file using commands like cat, less, or grep.

Basic Command to Display CPU Info

    cat /proc/cpuinfo

What Does /proc/cpuinfo Contain?

When you run the above command, it displays detailed information about each processor core on your system. Let’s break down the key sections of the output.

Example Output

Here’s an example of what you might see:

processor       : 0

vendor_id       : GenuineIntel

cpu family      : 6

model           : 158

model name      : Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz

stepping        : 10

microcode       : 0xca

cpu MHz         : 2600.000

cache size      : 12288 KB

physical id     : 0

siblings        : 12

core id         : 0

cpu cores       : 6

apicid          : 0

initial apicid  : 0

fpu             : yes

flags           : fpu vme de pse tsc msr pae mce cx8 sep pge ...

Understanding the Key Fields

Here’s a breakdown of the key fields in the output:

  1. processor:

    • This is the ID of the processor or core.
    • For example, if you have a quad-core CPU, you’ll see processor: 0, processor: 1, etc.
  2. vendor_id:

    • The manufacturer of the CPU.
    • Common values: GenuineIntel (Intel) or AuthenticAMD (AMD).
  3. cpu family:

    • A number representing the generation of the CPU.
  4. model:

    • The specific model number of the CPU within the family.
  5. model name:

    • The full name of the CPU, including its brand and speed.
    • Example: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz.
  6. cpu MHz:

    • The speed of the CPU in MHz (megahertz).
    • This can vary based on power-saving modes or load.
  7. cache size:

    • The size of the L2 or L3 cache, measured in KB.
  8. cpu cores:

    • The total number of physical cores in the CPU.
  9. siblings:

    • The total number of logical processors, including hyper-threading.
  10. flags:

    • A list of features or instructions the CPU supports.
    • Examples: sse, avx, vmx (virtualization), etc.

How to Filter Information?

If the output is too long, you can filter it to see specific details.

1. Find the CPU Model Name

    grep "model name" /proc/cpuinfo

2. Count the Number of Cores

    grep -c "processor" /proc/cpuinfo

3. Check CPU Cache Size

    grep "cache size" /proc/cpuinfo

Practical Uses of /proc/cpuinfo

1.Check CPU Compatibility:

  • Identify whether your CPU supports specific features (e.g., virtualization, AVX, or SSE).
  • Example: Look for vmx in the flags section.
    grep "flags" /proc/cpuinfo | grep vmx

 2.Monitor Performance:

  • Ensure the CPU is running at the expected speed.

 3.Diagnose Hardware Issues:

  • Compare the CPU model and cache size to specifications to check for hardware mismatches.

4.System Optimization:

  • Determine the number of cores to configure multi-threaded applications.

Alternative Tools to View CPU Info

For beginners, using /proc/cpuinfo can be a bit overwhelming. Here are simpler commands/tools:

lscpu Command:
    • Provides a concise summary of CPU details.

Graphical Tools:

  • If you prefer a GUI, tools like htop or sysinfo also display CPU details.

Summary

The /proc/cpuinfo file is a goldmine of information about your CPU. Using commands like cat, grep, and lscpu, you can easily check the number of cores, speed, and features your CPU supports. It's especially useful for configuring systems, diagnosing issues, and optimizing performance.

meminfo

This is an important file that provides detailed information about your computer's memory usage, including how much memory is installed, used, available, and much more.

What is /proc/meminfo?

  • /proc/meminfo is a virtual file in Linux that contains real-time memory statistics.
  • It is located in the /proc directory, which is a special directory containing system and process information.
  • The file is automatically updated by the Linux kernel and reflects the current state of your system's memory.

How to View /proc/meminfo?

You can use the cat command to display the content of this file in your terminal. Here’s how:

Command:

    cat /proc/meminfo
MemTotal:       16392696 kB
MemFree:        12345678 kB
MemAvailable:   14000000 kB
Buffers:          987654 kB
Cached:          7654321 kB
SwapTotal:       2097152 kB
SwapFree:        2097152 kB
Dirty:               102 kB
Writeback:            0 kB
AnonPages:       1234567 kB
Mapped:           987654 kB
Shmem:            876543 kB
Slab:            5432101 kB

 Breaking Down the Output

Let’s take it line by line and understand what each term means.

1. MemTotal

  • What it means: Total amount of physical memory (RAM) installed on your computer.
  • Why it matters: This is the maximum memory your system can use.
  • Example: MemTotal: 16392696 kB (This shows you have about 16GB of RAM).

2. MemFree

  • What it means: The amount of memory that is completely free and not being used by the system.
  • Why it matters: Indicates how much unused memory is available right now.
  • Example: MemFree: 12345678 kB (~12GB of RAM is unused).

3. MemAvailable

  • What it means: The amount of memory available for applications, considering buffers and cache can be reused.
  • Why it matters: This value is a better indicator of how much memory is truly available for new processes.
  • Example: MemAvailable: 14000000 kB (~14GB of memory is ready to use).

4. Buffers

  • What it means: Memory used for temporary storage of data being written to or read from disk.
  • Why it matters: Buffers speed up disk operations by storing intermediate data.
  • Example: Buffers: 987654 kB (~987MB used for buffering).

5. Cached

  • What it means: Memory used to cache files for faster access.
  • Why it matters: Cached memory is used to speed up access to frequently used files. This memory can be freed if needed.
  • Example: Cached: 7654321 kB (~7.6GB of files are cached).

6. SwapTotal

  • What it means: Total amount of swap space available.
  • Why it matters: Swap is virtual memory stored on disk that acts as an overflow when RAM is full. However, it's slower than RAM.
  • Example: SwapTotal: 2097152 kB (~2GB of swap space available).

7. SwapFree

  • What it means: Amount of swap space that is currently unused.
  • Why it matters: Indicates if your system is relying on swap due to insufficient RAM.
  • Example: SwapFree: 2097152 kB (~2GB of swap is still free).
8. Dirty
  • What it means: Memory pages that have been modified but not yet written to disk.
  • Why it matters: Small values are normal; large values might indicate disk performance issues.
  • Example: Dirty: 102 kB.

9. AnonPages

  • What it means: Memory used by applications (anonymous pages not backed by files).
  • Why it matters: Reflects how much memory your running programs are consuming.
  • Example: AnonPages: 1234567 kB (~1.2GB of application memory usage).

10. Slab

  • What it means: Memory used by the Linux kernel for managing internal data structures.
  • Why it matters: Helps you understand kernel memory usage.
  • Example: Slab: 5432101 kB (~5.4GB is used by the kernel).

Practical Uses of /proc/meminfo

  • Monitor memory usage: Check how much memory is free, used, or available.
  • Diagnose performance issues: Low MemFree and high SwapFree values indicate your system is low on RAM.
  • Analyze cache and buffer usage: Cached and buffered memory are essential for speeding up processes.
  • Troubleshoot swap: Identify if your system is relying heavily on swap space, which can slow it down.

Filtering Specific Information

If you want to see specific memory details, you can use the grep command.

Example 1: Check Total, Free, and Available Memory

grep -E "MemTotal|MemFree|MemAvailable" /proc/meminfo

Example 2: Check Swap Memory

grep -E "SwapTotal|SwapFree" /proc/meminfo

Simpler Alternatives to /proc/meminfo

If /proc/meminfo looks overwhelming, you can use these commands for a summary:

1. Free Command

    free -h

2. Top or Htop

Use real-time monitoring tools like: top htop

Conclusion

The /proc/meminfo file is a great tool for understanding how memory is being used on your Linux system. It provides detailed information about physical memory, swap, buffers, cache, and kernel usage. With this knowledge, you can monitor system performance, troubleshoot issues, and optimize applications.

du command- disk usage

du command is one of the most useful tools in Linux for checking disk usage. This command is perfect for finding out how much space files and directories are taking up on your system.

What is the du Command?

  • du stands for Disk Usage.
  • It shows the amount of disk space used by files and directories.
  • By default, it provides the size of directories and their contents in kilobytes (KB).
Syntax
du [OPTIONS] [FILE or DIRECTORY]

If no directory or file is specified, du checks the current directory.

Basic Usage

1. View Disk Usage of the Current Directory

    du

2. View Disk Usage of a Specific Directory

    du /path/to/directory

Common Options in du

Let’s make the command more user-friendly with some helpful options!

1. Show Human-Readable Sizes

To see sizes in KB, MB, or GB, use the -h option:

    du -h

2. Summarize Total Size

To get only the total size of a directory, use the -s option:

    du -sh

3. Display Individual Files

To see the size of all files and directories, use the -a option:

    du -ah

4. Exclude Specific Files or Directories

You can exclude certain items using the --exclude option:

    du -h --exclude="*.log"

Example Use Case:

  • Exclude all log files while analyzing disk usage.

5. Limit Depth of Directory Display

Use the --max-depth option to restrict how deeply du analyzes subdirectories:

    du -h --max-depth=1

6. Sort Results by Size

Combine du with the sort command to display results from smallest to largest:

    du -h | sort -h

7. Finding Large Directories

    du -h --max-depth=1 | sort -rh

Conclusion

The du command is a powerful tool for understanding how disk space is being used on your system. It’s simple to use, and with the right options, it can provide valuable insights into disk usage.

fdisk command - Fixed Disk

fdisk command is used to manage or partition your hard drives.

What is the fdisk Command?

  • fdisk stands for Fixed Disk.
  • It is a text-based tool used to create, delete, and manage partitions on a storage device like a hard disk or SSD.
  • It supports MBR (Master Boot Record) partitioning.

⚠️ Warning: Since fdisk modifies disk partitions, always proceed with caution. Incorrect usage can lead to data loss!

When to Use fdisk?

  • Adding a new hard drive to your system.
  • Creating or resizing partitions for installing multiple operating systems.
  • Removing unused or corrupt partitions.

Syntax of fdisk

The general syntax is:

fdisk [options] [device]

For example:

    fdisk /dev/sda

Here, /dev/sda refers to the first hard disk on your system.

Basic Workflow of fdisk

Let’s go through the typical steps to use fdisk effectively.

1. List All Available Disks

Before working with partitions, identify the disks available on your system:

    fdisk -l

  • Each disk is represented by a device file like /dev/sda or /dev/sdb.
  • Use this command to ensure you’re selecting the correct disk.

  • 2. Open a Disk in fdisk

    To manage a specific disk, specify its device name:

        fdisk /dev/sda

    You’ll enter an interactive mode where you can perform various actions.

    3. Display Current Partition Table

    In fdisk interactive mode, type:

    p

    This shows the current partitions on the selected disk.

    4. Create a New Partition

    To create a new partition, follow these steps:

    1. Press n to create a new partition.
    2. Choose the partition type:
      • p for primary partition.
      • e for extended partition.
    3. Specify the partition number (e.g., 1, 2).
    4. Enter the starting and ending sectors or press Enter for defaults.
    Command (m for help): n
    Partition type:
       p  primary (2 primary, 0 extended, 2 free)
       e  extended
    Select (default p): p
    Partition number (1-4, default 3): 3
    First sector (1050624-78125000, default 1050624): [Press Enter]
    Last sector, +sectors or +size{K,M,G,T,P} (1050624-78125000, default 78125000): +10G

    5. Delete a Partition

    To delete a partition:

    1. Press d.
    2. Enter the partition number to delete (e.g., 1, 2, etc.).
    Command (m for help): d
    Partition number (1,2, default 2): 2

    6. Write Changes to Disk

    Once you’ve created, deleted, or modified partitions, save your changes by typing:

    w

    This writes the new partition table to the disk and exits fdisk.

    7. Exit Without Saving

    If you want to exit without making changes, type:

    q

    Real-Life Use Cases

    1. Setting Up a New Disk

      • You’ve installed a new SSD and need to partition it before use.
    2. Repartitioning a Disk

      • If your disk has unallocated space, you can create new partitions to utilize it.
    3. Troubleshooting Boot Issues

      • You might need to fix or modify a boot partition.

    Conclusion

    The fdisk command is a powerful tool for disk partitioning. While it’s a bit advanced, practice will make it easier to use. Remember to always double-check your commands to avoid unintentional data loss.

    Comments

    Popular posts from this blog

    IT Workshop GXESL208 KTU BTech 2024 Scheme - Dr Binu V P

    Familiarization of basic Linux Commands- ls, mkdir, rmdir , rm, cat, cp, mv , chmod