How To Remove Files And Directories In Linux With Examples.
There are multiple ways to remove files and directories in Linux.
How To Remove Files And Directories In Linux [Examples]
Let’s have a look into the methods to remove files and directories in Linux.
How To Remove Files with rm command
You can easily remove any files in Linux with rm command. Yes, it is pretty simple as you can delete a single file in the current directory with rm command. Open the Linux terminal and type the rm
command with the file name that you want to delete.
rm file_to_be_deleted.txt
What If the file is not in the current working directory?
Don’t worry if the file that you want to delete is not in the current working directory as you can provide path to the file’s location to delete the file.
rm ./path/to/the/file/file_to_be_deleted.txt
How To Remove Multiple Files With rm Command At Once
Run the following command if you want to delete more than one file in Linux with rm command.
rm file_to_be_deleted.txt file_to_be_deleted_1.txt
How To Remove Selected Groups Of Files In Linux
You can use the wildcards to select groups of files that you want to delete at once. The following command will delete all of the doc image files in the current working directory. If the file that you are going to delete is write-protected you will be prompted before the file is deleted.
rm *.doc
How to Remove Directories with rm command in Linux
The process is almost the same as removing files with rm command in Linux. You can remove directories with rm command easily. To remove an empty directory, use the -d
(directory) option.
rm -d directory-to-be-deleted
Run the following command to remove the directories that are not in the current working directory. You need to provide a path to the file’s location to remove the directory.
rm -d /path/to/directory-to-be-deleted
How To Remove Multiple Directories With rm Command At Once
Before removing directories, make sure that you are aware of your directory’s structure. if you are not then you easily do it by installing the tree command.
Run the following command to install this tree command on Ubuntu or another Debian-based distribution.
sudo apt-get install tree
Now, you can run the following command to see the directory structure.
tree
tree path/to/directory
Run the following command to remove multiple directories with rm command. Similarly, If a directory or a file is write-protected, you will be prompted to confirm the deletion.
rm -d directory-to-be-deleted1 directory-to-be-deleted2 /path/to/directory-to-be-deleted3
To delete directories that are not empty, use the -r
(recursive) option. To be clear, this removes the directories and all files and sub-directories contained within them.
rm -r directory1 directory2 directory3
Run the following command to delete directories that are not empty. You need to be extra cautious while using rm -rf command as you might lose your data with one mistake.
rm -rf directory
How to Remove Directories with rmdir
Now, we will use another command to remove directories in Linux and the command is rmdir.
rmdir directory-to-be-deleted
rmdir
can only delete directories that are empty. If you try to delete a folder that is not empty, rmdir
will give you an error message. It will never delete files. Run the following command to delete multiple directories :
rmdir directory-to-be-deleted1 directory-to-be-deleted2 directory-to-be-deleted3
Run the following command to remove the directories that are not in the current working directory.
rmdir /path/to/directory-to-be-deleted
Summary: This is the complete solution to remove files and directories with rmdir and rm commands in Linux. Let us know if you want to add more content and make this post more easy. Don’t forget to share this post on How To Remove Files And Directories In Linux [Examples]