0% found this document useful (0 votes)
6 views24 pages

Working with Text Files in Linux

Uploaded by

Jagdeep Singh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views24 pages

Working with Text Files in Linux

Uploaded by

Jagdeep Singh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Module 4

__________________________________

Working with Text Files


4.1 Introduction to Text Files in Linux

Why Text Files are Critical in Linux

• Configuration files: Most Linux configuration is done via text


files
• Log files: System and application logs are text-based
• Scripts: Shell scripts, Python, Perl, etc. are text files
• Data processing: Text manipulation is fundamental to Linux
administration
4.1 Introduction to Text Files in Linux
Common Text File Locations
• # Configuration files
• /etc/ # System-wide configs
• ~/.config/ # User-specific configs
• ~/.bashrc # Shell configuration

• # Log files
• /var/log/ # System logs
• /var/log/messages # General system messages
• /var/log/secure # Authentication logs

• # Documentation
• /usr/share/doc/ # Package documentation
• README, INSTALL # Common documentation files
4.2 Redirecting Output
Understanding Standard Streams
• stdin (0): Standard input (keyboard by default)
• stdout (1): Standard output (screen by default)
• stderr (2): Standard error (screen by default)
4.2 Redirecting Output
Basic Redirection Operators
• Output Redirection (> and >>)

• # > Overwrites file (creates if doesn't exist)


• echo "Hello World" > [Link]
• ls -l > [Link]

• # >> Appends to file


• echo "Line 1" >> [Link]
• echo "Line 2" >> [Link]
• date >> [Link]
4.2 Redirecting Output
Basic Redirection Operators
• Error Redirection (2> and 2>>)

• # Redirect only error messages


• command_that_fails 2> [Link]
• ls nonexistentfile 2> [Link]

• # Append errors
• command 2>> error_log.txt
4.2 Redirecting Output
Basic Redirection Operators
• Combining stdout and stderr (&> and &>>)

• # Redirect both stdout and stderr


• command &> output_and_errors.log
• ls *.txt nonexistent &> all_output.txt

• # Append both
• command &>> [Link]
4.3 Viewing File Contents
• cat - Concatenate and Display Files
• # Basic viewing
• cat [Link]

• # View multiple files


• cat [Link] [Link]

• # Number lines
• cat -n [Link]

• # Show non-printing characters


• cat -A [Link] # Shows tabs as ^I, ends with $

• # Common uses
• cat > [Link] # Create file (Ctrl+D to finish)
• cat file1 file2 > [Link]
4.3 Viewing File Contents
• head - View Beginning of File
• # First 10 lines (default)
• head [Link]

• # First N lines
• head -n 20 [Link]
• head -20 [Link] # Short form

• # First N bytes
• head -c 100 [Link]

• # Multiple files
• head -5 [Link] [Link]

• # Practical examples
• head -1 /etc/passwd # View first line
• head -n 50 /var/log/messages | less
4.3 Viewing File Contents
• tail - View End of File
• # Last 10 lines (default)
• tail [Link]
• # Last N lines
• tail -n 15 [Link]
• tail -15 [Link]
• # Follow file in real-time (monitor logs)
• tail -f /var/log/messages
• tail -f /var/log/secure
• # Follow with line numbers
• tail -n 20 -f /var/log/httpd/access_log
• # Multiple files
• tail -5 *.log
• # Practical examples
• # Monitor growing log file
• tail -f /var/log/nginx/[Link]
• # Show last 100 lines then continue following
• tail -n 100 -f [Link]
4.3 Viewing File Contents
• Comparing Viewers
• # When to use each:
• cat # Small files, combining files, creating files
• less # Reading large files, searching within files
• more # Simple viewing (less is better)
• head # Checking file beginnings
• tail # Checking file ends, monitoring logs
4.4 Basic File Statistics
• wc - Word Count
• # Count lines, words, characters
• wc [Link]
• # Output: lines words characters filename
• # Count only lines
• wc -l [Link]
• # Count only words
• wc -w [Link]
• # Count only characters
• wc -c [Link]
• # Multiple files
• wc *.txt
• # Count all files in directory
• wc /etc/hosts /etc/passwd /etc/group
• # Practical examples
• wc -l /var/log/messages # Count log entries
4.5 Editing Files with vim
• Why vim?
• Ubiquitous: Available on almost all Unix/Linux systems
• Powerful: Extremely efficient for text editing
• Lightweight: Works over slow connections
• Modal: Different modes for different tasks
4.5 Editing Files with vim
• Basic vim Modes
• 1. Command Mode (Normal Mode) - Default mode for navigation
• 2. Insert Mode - For typing text
• 3. Visual Mode - For selecting text
• 4. Command-Line Mode - For entering commands
4.5 Editing Files with vim
• Starting and Exiting vim
• # Open file
• vim [Link]
• vi [Link] # vi is usually linked to vim

