cp, mv, rm
cp, mv and rm are like photocopying a page, moving a page to another folder, and putting a page in the shredder. Each one handles files in a different way.
In this page:
Syntax
cp source destination
mv source destination
rm [-r] file_or_directory
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
#!/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 try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
Common Mistakes
- Running
rm -rfwith an empty variable, as inrm -rf "$dir/", which can delete the wrong path. - Overwriting files with
cpormvwith no-ior-n. - Forgetting
-rwhen copying a directory.
🔒
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: