Batch Script – Remove
Last Updated :
03 Jun, 2022
Bash is a command language interpreter. It is used in most of the UNIX/LINUX-based distros. Bash support scripting means we can automate the Bash commands by writing some scripts with different logic, looping, and decision making. This benefits us to automate some time-consuming and tedious jobs.
Here in this article, we are going to discuss the rm command within Bash Scripting. rm command helps to remove files, directories, and symbolic links.
The basic syntax for the rm command is –
Syntax of rm command:
rm [OPTION]... FILE...
Let’s understand using examples:
Example 1: rm command for deleting a file
Example Script:
#!/bin/bash
rm myfile.txt
echo "File is deleted"
Script Output:
File is deleted
Below is the terminal shell pictorial depiction after executing the following script:
Here in this example, we have deleted one text file with the name myfile.txt using the rm command.
There are a few options we can use with the rm command they are as follows:
Options |
Description |
-f, –force |
It will ignore if the file does not exist in the directory. |
-i |
It will show a prompt message before every removal operation |
-I |
It will show a prompt message only one time before the removal operation of multiple files |
–one-file-system |
It will skip any directory that is on a file system different from that of the corresponding command-line argument |
-r, -R, –recursive |
It will remove directories and their contents recursively |
-d, –dir |
It will remove empty directories |
-v, –verbose |
It will explain what is being done |
Please refer to the rm command in Linux with examples article for examples of the above options.
Example 2: rm command from If Statements in a Script
Example Script:
#!/bin/bash
filename="myfile_1.txt"
if [ "$filename"=="myfile_1.txt" ];then
rm myfile_1.txt
echo "myfile_1.txt is deleted"
else
rm myfile_2.txt
echo "myfile_2.txt is deleted"
fi
Script Output:
myfile_1.txt is deleted
Below is the terminal shell pictorial depiction after executing the following script:
Here in this example, we have initially two text files, after that using the script we deleted one file (myfile_1.txt) as if the condition was satisfied and only that block got executed, so the rm command in else blocks is written for myfile_2.txt doesn’t get executed.
Example 3: rm command from Switch Statements in a Script
Example Script:
#!/bin/bash
preference="B"
case "$preference" in
# case 1
"A") rm file1.txt
echo "file1.txt is deleted";;
# case 2
"B") rm -rf mydir
echo "mydir is deleted";;
esac
Script Output:
mydir is deleted
Here in this example, we have deleted a non-empty directory using the rm command with the -rf option. We have used a switch statement for decision-making before performing the operation. If the preference was selected as A, then the rm command will delete a file with the file1.txt name.
Below is the terminal shell pictorial depiction after executing the following script:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...