Mastering file operations is essential for Linux beginners. Here’s a clear guide to key commands with practical examples:
🖼️ Understanding the Linux File Structure
Linux uses a tree-like directory structure starting from root (/). Always know your current location with pwd
.
🗑️ 1. Deleting Files and Folders
Delete a file:
rm filename.txt
Delete empty folder:
rmdir folder_name
Delete folder with contents:
rm -r folder_name
Use -i
for confirmation prompts: rm -i file.txt
⚠️ Danger Zone:
rm -rf /
will delete EVERYTHING! Triple-check paths before using -rf
.
📑 2. Copying Files and Folders
Copy a file:
cp source.txt destination/
Copy folder recursively:
cp -r source_folder/ destination/
Preserve file attributes:
cp -p important_doc.conf backup/
Use TAB key for path auto-completion to avoid typos
🚚 3. Moving/Renaming Files and Folders
Move a file:
mv file.txt /new/location/
Rename a file:
mv oldname.txt newname.txt
Move folder:
mv project/ ~/backups/
Same mv
command handles both moving and renaming
💡 Pro Tips for Beginners
-
Dry-run first: Add
-v
(verbose) to see what will happen:
cp -rv source/ destination/
-
Undo protection:
alias rm='rm -i' # Adds confirmation prompt alias cp='cp -i' alias mv='mv -i'
Add these to
~/.bashrc
for safety -
Restore deleted files?
Installtrash-cli
for recycle bin functionality:
sudo apt install trash-cli && trash file.txt
✅ Practice Exercise
mkdir test
cd test
touch file1.txt
cp file1.txt file2.txt
mv file2.txt renamed.txt
rm renamed.txt
cd ..
rm -r test
📌 Key Takeaway:
Always double-check paths before executing destructive commands. Start with safe options (-i
) until you’re confident!
Practice these commands in a test directory first to build confidence