Data Replication, Transfer, and Removal
Replicating Data with cp
# Syntax
cp [options] source destination
cp [options] source... target_directory
# Options
-r, -R # Copy directories recursively
-i # Prompt before overwriting
-u # Copy only when the source is newer or destination is missing
-v # Verbose output
-p # Preserve attributes like permissions and timestamps
-a # Archive mode, preserving everything and copying recursively
# Implementations
cp document.txt /var/tmp/ # Move a single file to a temporary directory
cp -r folder_one/ folder_two/ # Duplicate a directory tree
cp -i crucial_data.txt archives/ # Ask for confirmation on overwrite
cp -av /home/admin/documents/ /mnt/backup/ # Create a full backup preserving attributes
Transferring and Renaming via mv
# Syntax
mv [options] source destination
mv [options] source... target_directory
# Options
-i # Prompt before overwriting
-u # Move only if the source is newer
-v # Verbose output
-f # Force overwrite without prompting
# Implementations
mv old_report.txt new_report.txt # Change a file's name
mv document.txt /var/tmp/ # Transfer a file into a directory
mv *.jpeg ~/Images/ # Move all JPEG files
mv -i report.txt /home/admin/docs/ # Interactive overwrite prevention
Removing Items with rm
# Syntax
rm [options] file...
rmdir [options] empty_directory # Removes only empty directories
# Options
-r, -R # Delete directories and their contents recursively
-f # Force removal, ignore nonexistent files, never prompt
-i # Prompt before every removal
-v # Verbose output
# Implementations
rm document.txt # Erase a single file
rm -r folder/ # Delete a directory tree
rm -f /var/tmp/cache_file # Forcefully erase a file
rm -i *.log # Interactive removal of log files
rm -rf /path/to/obsolete_dir # Force recursive removal (Handle with extreme care!)
# Safety Tip: Always verify directory contents with `ls -R` before executing `rm -rf`.
Directory Navigation and Creation
Creating Folders via mkdir
mkdir new_folder # Create a single directory
mkdir -p lvl1/lvl2/lvl3 # Generate nested directories without errors
mkdir -m 755 public_folder # Create and set specific permissions immediately
mkdir folder_{a..e} # Generate multiple directories (folder_a to folder_e)
Listing Contents with ls
ls # Basic listing
ls -l # Long format showing metadata
ls -a # Include hidden entries
ls -h # Human-readable sizes (K, M, G)
ls -t # Sort by modification time
ls -R # Recursive listing of subdirectories
ls -lhS # Sort by size, human-readable
ls -ld */ # Display only directories
Switching Contexts with cd
cd /path/to/folder # Jump to an absolute path
cd ~ # Go to the user's home directory
cd - # Return to the previous working directory
cd .. # Move up one level
cd ../../ # Move up two levels
Inspecting File Content
Viewing File Data
cat document.txt # Output entire file to terminal
cat -n document.txt # Display with line numbers
cat part1.txt part2.txt > combined.txt # Concatenate files
less document.txt # Paginate output (Recommended)
# Inside less: q(quit), /pattern(search), n(next match), N(prev match), g(top), G(bottom)
more document.txt # Simple paginator
head document.txt # First 10 lines
head -n 20 document.txt # First 20 lines
tail document.txt # Last 10 lines
tail -n 20 document.txt # Last 20 lines
tail -f /var/log/messages # Continuously stream new lines
Access Control and Ownership
Modifying Permissions via chmod
# Symbolic Notation
chmod u+x execute_script.sh # Grant execute to user
chmod g-w settings.ini # Deny write to group
chmod o=r-- settings.ini # Set read-only for others
chmod a+rwx open_dir # Grant full access to everyone
chmod u=rwx,g=rx,o=r document.txt # Set distinct permissions for user, group, others
# Numeric Notation
chmod 755 execute_script.sh # Equals rwxr-xr-x
chmod 644 page.html # Equals rw-r--r--
chmod 700 secret_key # Equals rwx------
chmod -R 755 /path/to/folder # Apply recursively to a directory
Changing Ownership via chown
chown admin document.txt # Change the owner
chown admin:staff document.txt # Change both owner and group
chown -R admin:staff /path/to/folder # Apply recursively
chown :team shared_item # Change only the group
Altering Group Assignment via chgrp
chgrp team document.txt # Reassign the group
chgrp -R managers /path/to/folder # Apply recursively
Locating Files on the System
Real-Time Search using find
find /path -name "*.md" # Locate by exact name pattern
find /path -iname "item*" # Case-insensitive name search
find /path -type f -size +50M # Find files exceeding 50MB
find /path -mtime -3 # Files modified in the last 3 days
find /path -user sysadmin # Locate by owner
find /path -perm 644 # Locate by exact permission code
find . -name "*.cache" -delete # Find and remove matches
find /path -name "*.log" -exec rm {} \; # Execute a command on matches
Indexed Search using locate
updatedb # Refresh the file database (requires root)
locate document.txt # Rapid file lookup
locate -i "*.svg" # Case-insensitive search
locate -n 15 "*.py" # Limit output to 15 results
Archiving and Compression
Tape Archive (tar) Operations
# Creating Archives
tar -cvf bundle.tar folder/ # Archive without compression
tar -czvf bundle.tar.gz folder/ # Compress using Gzip
tar -cjvf bundle.tar.bz2 folder/ # Compress using Bzip2
tar -cJvf bundle.tar.xz folder/ # Compress using Xz
# Extracting Archives
tar -xvf bundle.tar # Unpack a tar file
tar -xzvf bundle.tar.gz # Unpack a Gzip archive
tar -xjvf bundle.tar.bz2 # Unpack a Bzip2 archive
tar -xJvf bundle.tar.xz # Unpack an Xz archive
tar -xvf bundle.tar.gz -C /target_dir # Extract to a specific directory
# Inspecting Archives
tar -tvf bundle.tar.gz # List contents without extracting
Zip Archive Operations
zip pack.zip item1 item2 # Compress individual files
zip -r pack.zip folder/ # Compress a directory recursively
unzip pack.zip # Decompress the archive
unzip -l pack.zip # View archive contents
unzip pack.zip -d /target_dir # Extract to a designated path
Gzip Compression Utilities
gzip document.txt # Compress and replace the original file
gzip -c document.txt > document.txt.gz # Compress while retaining the original
gunzip document.txt.gz # Decompress the file
gzip -d document.txt.gz # Alternative decompression method
zcat document.txt.gz # Read compressed data without extraction
File Metadata and Links
Timestamp Manipulation with touch
touch empty_item.txt # Generate a blank file
touch -c existing_item.txt # Refresh access and modification times
touch -t 202301011000 item.txt # Assign a custom timestamp
touch -r source.txt dest.txt # Mirror timestamps from another file
Link Creation with ln
ln document.txt hard_link.txt # Establish a hard link
ln -s /path/to/document.txt sym_link.txt # Create a symbolic link (soft link)
ln -sf /path/to/document sym_link # Forcefully create or update a symlink
Statistical Analysis Utilities
wc document.txt # Count lines, words, and bytes
wc -l document.txt # Count only lines
wc -w document.txt # Count only words
du document.txt # Calculate disk usage
du -h document.txt # Human-readable disk usage
du -sh folder/ # Summarize total directory size
df -h # Display filesystem disk space usage
Advanced Patterns and Pipelines
Batch Processing
# Bulk renaming
for f in *.txt; do mv "$f" "${f%.txt}.bak"; done
# Bulk replication
find . -name "*.jpeg" -exec cp {} /mnt/archives/ \;
# Safe removal via trash directory
mv document.txt ~/.local/share/Trash/files/
Stream Redirection
ls -l > listing.txt # Write standard output to a file
cat part1.txt >> part2.txt # Append data to an existing file
grep "exception" trace.log > exceptions.log # Filter and save matches
command 2> err.log # Redirect error stream to a file
Permission Hardening Practices
# Locate and correct overly permissive files
find /path -type f -perm 777 -exec chmod 644 {} \;
find /path -type d -perm 777 -exec chmod 755 {} \;
# Apply a sticky bit to restrict deletion to owners
chmod +t shared_area/
Risk Mitigation for Destructive Commands
Hazardous Operations (Verify Before Execution)
# Erase everything from root (Catastrophic!)
rm -rf /
# Empty a file irreversibly
> critical_data.txt
# Write raw data over a disk partition (Destructive)
dd if=image.iso of=/dev/sda
Protective Countermeasures
# Configure interactive aliases to prevent accidental overwrites
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
# Utilize trash-cli for recoverable deletion
trash-put document.txt # Move to a trash bin instead of deleting
trash-list # View trashed items
trash-restore # Recover deleted files
Command Quick Reference
| Category |
Command |
Frequent Flags |
Description |
| Copy |
cp |
-r, -i, -v |
Duplicate files or directories |
| Move/Rename |
mv |
-i, -v |
Relocate or rename items |
| Delete |
rm |
-r, -i, -f |
Erase files or directories |
| Directory Creation |
mkdir |
-p, -m |
Construct new directories |
| Content Viewing |
cat, less |
-n |
Read file contents |
| Permissions |
chmod |
-R |
Adjust access rights |
| Ownership |
chown |
-R |
Change file owner/group |
| Search |
find |
-name, -type |
Locate files dynamically |
| Compression |
tar, zip |
-z, -j |
Archive and compress data |