Skybinary

View Categories

1.2.1 Linux Commands For Linux File Mangement

3 min read

Linux File Mangement #

Editing files is one of the most essential tasks in Linux system administration. Configuration files, scripts, and logs often need modification, analysis, or formatting. Linux provides both command-line tools and text editors to perform these tasks efficiently.


1. sed (Stream Editor) #

Purpose:
sed is a non-interactive text stream editor used for filtering and transforming text in files or streams. It processes input line by line and applies operations such as substitution, deletion, and insertion.

Key Features:

  • Works in non-interactive mode (no need to open the file manually).
  • Supports powerful regular expressions for pattern matching.
  • Ideal for automation and scripting.

Common Syntax:

sed 's/pattern/replacement/' filename

Examples:

  • Replace the first occurrence of “error” with “warning” in each line: sed 's/error/warning/' logfile.txt
  • Replace all occurrences (global): sed 's/error/warning/g' logfile.txt
  • Delete lines containing a specific word: sed '/debug/d' logfile.txt

Usage in Administration:
Used in scripts to modify configuration files or log outputs automatically.


2. awk (Pattern Scanning and Processing Language) #

Purpose:
awk is a powerful text-processing language used for extracting, analyzing, and reporting data from files or input streams.

Key Features:

  • Reads files line by line and splits them into fields.
  • Supports arithmetic, string operations, and conditional statements.
  • Commonly used for report generation and data manipulation.

Common Syntax:

awk 'pattern { action }' filename

Examples:

  • Print the first column from a space-separated file: awk '{print $1}' data.txt
  • Print specific fields from /etc/passwd: awk -F: '{print $1, $7}' /etc/passwd
  • Print lines where the third field is greater than 50: awk '$3 > 50' file.txt

Usage in Administration:
awk is used to analyze logs, process CSV data, and generate reports on system performance or user activity.


3. printf Command #

Purpose:
printf is used to format and print text output. It’s similar to the printf function in C programming and offers better control than echo.

Key Features:

  • Supports formatted output using placeholders.
  • Works both in shell scripts and interactively.

Common Syntax:

printf "format string" arguments

Examples:

  • Simple formatted output: printf "User: %s\nShell: %s\n" "$USER" "$SHELL"
  • Print with padding and alignment: printf "%-10s %-10s\n" Name Score printf "%-10s %-10d\n" Alice 90 printf "%-10s %-10d\n" Bob 80

Usage in Administration:
Used in shell scripts for clear, readable output or for generating structured text reports.


4. nano (Command-Line Text Editor) #

Purpose:
nano is a beginner-friendly, command-line text editor. It’s easy to use and ideal for quick file edits directly in the terminal.

Key Features:

  • On-screen shortcut help at the bottom.
  • Supports search, cut/copy/paste, and syntax highlighting.
  • No need to learn complex commands (unlike vi or vim).

Basic Commands:

  • Open a file: nano filename
  • Save and exit:
    • Ctrl + O → Save (Write Out)
    • Ctrl + X → Exit
  • Search text: Ctrl + W

Usage in Administration:
Used for quick edits to configuration files like /etc/hosts, /etc/fstab, etc.


5. vi and vim (Visual Editors) #

Purpose:
vi is one of the oldest and most powerful text editors on Unix-like systems. vim (Vi IMproved) is an enhanced version with advanced features like syntax highlighting, undo levels, and plugin support.

Modes in vi/vim:

  1. Normal Mode – Navigate, delete, copy, or paste text.
  2. Insert Mode – Edit or insert text.
  3. Command Mode – Execute commands (save, quit, search, etc.).

Common Commands:

TaskCommand
Enter Insert Modei
Save File:w
Quit:q
Save and Quit:wq
Quit without Saving:q!
Delete a linedd
Copy a lineyy
Pastep
Search text/word

Usage in Administration:
vim is preferred by advanced users and system administrators for editing system files, scripts, and configuration files on remote servers via SSH.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top