There is a command called rmdir which is short for remove directory. However, this rmdir command can only be used for deleting empty directories.
If you try to delete a non-empty directory with rmdir, you’ll see an error message:
rmdir: failed to remove 'dir': Directory not empty
There is no rmdir force. You cannot force rmdir to delete non-empty directory.
This is why I am going to use the same rm command to delete folders as well. Remembering the rm command is a lot more useful than rmdir which in my opinion is not worth the trouble.
To remove an empty directory, you can use the -d option. This is equivalent to the rmdir command and helps you ensure that the directory is empty before deleting it.
To remove directory with contents, you can use the recursive option with rm command.
This will delete all the contents of the directory including its sub-directories. If there are write-protected files and directories, you’ll be asked to confirm the deletion.
If you want to avoid the confirmation prompt, you can force delete.
You can also delete multiple directories at once with rm command.
Awesome! So now you know how to remove directory in Linux terminal.
Here’s a summary of the rm command and its usage for a quick reference.
Purpose | Command |
---|---|
Delete a single file | rm filename |
Delete multiple files | rm file1 file2 file3 |
Force remove files | rm -f file1 file2 file3 |
Remove files interactively | rm -i *.txt |
Remove an empty directory | rm -d dir |
Remove a directory with its contents | rm -r dir |
Remove multiple directories | rm -r dir1 dir 2 dir3 |
I hope you like this tutorial and learned to delete files and remove directories in Linux command line. If you have any questions or suggestions, please leave a comment below.
You may need to delete files from your directory to free up space, clean up a project, or remove malware files. In this tutorial, we will learn how to delete all files in the current directory in Linux.
To delete all files in the directory use rm command followed by the path to the directory and a wildcard character «*».
This command will delete all files in the directory, but it will not delete any subdirectories or their contents. to delete subdirectories as well, you can add the «-r» flag to the command to make it recursive.
With that note: In Linux, it is best practice to navigate to a desired directory and perform the action from the current directory. Using an absolute path is more prone to accidentally choosing the incorrect path and may end up deleting the wrong files.
To delete all files in the directory /home/ubuntu/mydata/, type:
Here all the non-hidden files in the directory are deleted. However, it returns an error for sub-directories. On a side note, you can exclude a specific file from deletion by rm -v !(filename.txt).
To remove all files and sub-directories from the directory /home/ubuntu/mydata, type:
You can verify using ls -al /home/ubuntu/mydata to confirm all files and subdirectories in it are deleted.
As mentioned before safest way would be to navigate to the directory and delete files from the current directory.
Use cd command to change the directory. For example, we are using /home/ubuntu/mydata.
Change to /home/ubuntu/mydata:
Use pwd command to confirm the current path:
This will print out the absolute path of the directory.
List the directory content to make sure you going to delete the right files. For listing all files in the current directory use ls -al command.
This command will list all the contents of the directory /home/ubuntu/mydata such as sub-directories and files ( including hidden files).
Once we confirm we are in the right directory, we can delete all files in the current directory using rm -rv *.
This command will delete all the files and subdirectories in the current directory. The -v option gives a verbose output to show removed files and the directory.
Note: If you have any write-protected files and directories rm will prompt.
You can verify by typing ls -al
If you have any manually created hidden files, to remove them all use rm -rv .* command. This is because the asterisk (*) does not match files that begin with a dot, which are hidden files.
The individual directories . (indicates current directory) and .. (indicates parent directory ) will be still there — which cannot be deleted. No harm in keeping it.
Use rm -rf * to delete all files in the directory without any prompt. Prompt occurs when rm encounters any errors such as write access protection. This option helps to avoid it.
For example, to delete all files in the directory /home/ubuntu/mydata without any prompt, type:
We verify that the files have been deleted by running:
In this tutorial, we learned how to delete all files in a directory in Linux. Be cautious when using rm with asterisk command, as it can potentially delete important files. Always double-check the files you want to delete before executing the command.
If this resource helped you, let us know your care by a Thanks Tweet. Tweet a thanks
Adblock