1. Introduction
A typical task for a Linux user is to delete files or directories. Moreover, this is significant especially when it comes to managing files and space.
Although rm and unlink commands may seem very similar in their functionality, there are some key differences between the two.
Since using the wrong command in the wrong situation can result in unintentional data loss or corruption, therefore in this tutorial, we’ll shed the light on the key differences between both commands.
2. Using rm Command
Perhaps the most popular method to delete a file or a directory involves using the rm command.
In short, we’ll examine the most common uses of rm and its options when it comes to deleting files or directories:
$ ls
test1.txt
test2.txt
test3.txt
test4.txt
mydir/
$ rm test1.txt test2.txt
$ ls
test3.txt
test4.txt
mydir/
Using the ls command in the above example, we know that in our current working directory, we have 4 files and 1 subdirectory. Although we could have deleted the test1.txt and test2.txt files separately in two different commands, we opted to utilize the edge of the rm command of taking one or more filenames or directories as arguments.
Finally, we used ls again to confirm that the 2 files are deleted successfully.
Let’s have a look at the most used options that can be used with the rm command:
$ rm -i test3.txt
rm: remove regular file 'test3.txt'? y
$ ls
test4.txt
mydir/
As a matter of fact, some Linux users or administrators are extra cautious when it comes to deleting files or directories. Hence, the -i option is used to display a confirmation message before the delete operation takes place. Typically, when the letter y is entered as a response, the operation takes place.
Afterward, we used ls again to confirm that the test3.txt file had been removed.
Moreover, let’s check the -r option with the rm command:
$ stat mydir
File: mydir
Size: 4096 Blocks: 8 IO Block: 4096 directory
Device: 10304h/66308d Inode: 2162719 Links: 2
Access: (0775/drwxrwxr-x) Uid: ( 1000/ user) Gid: ( 1000/ user)
Access: 2023-03-07 09:00:00.000000000 -0500
Modify: 2023-03-07 09:00:00.000000000 -0500
Change: 2023-03-07 09:00:00.000000000 -0500
$ rm -r mydir/
$ stat mydir
stat: cannot stat 'mydir': No such file or directory
To begin with, the stat displays detailed information about the mydir directory, which indicates it exists. Consequently, rm -r is applied to the mydir directory, where, the -r option indicates the recursive deletion of files and other subdirectories under mydir.
Last but not least, as a confirmation of successful deletion operation, we use the stat command again and give it the mydir directory as an argument. Accordingly, it returns No such file or directory, which means that the directory has been removed successfully.
3. Using unlink Command
In this section, we’ll learn about using the unlink command for deleting files. Theoretically, unlink can’t delete directories. However, it is commonly used in deleting files or symbolic links.
Let’s use the unlink command to delete the test4.txt file:
$ unlink test4.txt
$ ls
In this example, the no output of ls notifies us that the above operation has been successful.
Unlike the rm command, unlink doesn’t allow deleting multiple files at once:
$ ls
file1.txt
file2.txt
file3.txt
$unlink *.txt
unlink: extra operand ‘file2’
Try 'unlink --help' for more information.
In essence, the above output shows that unlink faces difficulties deleting more than one file.
Since rm command performs a security check. If we don’t have write permission on a file, it will ask us to confirm it interactively or use the –force option:
$ ls
file2.txt
$ rm file2.txt
rm: remove write-protected regular empty file ' file2.txt'? n
$ unlink file2.txt
$ ls
First, we notice that the current working directory holds file2.txt only. Next, with unlink, there are no safety checks. It will delete the write-protected file. Finally, using the ls command, we notice the successful deletion of the file.
4. Recovering Files
There is a huge difference between using rm and unlink commands when it comes to recovering files. In this section, we will discuss these differences.
Let’s discover how to use the rm command to move files into the trash:
$ rm -v -t test.txt
removed 'test.txt' -> '/home/user/.local/share/Trash/files/test.txt'
In the above example, the test.txt file is removed to trash using rm command. In addition, the -v option prints a message for each file it deletes. Moreover, the -t option moves files to the trash folder instead of deleting them permanently. However, to force the deletion of files, without prompting for confirmation, use the -f option.
On the other hand, permanently deleting a file using unlink only requires the file name as an argument:
$ unlink test.txt
Let’s try to recover the file we have deleted using unlink:
$ restore-trash test.txt
Error: Unable to find file 'test.txt' in trash!
In order to restore files from trash, we use the restore-trash command. However, since unlink deletes files permanently, it is impossible to recover the test.txt file.
Alternatively, deleted files using rm command are recoverable:
The following files will be restored:
/home/user/.local/share/Trash/files/test.txt
Do you want to restore all of them? [y/N] y
In addition, we must confirm which files we need to recover from the trash folder by typing Y or N.
5. Differences Table
In this section, we’ll list the main differences discussed above in a table format.
Comparison Factor
rm
unlink
Files & Directories Deletion
Can delete files and directories
Can only delete files and can’t delete directories
Accepts Multiple Arguments
Can delete multiple files or directories by accepting multiple arguments such as file1 file2
Accepts only 1 argument
Available Options
-f, -i, -t, -I, -r, -R, -v, –help & –version
only –version & –help
Recoverable
restore-trash
Option Unavailable
In summary, the table above outlines the crucial differences between the rm and unlink commands. By understanding these differences, users can choose the appropriate command for their specific needs and ensure that they delete files and directories safely and effectively.
6. Conclusion
In this article, we looked at the differences between rm and unlink commands.
By understanding the differences between and unlink, we can use these commands more effectively and avoid unintentionally deleting files.