Skip to main content

Command Palette

Search for a command to run...

Essential Linux Commands and Shell Scripting Tips

Master Linux Commands and Scripting: From Beginner to Expert

Updated
3 min read
Essential Linux Commands and Shell Scripting Tips
M

I am deeply committed to continuous learning and keeping up with the latest developments in DevOps, ML, and cloud technologies. Beyond my technical skills, I actively engage in open-source contributions and participate in the broader tech community. I am also passionate about mentoring and helping others harness technology to drive positive impact.

In this blog post, we'll cover some essential Linux commands and shell scripting techniques that I recently learned. These commands are incredibly useful for managing files, monitoring system resources, and automating tasks. Let's dive in!

Creating and Editing Files

To create and edit files in Linux, you can use the vi editor or the touch command:

  • vi filename: Opens or creates a file for editing.

    • Press i to start writing.

    • Press Esc to stop writing and enter command mode.

    • Use :wq to save and exit, :q! to exit without saving, or :w to save only.

  • touch filename: Creates a new empty file.

Basic File and Directory Management

Here are some fundamental commands for managing files and directories:

  • ls: Lists files and folders in the current directory.

  • ls -ltr: Lists files with detailed information, sorted by modification time.

  • man command: Displays the manual for any command, e.g., man ls or man touch.

Making Files Executable

To make a file executable, you can use the following:

  • ./filename or sh filename: Executes the file if it has executable permissions.

chmod: Changes file permissions.

  • chmod 777 filename: Grants read, write, and execute permissions to everyone.

  • Permissions are represented as 4 (read), 2 (write), 1 (execute). The three digits specify permissions for the user, group, and others, respectively. So, "7" means you are granting all three permissions. "777" means the first "7" is granting permission to the user, the second one is granting permission to the group, and the third is granting permission to others.

Checking System Resources

Monitor your system's health using these commands:

  • nproc: Displays the number of CPU cores.

  • free: Shows memory usage.

  • top: Provides a dynamic view of system processes and resource usage.

  • df: Prints disk space usage.

Managing process

You can manage and inspect system processes with these commands:

  • ps: Lists current running processes.

  • ps -ef: Lists all processes with detailed information.

  • ps -ef | grep "process_name": Filters processes by name using grep.

    • Example: ps -ef | grep "amazon" filters processes containing "amazon".
  • ps -ef | grep "amazon" | awk -F" " '{print $2}': Extracts the process ID from the second column.

Searching and Analyzing logs

Use these commands to search for errors in log files:

  • cat logfile | grep error: Searches for the word "error" in a log file.

  • curl log_url | grep ERROR: Fetches a log file from a URL and filters for "ERROR" messages.

  • wget log_url: Downloads a log file from a URL.

User Management and Permissions

Switch users and manage permissions with the following commands:

  • su -: Switches to a different user.

  • sudo su -: Gains root user access.

  • sudo find / -name "filename": Searches for files by name across the system.

Handling Signals and Traps

Traps can be set to handle signals like interrupt signals (Ctrl+C):

  • trap "echo don't use Ctrl+C" SIGINT: Prints a message when Ctrl+C is pressed.

  • trap "rm -rf *" SIGINT: (Be cautious) Removes all files when Ctrl+C is pressed.

These commands are fundamental to Linux system administration and shell scripting. They allow you to effectively manage files, processes, and system resources. By practicing regularly, you'll soon become proficient at the command line! While these aren't all the commands you'll need to master to become an expert in shell scripting, they serve as a great starter pack to kickstart your journey. Keep learning :o