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/trashdirectory to store deleted files temporarily - How to create a custom
rmcommand script that moves deleted files to the trash directory - How to update the
PATHenvironment variable to use the customrmcommand - How to test the custom
rm -fcommand 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
rmcommand 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/trashdirectory 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
rmcommand:
sudo mkdir -p /usr/local/custom/bin
- Edit the
/etc/environmentfile:
sudo vim /etc/environment
- Add the
/usr/local/custom/bindirectory to the beginning of thePATHvariable:
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/environmentfile 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
rmcommand 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
rmcommand 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/projectdirectory:
touch /home/labex/project/trash_file.md
- Use the custom
rm -fcommand to delete the test file:
rm -f /home/labex/project/trash_file.md
- Verify that the file has been moved to the
/tmp/trashdirectory:
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
Top comments (0)