Linux File Operations Basics Introduction

File Operations Basics - File Management Fundamentals

In Linux professional work, file operations are the most frequently used skills. In this basics edition, you can safely learn basic commands like cp, mv, rm, cat along with dangerous pitfalls and common mistakes.

⚠️ Important Notice

Commands covered in this article include dangerous operations that permanently delete data. Before working in production environments, practice thoroughly in a safe practice environment.

Table of Contents

  1. cp - Safe and Reliable File Copying
  2. mv - Practical File Move/Rename Techniques
  3. rm - Deletion Basics and the Terror of rm -rf
  4. cat/less/more - Choosing File Content Viewing Tools

1. cp - Safe and Reliable File Copying

The cp command stands for "copy" and copies files or directories. In professional work, it's a critical command frequently used for backup creation and environment setup.

Basic Syntax

cp [options] source destination

πŸ“„ Basic File Copy

$ cp original.txt backup.txt

Copy original.txt as backup.txt

πŸ“ Copy to Directory

$ cp document.pdf ~/Documents/

Copy document.pdf to Documents directory

⭐ Essential Options for Professional Work

πŸ”’ -i (interactive): Overwrite Confirmation

Essential option in professional work. Asks for confirmation before overwriting existing files.

$ cp -i source.txt target.txt
overwrite target.txt? (y/n)

πŸ’‘ Pro Tip: Set alias cp='cp -i' to always get confirmation

πŸ“‚ -r (recursive): Recursive Directory Copy

Copies directories and all their contents.

$ cp -r project_folder/ backup_folder/

Note: Copying large directories takes time. Check the size beforehand.

$ du -sh project_folder/

πŸ“Š -v (verbose): Detailed Display

Displays copy operation details. You can see what's being copied.

$ cp -v *.txt backup/
'file1.txt' -> 'backup/file1.txt'
'file2.txt' -> 'backup/file2.txt'

πŸ•’ -p (preserve): Preserve Attributes

Preserves timestamps and permissions when copying. Important for backups.

$ cp -p important.log backup_important.log

🎯 Common Combinations in Professional Work

πŸ“„ Safe File Backup

$ cp -ivp config.conf config.conf.backup

Explanation: Safe backup with confirmation(-i), verbose(-v), preserve attributes(-p)

πŸ“ Duplicate Entire Project

$ cp -rvp project/ project_backup/

Explanation: Copy entire project with recursive(-r), verbose(-v), preserve(-p)

🎯 Create Configuration Template

$ cp sample.conf.template actual.conf

Explanation: Create actual configuration file from template

⚠️ Common Mistakes and Pitfalls

❌ Mistake: Forgetting -r for directory copy

$ cp folder/ backup/
cp: -r not specified; omitting directory 'folder/'

Correct: cp -r folder/ backup/

❌ Mistake: Destroying important files without overwrite confirmation

$ cp temp.txt important.txt  # important.txt gets overwritten!

Correct: cp -i temp.txt important.txt

2. mv - Practical File Move/Rename Techniques

The mv command stands for "move" and performs file moves and renames. Unlike cp, the original file is deleted, requiring more careful operations.

Basic Syntax

mv [options] source destination

🏷️ File Rename

$ mv old_name.txt new_name.txt

Change filename from old_name.txt to new_name.txt

πŸ“ File Move

$ mv document.pdf ~/Documents/

Move document.pdf to Documents directory

⭐ Critical Options for Professional Work

πŸ”’ -i (interactive): Overwrite Confirmation

Overwrite confirmation is essential for mv too. Prevents accidents when destination file exists.

$ mv -i source.txt target.txt
overwrite target.txt? (y/n)

πŸ“Š -v (verbose): Detailed Display

Confirms what was moved. Especially useful when handling multiple files.

$ mv -v *.log archive/
'access.log' -> 'archive/access.log'
'error.log' -> 'archive/error.log'

🚫 -n (no-clobber): Prevent Overwrite

Use when you absolutely don't want to overwrite existing files.

$ mv -n temp.txt important.txt  # Does nothing if important.txt exists

🎯 Common Patterns in Professional Work

πŸ—‚οΈ File Organization/Classification

# Classify files by extension
$ mv *.jpg images/
$ mv *.pdf documents/
$ mv *.txt text_files/

πŸ“… Rename to Dated Filename

$ mv report.txt report_$(date +%Y%m%d).txt

Result: report.txt β†’ report_20250111.txt

πŸ”„ Create Backup File and Replace

# Backup current config then replace with new one
$ mv config.conf config.conf.old
$ mv new_config.conf config.conf

πŸ›‘οΈ Safety Measures When Using mv

1. Habit of Prior Confirmation

# Check target files before moving
$ ls -la source_files/
$ ls -la destination/

