Incorporating rm Commands into Bash Scripts
13:06, 01.12.2025
Proper management of the files is crucial when working with the bash scripts. Among a huge variability of tasks probably the most important one is connected with the removal of the directories and files. For this purpose, usually, the rm command is used. With rm usage, users can go even further and automate the process of removal. It simplifies the process so much more, but it is also important to be cautious when working with critical information.
In this guide, you will get all the basic information about the rm command, its usage, several common examples, and more.
However, before we explain some of the most used cases, let’s clear it up, that we will discuss the usage on Linux-based systems, with version 4.0 and later of the bash shell, and you should have some basic understanding of the bash scripts.
Adding rm to Bash Scripts
Depending on the specific use case, the incorporation of the rm command in the bash script might slightly differ. Here we will share some of the standard and more complex variants so you will have a better understanding of its usage and some safety measures.
Sample 1. Let’s begin with the removal of the single file in a bash script, the process looks the following way:
#!/bin/bash
rm /path/to/drf.txt
echo "deleted"
Here, you will need to specify the pass to the needed file instead of drf.txt.
Sample 2. To remove several files with the same command, the process is the following:
#!/bin/bash
rm /pathtonecessaryfiles/*.txt
*.txt means that all the txt files in the mentioned directory will be deleted. This command should be used with caution because you might remove the necessary data.
Sample 3. To delete the entire directory with all the files, the following command should be used:
#!/bin/bash
rm -r /pathtodirectory
This option is usually necessary for the removal of the huge folders.
Sample 4. For force removal of the specific file without the necessity for confirmation, the following variant can be used:
#!/bin/bash
rm -f /pathtofile.txt
This is a great choice when removing protected files.
Sample 5. Usage of this command with if statements can make the process smarter by mentioning additional details like the following:
#!/bin/bash
if [ -f /pathtofile.txt ]; then
rm /pathtofile.txt
echo "Deleted"
else
echo "Hasn’t found"
fi
This script can be used for the detection of the specific file before the removal so there won’t be any errors in case the file hasn’t been found.
Sample 6. The combination of find and rm can help with the selective delete where you can specify certain criteria as follows:
#!/bin/bash
find /pathtodirectory -name "*.log" -type f -mtime +15 -exec rm {} \;
echo "Deleted"
With this script, you are searching for the log files that are older than 15 days and removing them. This is a wonderful variant for the automation of the deletion process of unnecessary files.
Sample 7. There might be some situations when you need to remove the file and guarantee that it won’t be recovered. This can be done as follows:
#!/bin/bash
shred -u /pathtofile.txt
rm /pathtofile.txt
This is an ideal variant when you want to remove data in the most secure way. As you see, together with the rm command, there t is also used shred for the prevention of recovery.
Sample 8. Here is one more example of the rm command usage for the scenarios when you want to check the existence of the directory/file prior to the removal. This can be done as follows:
#!/bin/bash
if [ -z "$1" ]; then
echo "Usage: $0 "
exit 1
fi
if [ -e "$1" ]; then
rm -r "$1"
echo "Deleted"
else
echo "Haven’t found"
fi
Conclusion
All the examples of the rm command usage should be applied safely for the prevention of some unpleasant consequences. Hope that now you have a better understanding of the incorporation of rm into the bash scripts and you will significantly automate some tasks.