Mastering Copy, Move & Delete: Files and Directories Made Easy

Command-Line Basics: How to Copy, Move and Delete Files and Folders

Overview

This guide covers the essential commands for copying, moving, and deleting files and directories on Unix-like systems (Linux, macOS) and Windows (PowerShell / Command Prompt). Each section gives basic usage, common options, and examples.


Unix-like (bash, zsh)

  • Copy files

    • Command: cp
    • Common options: -r (recursive for directories), -a (archive: preserve attributes), -i (interactive), -v (verbose)
    • Examples:
      • Copy a file: cp source.txt dest.txt
      • Copy into directory: cp file.txt /path/to/dir/
      • Copy directory recursively: cp -r src_dir/ dest_dir/
      • Preserve attributes: cp -a src_dir/ dest_dir/
  • Move/rename files

    • Command: mv
    • Common options: -i (interactive), -v (verbose)
    • Examples:
      • Rename: mv oldname.txt newname.txt
      • Move to directory: mv file.txt /path/to/dir/
      • Move directory: mv src_dir/ /path/to/dest/
  • Delete files and directories

    • Commands: rm (files), rm -r (directories), rmdir (empty directories)
    • Common options: -f (force), -i (interactive), -v (verbose)
    • Examples:
      • Delete a file: rm file.txt
      • Delete directory and contents: rm -r dir_name/
      • Permanently force delete without prompt: rm -rf unwanted_dir/
      • Remove empty directory: rmdir empty_dir/
  • Safety tips

    • Use -i to confirm before overwriting/deleting.
    • Test with echo or ls before running destructive commands.
    • Avoid running rm -rf / or with wildcards without checking current path.

Windows (Command Prompt)

  • Copy files

    • Command: copy (files), xcopy (files & directories), robocopy (robust)
    • Examples:
      • Copy a file: copy source.txt dest.txt
      • Copy directory with xcopy: xcopy /E /I src_dir dest_dir
      • Robust copy: robocopy C:\src C:\dest /MIR
  • Move/rename files

    • Command: move
    • Examples:
      • Move file: move file.txt C:\path\to\dir</li>
      • Rename: move oldname.txt newname.txt
  • Delete files and directories

    • Commands: del (files), rmdir /S (directories)
    • Examples:
      • Delete file: del file.txt
      • Delete directory and contents: rmdir /S /Q C:\path\to\dir
  • Notes

    • robocopy is preferred for large or resumed transfers.
    • Use /Q for quiet, /S to include subdirectories (excluding empty).

PowerShell (recommended on modern Windows)

  • Copy

    • Command: Copy-Item
    • Examples:
      • Copy file: Copy-Item -Path .\file.txt -Destination C:\dest</li>
      • Copy directory recursively: Copy-Item -Path .\src_dir -Destination C:\dest -Recurse -Force
  • Move

    • Command: Move-Item
    • Example: Move-Item -Path .\file.txt -Destination C:\newfolder</li>
  • Remove

    • Command: Remove-Item
    • Examples:
      • Delete file: Remove-Item -Path .\file.txt
      • Delete directory recursively: Remove-Item -Path .\dir -Recurse -Force
  • Advantages

    • PowerShell supports objects, pipelines, and safer cmdlets with -WhatIf to preview actions: e.g., Remove-Item -Path .\dir -Recurse -WhatIf

Common patterns & best practices

  • Backup before destructive ops.
  • Use verbose and dry-run/WhatIf where available.
  • Prefer full paths to avoid surprises from current working directory.
  • For bulk operations, test on a small sample first.
  • Use version control or snapshots for important files.

Quick reference (examples)

  • Unix: cp -a src/ dest/ ; mv file.txt /tmp/ ; rm -ri old_dir/
  • PowerShell: Copy-Item .\src -Destination C:\dest -Recurse ; Move-Item .\a.txt C:\x\ ; Remove-Item .\tmp -Recurse -WhatIf
  • Windows CMD: xcopy C:\src C:\dest /E /I ; move a.txt C:\x\ ; rmdir /S /Q C:\old

If you want commands tailored to a specific OS/version or examples for batch/shell scripts, tell me which OS and I’ll provide them.

Comments

Leave a Reply