# Execute move
$ mv -iv source_files/* destination/

2. Backup Important Files

# Backup important files before moving
$ cp -p important.txt important.txt.backup
$ mv important.txt new_location/

3. Execute Critical Operations Step by Step

# Don't move large quantities at once, test gradually
$ mv -v file1.txt backup/  # First, just one
$ mv -v file*.txt backup/  # If successful, move the rest

3. rm - Deletion Basics and the Terror of rm -rf

The rm command stands for "remove" and deletes files or directories. Once deleted, recovery is basically impossible, making it one of the most dangerous Linux commands.

Basic Syntax

rm [options] file/directory

πŸ“„ Basic File Deletion

$ rm file.txt

Delete file (normally without confirmation)

🚨 rm -rf : System-Destroying Dangerous Command

rm -rf has "nuclear weapon" level danger. One mistake can destroy entire systems.

πŸ’€ Actual Disaster Examples

  • GitLab's 12-hour Data Loss Incident: Admin's rm -rf error
  • Pixar's Toy Story 2 Deletion: Accidental rm -rf * execution
  • Multiple Companies' Complete DB Deletion: Script variable errors

⚑ Particularly Dangerous Patterns

$ rm -rf /     # Delete entire system
$ rm -rf /*    # Delete everything under root
$ rm -rf $HOME # Delete entire home directory
$ rm -rf ./*   # Delete everything in current directory

πŸ›‘οΈ Safe rm Usage

πŸ”’ -i (interactive): Confirmation Before Deletion

Essential when using rm. Asks for confirmation before deleting each file.

$ rm -i file.txt
remove file.txt? (y/n)

πŸ“‚ -r (recursive): Directory Deletion

Deletes directory and its contents. Always combine with -i.

$ rm -ri old_project/

Absolutely avoid: rm -rf (recursive delete without confirmation)

πŸ“Š -v (verbose): Display Deletion Contents

Displays what's being deleted. Important for safety confirmation.

$ rm -iv *.tmp
removed 'temp1.tmp'
removed 'temp2.tmp'

🚨 Essential Checks Before Using rm

Safety Confirmation Procedure Before Deletion

  1. Check Current Location
    $ pwd
  2. Clearly Confirm Deletion Target
    $ ls -la target_files*
  3. Check for Important Files
    # Check if important files are included
    $ ls -la | grep important
  4. Verify Backup Exists
    $ ls -la *.backup
  5. Execute Step-by-Step Deletion
    # First test delete one file
    $ rm -iv test_file.txt
    # If no problem, delete the rest
    $ rm -iv other_files*

πŸ’‘ Safer Alternatives to rm

πŸ—‘οΈ Move to Trash (trash-cli)

Move to trash instead of permanent deletion. Recoverable.

# Install trash-cli (Ubuntu/Debian)
$ sudo apt install trash-cli

# Move file to trash
$ trash file.txt

# Check trash contents
$ trash-list

# Restore from trash
$ trash-restore

πŸ“ Move to Deletion Directory

Move to dedicated directory before deletion. Delete after manual confirmation.

# Create deletion directory
$ mkdir ~/.to_delete

# Move files to deletion directory
$ mv unwanted_files* ~/.to_delete/

# Actually delete after checking later
$ rm -ri ~/.to_delete/*

πŸ›‘οΈ Additional Measures for System Protection

Improve Safety with Alias Settings

# Add to ~/.bashrc
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

Protect Important Directories

# Make important directory read-only
$ chmod -w important_directory/

4. cat/less/more - Choosing File Content Viewing Tools

There are multiple commands for displaying file contents, but each has different characteristics and appropriate use cases. In professional work, it's important to choose appropriately based on file size and purpose.

cat - Simple Full Content Display

cat stands for "concatenate" and displays file contents concatenated together.

πŸ“„ Basic File Display

$ cat config.txt

Display entire file contents at once

⚠️ Cautions When Using cat

Don't use cat for large files!

$ cat huge_log.txt  # Danger! Terminal may freeze

Files over several MB may freeze the terminal or slow down the system.

less - Professional's Favorite High-Function Viewer

less is a highly functional command for viewing files page by page.

πŸ“Š Safe Viewing of Large Files

$ less large_file.log

Display contents page by page, memory efficient

Operation Keys in less

Key Action Usage Frequency
Space or f Next page β˜…β˜…β˜…
b Previous page β˜…β˜…β˜…
/string Forward search β˜…β˜…β˜…
?string Backward search β˜…β˜…
n Next search result β˜…β˜…β˜…
N Previous search result β˜…β˜…
g Go to file beginning β˜…β˜…
G Go to file end β˜…β˜…β˜…
q Quit β˜…β˜…β˜…

Usage Guidelines

πŸ“ When to Use cat

  • Small config files (under 100 lines)
  • When passing entire file contents to other commands
  • When concatenating multiple files
$ cat small_config.txt
$ cat file1.txt file2.txt > combined.txt

πŸ“Š When to Use less

  • Viewing large log files
  • When searching within files
  • When wanting to save memory
  • When wanting vi-like operations
$ less /var/log/system.log
$ less +G access.log  # Display from end

πŸ—ΊοΈ No Need to Use more

more is a downgraded version of less and is rarely used nowadays. Always use less.

πŸ’‘ Professional File Viewing Techniques

Check File Size Beforehand

# Check size before using cat
$ ls -lh large_file.txt
-rw-r--r-- 1 user group 50M Jan 11 10:00 large_file.txt
# 50MB so use less
$ less large_file.txt

Combining with Real-time Log Monitoring

# Check latest logs with less, then monitor with tail -f
$ less +G /var/log/app.log  # Display from end
# After exiting with q
$ tail -f /var/log/app.log  # Real-time monitoring

🎯 Next Steps

In the Basics edition, you learned basic file operations with cp, mv, rm, cat and safety measures.

In the next Advanced edition, you'll master more advanced file analysis and processing techniques including head, tail, file, stat, and pipes.

πŸ“ Complete File Operations Series

  1. Basics Edition (This Article) - Basic operations and safety measures for cp, mv, rm, cat
  2. Advanced Edition - head, tail, file, stat, pipes, practical exercises

πŸ”— Related Articles