Implement a Custom Trash-Enabled Linux Command

labby

Labby

Posted on October 10, 2024

Implement a Custom Trash-Enabled Linux Command

Introduction

MindMap

In this project, you will learn how to create a custom rm command that moves deleted files to a trash directory instead of permanently deleting them. This is a useful feature to have on your Linux server to prevent accidental deletion of crucial files.

👀 Preview

labex:project/ $ ll /tmp/trash
total 2.0K
-rw-r--r-- 1 labex labex 8 Oct 26 17:47 trash_file.md
Enter fullscreen mode Exit fullscreen mode

🎯 Tasks

In this project, you will learn:

  • How to set up the /tmp/trash directory to store deleted files temporarily
  • How to create a custom rm command script that moves deleted files to the trash directory
  • How to update the PATH environment variable to use the custom rm command
  • How to test the custom rm -f command to ensure it is working as expected

🏆 Achievements

After completing this project, you will be able to:

  • Protect your Linux server from accidental file deletion by implementing a custom rm command with a trash directory
  • Understand how to modify system commands to change their default behavior
  • Gain experience in shell scripting and environment variable management

Set Up the Trash Directory

In this step, you will learn how to set up the /tmp/trash directory to store deleted files temporarily.

  1. Open a terminal.
  2. Create the /tmp/trash directory and set the appropriate permissions:
sudo mkdir /tmp/trash
sudo chown root:root /tmp/trash
sudo chmod 1777 /tmp/trash
Enter fullscreen mode Exit fullscreen mode

The 1777 permission sets the directory to have the "sticky bit" enabled, which allows all users to write to the directory, but only the owner can delete files within it.

Update the PATH Environment Variable

In this step, you will update the PATH environment variable to ensure that the custom rm command is used instead of the default system rm command.

  1. Create a new directory to hold the custom rm command:
sudo mkdir -p /usr/local/custom/bin
Enter fullscreen mode Exit fullscreen mode
  1. Edit the /etc/environment file:
sudo vim /etc/environment
Enter fullscreen mode Exit fullscreen mode
  1. Add the /usr/local/custom/bin directory to the beginning of the PATH variable:
PATH="/usr/local/custom/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin"
Enter fullscreen mode Exit fullscreen mode
  1. Save the file and exit the editor.
  2. Source the /etc/environment file to update the current shell's environment:
source /etc/environment
sudo chmod a+x /usr/local/custom/bin
Enter fullscreen mode Exit fullscreen mode

Now, the custom rm command will be used instead of the default system rm command.

Create the Custom rm Command

In this step, you will create a custom rm command that will move deleted files to the /tmp/trash directory instead of permanently deleting them.

  1. Create the custom rm command script:
sudo vim /usr/local/custom/bin/rm
Enter fullscreen mode Exit fullscreen mode

Add the following content to the file:

#!/bin/zsh

# This script is used to move files or directories to the trash directory instead of permanently deleting them.
# If the -f option is used, the targets will be moved to the trash directory.
# If the targets do not exist, an error message will be displayed.

TRASH_DIR="/tmp/trash"

if [[ "$1" == "-f" ]]; then
  shift
  for arg in "$@"; do
    # Check if the target exists
    if [[ -e "$arg" ]]; then
      # Move the target to the trash directory
      mv -f "$arg" "$TRASH_DIR"
    else
      echo "Error: $arg does not exist."
    fi
  done
else
  # Execute the original rm command
  /bin/rm "$@"
fi
Enter fullscreen mode Exit fullscreen mode
  1. Set the appropriate permissions for the custom rm command script:
sudo chown root:root /usr/local/custom/bin/rm
sudo chmod 755 /usr/local/custom/bin/rm
Enter fullscreen mode Exit fullscreen mode

Test the Custom rm Command

In this step, you will test the custom rm -f command to ensure that it is working as expected.

  1. Create a test file in the /home/labex/project directory:
touch /home/labex/project/trash_file.md
Enter fullscreen mode Exit fullscreen mode
  1. Use the custom rm -f command to delete the test file:
rm -f /home/labex/project/trash_file.md
Enter fullscreen mode Exit fullscreen mode
  1. Verify that the file has been moved to the /tmp/trash directory:
ls -l /tmp/trash
Enter fullscreen mode Exit fullscreen mode

You should see the trash_file.md file listed in the output.

total 2.0K
-rw-r--r-- 1 labex labex 8 Oct 26 17:47 trash_file.md
Enter fullscreen mode Exit fullscreen mode

Congratulations! You have successfully implemented the custom rm command that moves deleted files to the /tmp/trash directory instead of permanently deleting them.

Summary

Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.


🚀 Practice Now: Implement Custom Trash-Enabled Command


Want to Learn More?

💖 💪 🙅 🚩
labby
Labby

Posted on October 10, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related