Remove the Files
Two commands are mainly used in Linux to remove the file. One is the “unlink” command and the other is the “rm” command. The “unlink” command can remove a single file only but the “rm” command can be used in different ways to remove one or more files. The ways of deleting the files are by using these commands and the Bash script that uses these commands are shown in this part of the tutorial.
Remove the File Using the “Unlink” Command
Syntax:
The syntax of the “unlink” command is given in the following illustration. A single filename is used with this command to remove the file.
Example 1: Remove a Single File Using the Unlink Command
Run the following commands to check the list of files and folders of the current location, remove a particular file using the “unlink” command, and check whether the file is deleted or not:
file.
$ unlink test.txt
$ ls
It is shown in the following output that the test.txt file is deleted after executing the “unlink” command:
Remove the File Using the “Rm” Command
This command can remove one or more files at a time by using different options. The syntax of the “rm” command is given in the following:
Syntax:
The option of this command is optional.
file.
The mostly used options of the “rm” commands to remove the files are described in the following:
Option | Purpose |
-i | It is used to provide a prompt message before deleting a file. |
-I | It is used to provide a prompt message if more than three files are required to delete. |
-f , –force | It is used to ignore the prompt message before deleting any write-protected file. |
Example 2: Remove a Single File Using the “Rm” Command
Run the following commands to check the list of files and folders of the current location, remove a particular file using the “rm” command without any option, and check whether the file is deleted or not:
file.
$ rm temp.txt
$ ls
It is shown in the following output that the temp.txt file is deleted after executing the “rm” command.
Example 3: Remove Multiple Files Using the “Rm” Command
Run the following commands to check the list of files and folders of the current location, remove two files using the “rm” command without any option, and check whether the files are deleted or not:
file.
$ rm t1.txt t2.txt
$ ls
It is shown in the following output that t1.txt and t2.txt files are deleted after executing the “rm” command:
Example 4: Remove a File Using the “Rm” Command with a Prompt Message
Run the following commands to check the list of files and folders of the current location, remove a file using the “rm” command with the -i option to delete the file after confirmation, and check whether the file is deleted or not:
file.
$ rm -i t3.txt
$ ls
It is shown in the following output that the t3.txt file has not been deleted after executing the “rm” command with the -i option because “n” was given by the user:
Example 5: Remove More Than Three Files Using the “Rm” Command with a Prompt Message
Run the following commands to check the list of files and folders of the current location and remove four files using the “rm” command with the -I option:
file.
$ rm -I t1.txt t2.txt t3.txt t4.txt
It is shown in the following output that no file has been deleted after executing the “rm” command with the -I option because “n” was given by the user:
Example 6: Remove Files Using the “Rm” Command with Wildcard Characters
When the filename is unknown which is required to delete or the list of the files of the specific extension is required to delete, the wildcard character can be used to delete the particular file or files. Run the following commands to delete all the text files that start with “h”:
file.
$ rm h*.txt
$ ls
It is shown in the following output that there are six text files in the current location and there is only one text file that starts with “h”. So, the hello.txt filename is removed after executing the “rm” command that defined the filename as “h*.txt”. Here, the “*” symbol indicates any number of characters.
It is shown in the following output that there are six text files in the current location and there is only one text file that contains 5 characters and the filename is items.txt. So, the filename items.txt is removed after executing the “rm” command that defined the filename as ?????.txt. Here, the “?” symbol indicates any specific character.
Example 7: Remove the File Using the Bash Script
Create a Bash file with the following script that takes the filename from the user, checks whether the filename exists or not in the current location, and deletes the file if it exists.
file.
#Take the filename from the user
read -p "Enter the file name to remove:" filename
# Check whether the file exists or not
if [ -f $filename ]; then
rm $filename
echo "$filename is deleted."
fi
It is shown in the following output that the test.txt file is deleted after executing the “rm” command:
Remove Directories
A directory can be empty and non-empty. Both “rm” and “rmdir” commands are used to remove the directories in Linux. Both commands have multiple options to remove the directories which are explained in this part of this tutorial with examples.
Remove a Directory Using the “Rm” Command
The following options of the “rm” command are mostly used to remove the directories in Linux:
Option | Purpose |
-d | It is used to remove an empty directory. |
-r | It is used to remove a non-empty directory. |
-rf | It is used to remove the write-protected non-empty directory without a prompt message. |
Example 1: Remove an Empty Directory Using the “Rm” Command
Run the following commands to check the list of files and folders of the test directory and remove the directory using the “rm” command if the directory is empty:
file.
$ rm -d test
$ ls
It is shown in the following output that the test directory is empty and it has been deleted after executing the “rm” command with the -d option:
Example 2: Remove the Non-Empty Directory Using the “Rm” Command
Run the following commands to check the list of files and folders of the non-empty test directory using the “rm” command with the -r option:
file.
$ rm -d temp
$ rm -r temp
$ ls
It is shown in the following output that the temp directory is a non-empty directory and it has been deleted after executing the “rm” command with the -r option:
Remove Directory Using the “Rmdir” Command
The “rmdir” is another useful option to remove one or more directories using the various options. The syntax of this command is as follows:
file.
The following options of the “rmdir” command are mostly used to remove the directories in Linux:
Option | Purpose |
-p, –parents | It is used to remove a directory with subdirectories. |
-v, –verbose | It is used to print a message after deletion. |
–ignore-fail-on-non-empty | It is used to ignore a failure for the non-empty directory. |
Example 3: Remove a Directory Using the “Rmdir” Command
Run the following commands to check the list of files and folders of the current directory and delete an empty directory named books using the “rmdir” command without any option:
file.
$ rmdir books
$ ls
It is shown in the following output that the empty directory books is deleted after executing the “rmdir” command:
Example 4: Remove Multiple Directories Using the “Rmdir” Command
Run the following commands to check the list of files and folders of the current directory and delete two empty directories named temp and mydir using the “rmdir” command without any option:
file.
$ rmdir temp mydir
$ ls
It is shown in the following output that two directories, temp and mydir, are deleted after executing the “rmdir” command:
Example 5: Remove the Empty Directory with Sub-Directories Using the “Rmdir” Command
Run the following commands to check the list of files and folders of the test directory and delete this directory with a sub-directory using the “rmdir” command with the -p option:
file.
$ ls test/messages
$ rmdir -p test/messages
$ ls
It is shown in the following output that the test directory with messages directory are deleted after executing the “rmdir” command:
Example 6: Ignore the Failure Message for a Non-Empty Directory Using the “Rmdir” Command
Run the following commands to check the list of files and folders of the temp directory and delete this directory using the “rmdir” command with the –ignore-fail-on-non-empty option:
file.
$ rmdir --ignore-fail-on-non-empty temp
$ ls temp
It is shown in the following output that two directories, a non-empty directory named temp has been tried to delete using the “rmdir” command. But the directory has not been deleted because the “rmdir” command is not able to remove a non-empty directory. The non-empty directory can be deleted using the “rm” command.
Example 7: Remove a Directory Using Bash Script
Create a Bash file with the following script that takes the directory name from the user, checks whether the directory exists or not in the current location, and deletes the directory if it exists and is empty.
file.
#Take directory name from the user
read -p "Enter the directory name to remove:" dirname
# Check whether the directory exists or not
if [ -d $dirname ]; then
rm -r $dirname
echo "$dirname directory is deleted."
fi
It is shown in the following output that the temp directory is empty and the food directory is non-empty. The temp was taken as the directory name in the first execution of the script and the food directory was taken in the second execution of the script. Both directories are deleted using the “rm” command with the –r option.
Remove Links
The hard link is used to refer to a file with the same inode number. When the file is deleted, the link is also removed. The symbolic link or soft link creates a different inode number when it refers to a file. The “ln” command is used to create a hard or symbolic link. Suppose that the company.txt file exists in the current location.
The following command creates the hard link named c1 for the company.txt file:
file.
The following command creates the soft link named c2 for the company.txt file:
file.
Example 1: Remove the Hard Link Using the “Unlink” Command
Run the following commands to check the list of the files, folders, and links of the current location, remove the hard link using the “unlink” command, and check whether the link has been removed or not:
file.
$ unlink c1
$ls
It is shown in the following output that there is a text file, a hard link, and a symbolic link in the current location and the c1 hard link has been removed.
Example 2: Remove a Symbolic Link Using the “Rm” Command
Run the following commands to check the list of the files, folders, and links of the current location, remove the symbolic link using the “rm” command, and check whether the link has been removed or not:
file.
$ rm c2
$ls
It is shown in the following output that there is a text file and a symbolic link in the current location and the c2 symbolic link has been removed:
Example 3: Remove a Link Using Bash Script
Create a Bash file with the following script that takes the link name for removal. If the link exists in the current location, the link will be removed.
file.
#Take the link name from the user
read -p "Enter the link name to remove:" linkname
# Check whether the link exists or not
if [ -L $linkname ]; then
rm $linkname
echo "$linkname link is deleted."
fi
It is shown in the following output that there is a symbolic name f1 in the current location. This link is removed after executing the script and taking the link name as input.
Conclusion
The different ways of removing files, directories, and links are shown in this tutorial using the “unlink”, “rm”, and “rmdir” commands. We hope that this tutorial will help the new Linux users to know the uses of these commands and apply these commands properly to remove the files, directories, and links in Linux.