Published on

Basic Terminal Commands: cp, mv, rm, and rmdir

Authors
  • avatar
    Name
    Hieu Cao
    Twitter

Introduction

The terminal offers powerful commands for managing files and directories. Four essential commands—cp, mv, rm, and rmdir—allow users to copy, move, and delete files and folders efficiently. This guide covers the basics of these commands, their common options, and practical use cases.

The cp Command: Copy Files and Directories

The cp command is used to copy files or directories from one location to another.

Syntax

cp [options] source destination

Common Usage

  1. Copy a File

    cp file.txt /path/to/destination/
    
    • Copies file.txt to the specified destination.
  2. Copy a Directory

    cp -r folder /path/to/destination/
    
    • Copies the entire folder and its contents recursively.
  3. Interactive Mode

    cp -i file.txt /path/to/destination/
    
    • Prompts before overwriting existing files.
  4. Preserve File Attributes

    cp -p file.txt /path/to/destination/
    
    • Retains the original file's timestamps and permissions.

The mv Command: Move or Rename Files

The mv command is used to move files or directories to a new location or rename them.

Syntax

mv [options] source destination

Common Usage

  1. Move a File

    mv file.txt /path/to/destination/
    
    • Moves file.txt to the specified destination.
  2. Rename a File

    mv oldname.txt newname.txt
    
    • Renames oldname.txt to newname.txt.
  3. Interactive Mode

    mv -i file.txt /path/to/destination/
    
    • Prompts before overwriting existing files.
  4. Verbose Output

    mv -v file.txt /path/to/destination/
    
    • Displays details of the operation.

The rm Command: Remove Files and Directories

The rm command deletes files or directories. Use it with caution as it permanently removes data.

Syntax

rm [options] file_name

Common Usage

  1. Remove a File

    rm file.txt
    
    • Deletes file.txt.
  2. Remove a Directory and Its Contents

    rm -r folder
    
    • Deletes the folder and all its contents recursively.
  3. Force Deletion

    rm -f file.txt
    
    • Deletes file.txt without prompting.
  4. Interactive Mode

    rm -i file.txt
    
    • Prompts before deleting.

The rmdir Command: Remove Empty Directories

The rmdir command is used to delete empty directories.

Syntax

rmdir [directory_name]

Common Usage

  1. Remove an Empty Directory

    rmdir empty_folder
    
    • Deletes empty_folder if it is empty.
  2. Verbose Output

    rmdir -v empty_folder
    
    • Displays details of the operation.

Note

To remove non-empty directories, use the rm -r command instead.


Practical Example

Let’s demonstrate these commands in a workflow:

  1. Create a File and Copy It

    echo "Sample text" > file.txt
    cp file.txt backup.txt
    
  2. Rename the Backup File

    mv backup.txt archive.txt
    
  3. Delete the Original File

    rm file.txt
    
  4. Remove an Empty Directory

    mkdir empty_folder
    rmdir empty_folder
    

Conclusion

The cp, mv, rm, and rmdir commands are indispensable for managing files and directories in the terminal. By mastering these commands, you can efficiently organize and clean up your files and folders. Practice using these commands to streamline your workflow and enhance productivity!