This post may contain affiliate links/ads and I may earn a small commission when you click on the links/ads at no additional cost to you. As an Amazon Affiliate, I earn from qualifying purchases. Techsphinx also participates in the StationX Affiliate program. You can read my full disclaimer here.
This tutorial demonstrates How you can remove files and directory (Folder) in Linux using the terminal.
You can easily delete files and directories graphically, but using a terminal has its unique traits which I will show you later in this post.
As someone said, with great power comes great responsibilities. Using a terminal will make your workflow very smooth and fast but if you delete files and directories using the commands shown in this post, they will not be moved to trash. They will be deleted permanently.
So, be extra careful when executing these commands.
How to Delete Files in Linux?
To delete files in Linux, you can use the unlink or rm command. The unlink command will only delete a single file but with the rm (short for remove) command you can delete multiple files and even directories.
So, I will focus more on the usage of the rm command.
Delete Single File
To remove a single file from Linux, run the unlink command followed by the name of the file which you want to remove.
unlink file1
(Replace file1 with the file you want to delete.)
Now, you can also perform the same action using the rm command.
rm file1
If the file you want to delete is not in the current working directory, then you can specify the full path to the file.
rm /path/to/file/file1
If the file is write-protected, then it will ask you for confirmation.
In that case, type “y” and hit enter.
Delete Multiple Files
When deleting multiple files, check the files you wish to delete if you don’t wish to accidentally delete any important file. As I said before, these files will be permanently deleted.
To delete multiple files in Linux, you can use the rm command.
Run rm command followed by the name of files (separated by a space) you wish to remove.
rm file1 file2 file3 file4
(Make sure there is a space between the rm command and all the files mentioned.)
The above command will delete file1, file2, file3 and file4.
You can also use the wildcard (*) and regular expressions to filter and delete only the files with matching pattern.
rm *.jpg
The above command will delete all the files with the “.jpg” extension from the current directory. It will not delete files with other extensions.
The rm command has many useful options. You can use the “-i” (interactive) option to confirm each file before deleting it. You also have to type “y” and hit enter to delete each file.
rm -i file1 file2 file3 file4
The output of the above command will be something like this:
rahul@techsphinx:~/Desktop/myfolder$ rm -i file1 file2 file3 file4
rm: remove regular empty file 'file1'? y
rm: remove regular empty file 'file2'? y
rm: remove regular empty file 'file3'? y
rm: remove regular empty file 'file4'? y
If you have so many files to remove and you don’t want to confirm for each one of them, then use the “-I” option. This will ask only once for all the files.
rm -rI file1 file2 file3 file4
Output:
rahul@techsphinx:~/Desktop/myfolder$ rm -rI file1 file2 file3 file4
rm: remove 4 arguments recursively? y
Another option is “-f” (force), through which you can delete any file forcefully without asking for confirmation.
rm -f file1 file2 file3 file4
If you wish to see the files that are being deleted, you can use the “-v” (verbose) option.
rm -v file1 file2 file3 file4
The output of the above command will be something like this:
rahul@techsphinx:~/Desktop/myfolder$ rm -v file1 file2 file3 file4
removed 'file1'
removed 'file2'
removed 'file3'
removed 'file4'
You can also combine options. For example, if you wish to delete all the files forcefully but also wants to see what files have been deleted then you can combine the “-f” and “-v” option.
rm -fv file1 file2 file3 file4
How to Remove Directory (Folder) in Linux?
I will show you “rmdir” and “rm” command to remove the directory in Linux. As I stated earlier, the rm command can delete directories too.
If you are new to Linux, then let me tell you this beforehand that folders are called directories in Linux.
Let’s start by removing a single directory.
Remove Single Directory in Linux
To delete a single directory, you can use the “rmdir” command followed by the name of the directory you wish to remove.
Note that, rmdir command will only remove an empty directory.
rmdir dir1
You can also specify the full path, if the directory is not present in the current location.
rmdir /path/to/dir1
Alternatively, you can also use the “rm” command with the “-d” (directory) option to remove an empty directory.
rm -d dir1
The above commands I mentioned remove only the empty directory, to remove a non-empty directory use the “rm” command with the “-r” (recursive) option.
rm -r dir1
The above command will remove all the contents of dir1 first then remove the dir1 directory.
If you wish to see the working of the above command use the “-v” (verbose) option.
Here’s the output of the command with verbose:
rahul@techsphinx:~/Desktop/myfolder$ rm -rv dir1
removed 'dir1/file1'
removed 'dir1/file3'
removed 'dir1/file2'
removed directory 'dir1'
If you wish to remove the directory forcefully, you can use the “-f” option.
rm -rf dir1
Just like with removing files in Linux, you can also use wildcards (*) and regular expressions to delete files matching a particular pattern.
rm -r *_bak
This will remove all directories that end with “_bak”.
Remove Multiple Directories in Linux
You can use the same “rm” command to delete multiple directories. Just run the “rm -r” command followed by the names of directories you wish to remove separated by a space.
rm -r dir1 dir2 dir3 dir4
You can also remove an empty directory inside another directory using the “-p” (parents) option of the rmdir command.
rmdir -p dir1/dir2
The above command will first remove the dir2 and then it will remove its parent directory dir1. I used the verbose option to see the output.
rahul@techsphinx:~/Desktop/myfolder$ rmdir -pv dir1/dir2
rmdir: removing directory, 'dir1/dir2'
rmdir: removing directory, 'dir1'
Wrapping Up!
As you’ve seen in this post, you can use the rm command to remove both files and directory in Linux. The rm command has many options, you can check the manual of the rm command using:
man rm
Also, as previously mentioned, be cautious when removing directories and files in Linux using the “rm” command.
I hope you got to learn something new today, if you have any questions or feedback, feel free to leave a comment.
If you like this post, then follow Techsphinx on Facebook and Twitter for more reviews, tricks, tips and tutorials.
This article needs update or correction? Report the issue here so I can update it.