Implement Custom Trash-Enabled Command

LinuxLinuxBeginner
Practice Now

Introduction

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

🎯 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

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL shell(("`Shell`")) -.-> shell/ControlFlowGroup(["`Control Flow`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux(("`Linux`")) -.-> linux/VersionControlandTextEditorsGroup(["`Version Control and Text Editors`"]) shell(("`Shell`")) -.-> shell/BasicSyntaxandStructureGroup(["`Basic Syntax and Structure`"]) shell(("`Shell`")) -.-> shell/VariableHandlingGroup(["`Variable Handling`"]) shell(("`Shell`")) -.-> shell/FunctionsandScopeGroup(["`Functions and Scope`"]) shell(("`Shell`")) -.-> shell/SystemInteractionandConfigurationGroup(["`System Interaction and Configuration`"]) shell/ControlFlowGroup -.-> shell/if_else("`If-Else Statements`") linux/BasicSystemCommandsGroup -.-> linux/source("`Script Executing`") linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/FileandDirectoryManagementGroup -.-> linux/mkdir("`Directory Creating`") linux/BasicFileOperationsGroup -.-> linux/mv("`File Moving/Renaming`") linux/BasicFileOperationsGroup -.-> linux/rm("`File Removing`") linux/UserandGroupManagementGroup -.-> linux/sudo("`Privilege Granting`") linux/BasicFileOperationsGroup -.-> linux/chown("`Ownership Changing`") linux/BasicFileOperationsGroup -.-> linux/chmod("`Permission Modifying`") linux/VersionControlandTextEditorsGroup -.-> linux/vim("`Text Editing`") shell/BasicSyntaxandStructureGroup -.-> shell/shebang("`Shebang`") shell/BasicSyntaxandStructureGroup -.-> shell/comments("`Comments`") shell/BasicSyntaxandStructureGroup -.-> shell/quoting("`Quoting Mechanisms`") shell/VariableHandlingGroup -.-> shell/variables_decl("`Variable Declaration`") shell/VariableHandlingGroup -.-> shell/variables_usage("`Variable Usage`") shell/ControlFlowGroup -.-> shell/for_loops("`For Loops`") shell/ControlFlowGroup -.-> shell/cond_expr("`Conditional Expressions`") shell/FunctionsandScopeGroup -.-> shell/scope_vars("`Scope of Variables`") shell/SystemInteractionandConfigurationGroup -.-> shell/globbing_expansion("`Globbing and Pathname Expansion`") subgraph Lab Skills shell/if_else -.-> lab-301457{{"`Implement Custom Trash-Enabled Command`"}} linux/source -.-> lab-301457{{"`Implement Custom Trash-Enabled Command`"}} linux/echo -.-> lab-301457{{"`Implement Custom Trash-Enabled Command`"}} linux/mkdir -.-> lab-301457{{"`Implement Custom Trash-Enabled Command`"}} linux/mv -.-> lab-301457{{"`Implement Custom Trash-Enabled Command`"}} linux/rm -.-> lab-301457{{"`Implement Custom Trash-Enabled Command`"}} linux/sudo -.-> lab-301457{{"`Implement Custom Trash-Enabled Command`"}} linux/chown -.-> lab-301457{{"`Implement Custom Trash-Enabled Command`"}} linux/chmod -.-> lab-301457{{"`Implement Custom Trash-Enabled Command`"}} linux/vim -.-> lab-301457{{"`Implement Custom Trash-Enabled Command`"}} shell/shebang -.-> lab-301457{{"`Implement Custom Trash-Enabled Command`"}} shell/comments -.-> lab-301457{{"`Implement Custom Trash-Enabled Command`"}} shell/quoting -.-> lab-301457{{"`Implement Custom Trash-Enabled Command`"}} shell/variables_decl -.-> lab-301457{{"`Implement Custom Trash-Enabled Command`"}} shell/variables_usage -.-> lab-301457{{"`Implement Custom Trash-Enabled Command`"}} shell/for_loops -.-> lab-301457{{"`Implement Custom Trash-Enabled Command`"}} shell/cond_expr -.-> lab-301457{{"`Implement Custom Trash-Enabled Command`"}} shell/scope_vars -.-> lab-301457{{"`Implement Custom Trash-Enabled Command`"}} shell/globbing_expansion -.-> lab-301457{{"`Implement Custom Trash-Enabled Command`"}} end

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

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
  1. Edit the /etc/environment file:
sudo vim /etc/environment
  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"
  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

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

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
  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

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
  1. Use the custom rm -f command to delete the test file:
rm -f /home/labex/project/trash_file.md
  1. Verify that the file has been moved to the /tmp/trash directory:
ls -l /tmp/trash

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

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.

Other Linux Tutorials you may like