• # Exit commands (from Command Mode)


• :q # Quit (if no changes)
• :q! # Quit without saving changes
• :w # Save
• :wq or :x # Save and quit
• ZZ # Save and quit (Shift+ZZ)
4.5 Editing Files with vim
• Navigation in Command Mode
• Basic Movement:
• h, j, k, l # Left, Down, Up, Right
• Arrow keys # Also work
• Word Movement:
• w # Next word
• b # Previous word
• e # End of word
• Line Movement:
• 0 # Beginning of line
• $ # End of line
4.5 Editing Files with vim
• Navigation in Command Mode
• Screen Movement:
• Ctrl+f # Forward one screen
• Ctrl+b # Back one screen
• Ctrl+d # Down half screen
• Ctrl+u # Up half screen
• H # Top of screen
• M # Middle of screen
• L # Bottom of screen
• File Movement:
• gg # First line of file
• G # Last line of file
• 50G # Go to line 50
• :50 # Go to line 50 (command line)
4.5 Editing Files with vim
• Entering Insert Mode
• # From Command Mode:
• i # Insert before cursor
• a # Append after cursor
• I # Insert at beginning of line
• A # Append at end of line
• o # Open new line below
• O # Open new line above

• # Return to Command Mode:


• Esc # Escape key
• Ctrl+[ # Alternative
4.5 Editing Files with vim
• Editing Commands
• # Deleting:
• x # Delete character under cursor
• dw # Delete word
• dd # Delete line
• 5dd # Delete 5 lines
• d$ # Delete to end of line
• d0 # Delete to beginning of line

• # Copying (yank) and Pasting:


• yy # Yank (copy) line
• 5yy # Yank 5 lines
• yw # Yank word
• p # Paste after cursor
• P # Paste before cursor
4.5 Editing Files with vim
• Editing Commands
• # Undo/Redo:
• u # Undo
• Ctrl+r # Redo

• # Change/Replace:
• r # Replace single character
• cw # Change word
• cc # Change line
• C # Change to end of line
• s # Substitute character and enter insert mode
4.5 Editing Files with vim
• Search and Replace
• # Searching (Command Mode):
• /pattern # Search forward
• ?pattern # Search backward
• n # Next match
• N # Previous match
• * # Search for word under cursor

• # Search and replace (Command-Line Mode):


• :s/old/new/ # Replace first occurrence in line
• :s/old/new/g # Replace all in line
• :%s/old/new/g # Replace all in file
• :%s/old/new/gc # Replace with confirmation
• :5,10s/old/new/g # Replace between lines 5-10
4.5 Editing Files with vim
• Saving and File Operations
• # File operations:
• :w # Save
• :w [Link] # Save as
• :w! # Force save (read-only files)
4.6 Alternative Text Editors
• nano - Simple Editor for Beginners
• # Open file
• nano [Link]

• # Common nano shortcuts (displayed at bottom):


• Ctrl+O # Write (save) file
• Ctrl+X # Exit
• Ctrl+W # Search
• Ctrl+\ # Search and replace
• Ctrl+K # Cut line
• Ctrl+U # Paste
• Ctrl+6 # Start selection
• Ctrl+G # Get help

• # Open with line numbers


• nano -l [Link]
4.6 Alternative Text Editors
• gedit - Graphical Editor
• # Open in GUI
• gedit [Link] &

• # Open from terminal (stays in background)


• gedit [Link]

You might also like