This tutorial provides a comprehensive guide to essential file operations in Linux, including copying, moving, renaming, and deleting files. You’ll learn how to use the `cp`, `mv`, and `rm` commands effectively, with practical examples and engaging exercises to reinforce your understanding.

Beginner30 minutes

Step 1: Introduction to File Operations in Linux

In Linux, file operations like copying, moving, renaming, and deleting are fundamental tasks. These operations are performed using command-line tools such as cp, mv, and rm. Understanding these commands is crucial for managing files and directories efficiently.

Practice Exercise

List all the files in your current directory using the ls command. Identify a file you want to work with for the upcoming steps.

Show Solution
# Use the `ls` command to list files
ls
# Example output: file1.txt  file2.txt  directory1

Step 2: Copying Files with `cp`

The cp command is used to copy files and directories. The basic syntax is cp source destination. To copy a directory and its contents, use the -R (recursive) flag.

# Copy a file
cp file1.txt file1_copy.txt

# Copy a directory recursively
cp -R directory1 directory1_copy

Practice Exercise

Create a new directory called backup. Copy all .txt files from your current directory into the backup directory.

Show Solution
# Create a backup directory
mkdir backup

# Copy all .txt files to the backup directory
cp *.txt backup/

# Verify the files were copied
ls backup

Step 3: Moving and Renaming Files with `mv`

The mv command is used to move or rename files and directories. The syntax is mv source destination. If the destination is a directory, the file is moved into it. If the destination is a new name, the file is renamed.

# Rename a file
mv file1.txt file1_renamed.txt

# Move a file to a directory
mv file1_renamed.txt directory1/

Practice Exercise

Rename a file in your current directory and then move it into a subdirectory. Verify the changes using the ls command.

Show Solution
# Rename a file
mv file1.txt file1_new.txt

# Move the renamed file to a subdirectory
mv file1_new.txt directory1/

# Verify the changes
ls directory1

Step 4: Removing Files with `rm`

The rm command is used to remove files and directories. Use rm file to delete a file and rm -r directory to delete a directory and its contents. Caution: This operation is irreversible!

# Remove a file
rm file1.txt

# Remove a directory and its contents
rm -r directory1

Practice Exercise

Create a temporary file and directory. Practice deleting them using the rm command. Be cautious and double-check before executing the command.

Show Solution
# Create a temporary file and directory
touch temp_file.txt
mkdir temp_dir

# Remove the temporary file
rm temp_file.txt

# Remove the temporary directory
rm -r temp_dir

# Verify the deletions
ls

Step 5: Understanding Risks and Precautions with `rm`

The rm command is powerful but dangerous. Accidentally deleting important files can have serious consequences. Always double-check the files or directories you're deleting. Use the -i (interactive) flag to confirm each deletion.

# Remove files interactively
rm -i file1.txt file2.txt

Practice Exercise

Create three dummy files. Use the rm -i command to delete them one by one, confirming each deletion.

Show Solution
# Create dummy files
touch dummy1.txt dummy2.txt dummy3.txt

# Remove files interactively
rm -i dummy1.txt dummy2.txt dummy3.txt

# Example interaction:
# rm: remove regular empty file 'dummy1.txt'? y
# rm: remove regular empty file 'dummy2.txt'? n
# rm: remove regular empty file 'dummy3.txt'? y

Step 6: Combining Commands for Advanced File Management

You can combine commands like cp, mv, and rm with other Linux utilities (e.g., find, grep) for advanced file management tasks. For example, you can copy all .log files from one directory to another and then delete the originals.

# Copy all .log files and delete originals
cp /path/to/source/*.log /path/to/destination/
rm /path/to/source/*.log

Practice Exercise

Find all .txt files in your current directory, copy them to a new directory called text_files, and then delete the originals. Use a combination of find, cp, and rm commands.

Show Solution
# Create the text_files directory
mkdir text_files

# Find and copy .txt files
find . -name '*.txt' -exec cp {} text_files/ \;

# Delete the original .txt files
find . -name '*.txt' -exec rm {} \;

# Verify the operation
ls text_files

Step 7: Real-World Scenario: Organizing a Project Directory

Imagine you're organizing a project directory. You need to move all .jpg files to an images folder, rename .txt files to .md, and delete temporary .tmp files.

# Move .jpg files to images folder
mkdir images
mv *.jpg images/

# Rename .txt files to .md
for file in *.txt; do mv "$file" "${file%.txt}.md"; done

# Delete .tmp files
rm *.tmp

Practice Exercise

Create a mock project directory with .jpg, .txt, and .tmp files. Organize the directory as described above.

Show Solution
# Create mock files
touch image1.jpg image2.jpg doc1.txt doc2.txt temp1.tmp temp2.tmp

# Move .jpg files to images folder
mkdir images
mv *.jpg images/

# Rename .txt files to .md
for file in *.txt; do mv "$file" "${file%.txt}.md"; done

# Delete .tmp files
rm *.tmp

# Verify the organization
ls
ls images

Step 8: Challenge: Automating File Operations with a Script

Write a bash script to automate the following tasks:

  • Create a backup of all .conf files in a directory.
  • Move all .log files to a logs directory.
  • Delete all .tmp files.
# Example script
#!/bin/bash
mkdir backup logs
cp *.conf backup/
mv *.log logs/
rm *.tmp

Practice Exercise

Write and execute the script described above. Test it with a directory containing .conf, .log, and .tmp files.

Show Solution
# Create the script
cat > organize_files.sh <<EOL
#!/bin/bash
mkdir -p backup logs
cp *.conf backup/
mv *.log logs/
rm *.tmp
EOL

# Make the script executable
chmod +x organize_files.sh

# Run the script
./organize_files.sh

# Verify the results
ls backup
ls logs

Sign in to take Cornell notes on this lesson — they save automatically and stay with your account.

Sign in

Click to access the login or register cheese