← Back to Bash Course | Chapter 9: File Testing & Filesystem | Lesson 9 of 13

cp, mv, rm

In this page:

  1. cp, mv, rm

cp, mv, rm

cp copies files, mv moves or renames them, and rm deletes them. cp -r and rm -r operate recursively on directories. These are the fundamental filesystem manipulation commands used constantly in shell scripts.

Warning: rm -rf deletes recursively without confirmation — a typo in the path can destroy far more than intended.

Example: cp, mv, rm

bash
#!/bin/bash
echo "original" > original.txt

cp original.txt copy.txt
mv copy.txt renamed.txt

ls original.txt renamed.txt

rm renamed.txt
echo "renamed.txt removed"

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.