Introduction
Managing files and directories efficiently is a fundamental skill for anyone working with Linux. Commands like mv
, rm
, and cp
enables users to move, rename, delete, and copy files and directories with precision and speed. In this tutorial, we’ll explore how these commands work, their essential options, and practical use cases. Whether you’re organizing files, cleaning up directories, or creating backups, mastering these commands will streamline your workflow and improve productivity in a Linux environment.
The mv
Command: Move and Rename Files
The mv
command is used to move or rename files and directories. It’s straightforward yet incredibly powerful.
Example 1: Renaming a File
mv old_filename.txt new_filename.txt
This command renames old_filename.txt
to new_filename.txt
.
Example 2: Moving a File to Another Directory
mv document.txt /path/to/destination/
This moves document.txt
to the Documents
directory.
Tip: Use the -i
option to get a prompt before overwriting files:
The rm
Command: Remove Files and Directories
The rm
command is used for deleting files and directories. Use it carefully to avoid accidental deletions.
Example 1: Removing a File
rm unwanted_file.txt
Example 2: Removing a Directory and Its Contents
rm -r old_directory/
Tip: For safety, use the -i
option to confirm deletions:
rm -i file.txt
The cp
Command: Copy Files and Directories
The cp
command is essential for creating backups or duplicating files and directories.
Example 1: Copying a Single File
cp source.txt destination.txt
This creates a copy of source.txt
named destination.txt
.
Example 2: Copying an Entire Directory
cp -r /source_directory/ /destination_directory/
The -r
option copies the entire directory, including subdirectories.
Tip: Use the -v
option to see the copy progress:
cp -v file.txt /backup/
Advanced Use Cases
Combining Commands
Automate repetitive tasks by combining these commands. For example, move and rename files with specific extensions:
find /path/to/files/ -name "*.log" -exec mv {} /path/to/logs/ \;
Using Wildcards
Work with multiple files at once:
cp *.txt /backup/
rm *.tmp