When you are new to the command line, even the simplest of the tasks may leave you searching the internet.
Take the removal of files for example. You don't have the luxury of right-clicking and opting for the delete option here.
But when you know the right commands, things are not as scary.
To delete a file in the Ubuntu terminal, you can use the
rm command like this:
You won't see any output by default if the file is removed successfully.
$ ls file_to_be_deleted $ rm file_to_be_deleted $
You may also provide the path of the file if you are not in the same directory.
There are a few more things about removing files in the command line that you may want to know. Let's see it in a bit more detail.
Remove a single file in the command line
Although there are other commands that you can use to remove files, the
rm command is the one that is used more frequently than others, and for a reason -- the ease of use and versatility.
Let us first look at the syntax of
That's quite simple, right? It shows no output for successful deletion. You can make it show an output by using the verbose mode.
Removing files in verbose mode
To see an output with the rm command, use the option
rm -v filename
I have a file with the name 'file_to_be_deleted' and I wish to delete it. Let's see how to remove this file using the
$ ls file_to_be_deleted $ rm -v file_to_be_deleted removed 'file_to_be_deleted'
As evident from the output, the desired file has now been removed.
This verbose mode is helpful when you are removing multiple files. You can see which files were deleted.
Removing multiple files
rm command is not limited to removing only a single file at a time. You can delete as many files as you want. Just provide the filenames or paths:
rm file1 file2 file3
Here's a practical example of deleting multiple files with the optional verbose mode:
$ rm -v file_to_del_1 file_to_del_2 file_to_del_3 file_to_del_4 file_to_del_5 removed 'file_to_del_1' removed 'file_to_del_2' removed 'file_to_del_3' removed 'file_to_del_4' removed 'file_to_del_5'
Removing multiple files using wildcard or regex
It's easy to provide file names if you have 5 of them. It won't be easy if you have 50 of them.
If the filenames follow a certain pattern, you can use wildcard or regex to delete them all at once.
For example, in my current directory, I have files that end with the extension '.exe', which aren't terribly useful on Ubuntu.
So let us see how to remove them.
$ ls -1 *.exe cutting_edge_chromium.exe defender_of_windows.exe my_outlook_on_this_world.exe totally_legit_ms_office.exe
rm command supports specifying files using wildcards. You can use regular expressions too, but that is too complicated for our simple demonstration.
Here, I will use the asterisk wildcard character to achieve this.
$ rm -v *.exe removed 'cutting_edge_chromium.exe' removed 'defender_of_windows.exe' removed 'my_outlook_on_this_world.exe' removed 'totally_legit_ms_office.exe'
This removed all the '.exe' files, regardless of their names. How amazing is that? But it's also risky because you don't know which files are being removed.
Get prompted before file removal
Using wildcards and/or regular expressions to remove a large number of files is very convenient. But it is also very dangerous. Maybe there was a file that you did not intend to remove but is now removed due to some misconfiguration with the wildcard and/or regular expression.
For this, use the
-i option. This option will make the
rm command ask you if you wish to desire a file. This will be done for every file that is specified for removal.
$ ls *del* do_not_delete file_to_delete marked_for_deletion never_delete_this unsure_about_deletion $ rm -v -i *del* rm: remove regular empty file 'do_not_delete'? n rm: remove regular empty file 'file_to_delete'? y removed 'file_to_delete' rm: remove regular empty file 'marked_for_deletion'? y removed 'marked_for_deletion' rm: remove regular empty file 'never_delete_this'? n rm: remove regular empty file 'unsure_about_deletion'? n
I can specify which file to keep or which file to remove using
As you can see, I kept the files 'do_not_delete', 'never_delete_this' and 'unsure_about_deletion', but I let
rm remove files 'file_to_delete' and 'marked_for_deletion'.
This can come in very handy when you are not sure about which files to delete, or when there are a large number of files present and you want to be sure before removing them.
Force delete write-protected files
I have a file, and its permissions are as shown below:
$ ls my_special_file my_special_file
If you closely, I do not have permission to write. This means that I can not modify 'my_special_file'. When I try to remove it, it will ask me if I want to remove a write-protected file.
$ rm -v my_special_file rm: remove write-protected regular empty file 'my_special_file'? y removed 'my_special_file'
If I do not want a prompt, I will use the
-f option to remove files forcefully.
$ rm -v -f my_special_file removed 'my_special_file'
Bonus Tip: Using the 'unlink' command to remove files
The GNU Coreutils, along with the
rm command, also provides
unlink command for deleting files.
The syntax is as follows:
There are two core differences between
unlinkcommad does not have any additional options to extend or modify its behavior
unlinkcommand does not allow specifying multiple files at once for removal.
Let's see unlinking these two files:
$ ls unlink_* unlink_that unlink_this $ unlink unlink_this unlink_that unlink: extra operand ‘unlink_that’ Try 'unlink --help' for more information.
Oh right! I forgot. Unlink only one file at a time.
$ unlink unlink_this
Do note that there is no option to enable/increase verbosity, so I can not show you what the unlink command did except say that upon successful completion, it outputs nothing.
In this case, there was no output, so the 'unlink_this' file was removed/unlinked successfully.
rm command is what I personally prefer, as it is used for removal of files as well as directories. Yes, the same rm command is used for deleting directories as well.
I hope you learned a few good tips on deleting files in command line in Ubuntu Linux.