Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Comprehensive Guide to Linux Command Line Operations

Tech May 12 2

Linux vs. Windows

Linux offers several advantages over Windows in server and development environments:

  • Stability and Efficiency: Known for high uptime and efficient resource utilization.
  • Cost: Generally free and open-source, though enterprise support may incur costs.
  • Security: Fewer vulnerabilities and rapid patch deployment.
  • Multi-user/Multi-tasking: Designed natively to handle multiple users and processes simultaneously.
  • Permissions: A robust user and file permission strategy enhances security.
  • Embedded Systems: Ideal for small kernel embedded applications.

Directory Structure: Root vs. Home

Understanding the file system hierarchy is critical:

  • Root Directory (/): The top-level directory. You cannot navigate above this directory.
  • Home Directory (~): The designated directory for a specific user. For a regular user, this is typically /home/username. For the root user, it is /root.

When a user logs in, they start in their home directory. Regular users generally lack write perimssions in the root directory for security reasons, whereas the superuser (root) has full control.

The Command Line Interface

The command prompt provides context about the current session. For example:

[root@host01 ~]# pwd
/root

Prompt Breakdown:

  • root: The current username.
  • host01: The hostname of the machine.
  • ~: Indicates the current directory is the home directory.
  • #: Indicates a root user shell (standard users see $).

Essential Shortcuts

Efficiency in the shell relies heavily on keyboard shortcuts:

  • Tab: Auto-complete commands and file paths.
  • Ctrl + R: Search through command history.
  • Ctrl + L: Clear the screen.
  • Ctrl + C: Terminate the currently running process.
  • Ctrl + A / Ctrl + E: Jump to the start or end of the command line.
  • Ctrl + U / Ctrl + K: Cut text from the cursor to the beginning or end of the line.
  • Ctrl + Y: Paste previously cut text.

File and Directory Management

Navigation and Viewing

  • pwd (Print Working Directory): Displays the full path of the current directory.

  • ls (List): Lists directory contents. ``` ls -l # Detailed format ls -a # Include hidden files ls -t # Sort by modification time

  • cd (Change Directory): Moves between directories. ``` cd / # Go to root cd ~ # Go to home cd .. # Go up one level cd - # Switch to previous directory

    
    

File Creation and Text Manipulation

  • touch: Creates an empty file or updates timestamps. ``` touch new_file.txt

  • cat: Displays the entire content of a file. ``` cat -n server.log # Display with line numbers

  • Redirection Operators:

    • >: Overwrites file content.
    • >>: Appends content to the end of a file.
    echo "System Update" >> logs/status.txt
    
    
  • less: Allows paging through large files (search with /, quit with q).

  • head / tail: View the beginning or end of a file. Use tail -f to monitor logs in real-time. ``` tail -n 5 error.log # Show last 5 lines

  • mkdir: Creates directories. ``` mkdir -p project/src/main # Create nested directories recursively

    
    

File Operations

  • cp (Copy): Copies files or directories. ``` cp source.txt destination.txt cp -r folder/ backup/ # Recursive copy

  • mv (Move): Moves or renames files. ``` mv old_name.txt new_name.txt mv file.txt /tmp/

  • rm (Remove): Deletes files. Use with caution. ``` rm file.txt rm -rf directory/ # Force recursive delete (Dangerous)

    
    

User and Permission Management

User Administration

Linux is a multi-user system. The root user has unrestricted access.

  • sudo: Executes a command with superuser privileges.

  • useradd / userdel: Adds or removes user accounts. ``` sudo useradd newuser sudo passwd newuser sudo userdel -r newuser # Remove user and home directory

  • su: Switches to a different user. ``` su - admin # Switch to admin user

    
    

Group Management

  • groupadd / groupdel: Manages user groups.

  • groups: Shows which groups a user belongs to.

  • chown / chgrp: Changes file owner and group. ``` chown user:group file.txt chown -R user:group /data/ # Recursive change

    
    

File Permissions (chmod)

Permissions define read (r), write (w), and execute (x) access for the owner, group, and others.

Numeric Notation:

  • 4 = Read
  • 2 = Write
  • 1 = Execute
chmod 755 script.sh  # Owner: rwx (7), Group: rx (5), Others: rx (5)

Symbolic Notation:

chmod u+x script.sh   # Add execute for owner
chmod g-w file.txt     # Remove write for group

Searching and Text Processing

Searching Files

  • locate: Fast search using a database (requires updatedb). ``` locate config.json

  • find: Real-time search with powerful filtering. ``` find /var -name ".log" -size +10M find . -type f -name ".conf"

    
    

Text Utilities

  • sort: Sorts lines in a text file. ``` sort -r names.txt # Reverse sort sort -n numbers.txt # Numerical sort

  • wc (Word Count): Counts lines, words, and bytes. ``` wc -l report.txt # Count lines only

  • uniq: Filters out adjacent duplicate lines. ``` sort data.txt | uniq

    
    

Pipelines

Pipes (|) connect the output of one command to the input of another.

cat access.log | grep "ERROR" | wc -l

This example reads a log file, filters for "ERROR", and counts the occurrences.

Vim Editor Guide

Vim is a modal text editor. It operates primarily in three modes: Normal, Insert, and Command-line.

Opening and Modes

  • Opening: vim filename
  • Normal Mode: Default mode for navigation and commands (press Esc to return here).
  • Ensert Mode: For typing text (press i or a to enter).
  • Command-line Mode: For saving/exiting (press : in Normal mode).

Navigation in Normal Mode

  • h, j, k, l: Move left, down, up, right.
  • w / b: Jump forward/backward by word.
  • 0 / $: Jump to start/end of line.
  • gg / G: Jump to first/last line of file.
  • :20 + Enter: Jump to line 20.

Editing Operations

  • x: Delete character under cursor.
  • dd: Delete (cut) current line.
  • yy: Yank (copy) current line.
  • p / P: Paste below/after cursor.
  • u: Undo last change.
  • Ctrl + r: Redo.

Saving and Exiting

  • :w: Save.
  • :q: Quit.
  • :wq: Save and quit.
  • :q!: Force quit without saving.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.