- Published on
Basic Terminal Commands: cp, mv, rm, and rmdir
- Authors
- Name
- Hieu Cao
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.
cp
Command: Copy Files and Directories
The The cp
command is used to copy files or directories from one location to another.
Syntax
cp [options] source destination
Common Usage
Copy a File
cp file.txt /path/to/destination/
- Copies
file.txt
to the specified destination.
- Copies
Copy a Directory
cp -r folder /path/to/destination/
- Copies the entire
folder
and its contents recursively.
- Copies the entire
Interactive Mode
cp -i file.txt /path/to/destination/
- Prompts before overwriting existing files.
Preserve File Attributes
cp -p file.txt /path/to/destination/
- Retains the original file's timestamps and permissions.
mv
Command: Move or Rename Files
The The mv
command is used to move files or directories to a new location or rename them.
Syntax
mv [options] source destination
Common Usage
Move a File
mv file.txt /path/to/destination/
- Moves
file.txt
to the specified destination.
- Moves
Rename a File
mv oldname.txt newname.txt
- Renames
oldname.txt
tonewname.txt
.
- Renames
Interactive Mode
mv -i file.txt /path/to/destination/
- Prompts before overwriting existing files.
Verbose Output
mv -v file.txt /path/to/destination/
- Displays details of the operation.
rm
Command: Remove Files and Directories
The The rm
command deletes files or directories. Use it with caution as it permanently removes data.
Syntax
rm [options] file_name
Common Usage
Remove a File
rm file.txt
- Deletes
file.txt
.
- Deletes
Remove a Directory and Its Contents
rm -r folder
- Deletes the
folder
and all its contents recursively.
- Deletes the
Force Deletion
rm -f file.txt
- Deletes
file.txt
without prompting.
- Deletes
Interactive Mode
rm -i file.txt
- Prompts before deleting.
rmdir
Command: Remove Empty Directories
The The rmdir
command is used to delete empty directories.
Syntax
rmdir [directory_name]
Common Usage
Remove an Empty Directory
rmdir empty_folder
- Deletes
empty_folder
if it is empty.
- Deletes
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:
Create a File and Copy It
echo "Sample text" > file.txt cp file.txt backup.txt
Rename the Backup File
mv backup.txt archive.txt
Delete the Original File
rm file.txt
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!