Essential Linux Commands and Concepts

Commands to Access Documentation and View Processes

  • man: Displays the manual for any Linux command. For example:

      man ls
    

    This opens the manual for the ls command, explaining its usage and options.

  • ps -aux: Lists all running processes along with details such as:

    • Process ID (PID)

    • CPU and RAM usage

    • Command that initiated the process

  • top: Provides a dynamic, real-time view of system resources including:

    • CPU usage

    • RAM usage

    • Process priorities

    • Automatically refreshes at regular intervals.

  • htop: A more user-friendly and interactive version of top, offering better visualization and easier navigation.

Managing Process Priorities

  • nice: Assigns priority to a new process. Lower values indicate higher priority. Example:

      nice -n 10 my_process
    
  • renice: Adjusts the priority of an already running process. Example:

      renice -n 5 -p <PID>
    

    This changes the priority of the process identified by <PID>.

Memory Management Commands

  • free -m: Displays memory usage in megabytes, including:

    • Total memory

    • Used memory

    • Free memory

  • Cache: Memory occupied by previously used processes that still resides in RAM. To view or clear cache:

      cat /proc/sys/vm/drop_caches
    

Viewing CPU Information

  • lscpu: Displays detailed CPU information such as:

    • Number of cores

    • CPU architecture

    • Clock speed

Shell Scripting Essentials

Shell scripting automates repetitive tasks using Linux commands.

Key Commands and Techniques

  • echo: Prints text or variables to the terminal.

      echo "Hello, World!"
    
  • Setting Variables:

      x=5
      echo $x  # Prints the value of x
    
  • read: Accepts user input during script execution.

      read var
      echo $var
    
  • Backticks (`): Executes commands within a script.

      echo `date`  # Prints the current date
    
  • Escape Sequences (-e): Enables special characters in echo.

      echo -e "Line1\nLine2"  # Prints with a newline
    

Example Script

Here’s a simple script to demonstrate these concepts:

#!/bin/bash
# A simple script to demonstrate Linux commands

# Set a variable
name="User"

# Print a welcome message
echo "Hello, $name!"

# Display current date and time
echo "Current date and time: `date`"

# Ask for user input
read -p "Enter your favorite Linux command: " cmd

# Display the manual for the entered command
man $cmd

Summary

These Linux commands and shell scripting basics provide a foundation for managing processes, system resources, and automation. Mastery of these tools empowers users to optimize and streamline their workflows efficiently.