Implement a Custom Trash-Enabled Linux Command
Labby
Posted on October 10, 2024
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 customrm
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.
- Open a terminal.
- 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.
- Create a new directory to hold the custom
rm
command:
sudo mkdir -p /usr/local/custom/bin
- Edit the
/etc/environment
file:
sudo vim /etc/environment
- Add the
/usr/local/custom/bin
directory to the beginning of thePATH
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"
- Save the file and exit the editor.
- 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.
- 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
- 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.
- Create a test file in the
/home/labex/project
directory:
touch /home/labex/project/trash_file.md
- Use the custom
rm -f
command to delete the test file:
rm -f /home/labex/project/trash_file.md
- 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.
🚀 Practice Now: Implement Custom Trash-Enabled Command
Want to Learn More?
- 🌳 Learn the latest Shell Skill Trees
- 📖 Read More Shell Tutorials
- 💬 Join our Discord or tweet us @WeAreLabEx
Posted on October 10, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.