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.
Delete all files in the directory
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:
rm -r /home/ubuntu/mydata/*
You can verify using ls -al /home/ubuntu/mydata to confirm all files and subdirectories in it are deleted.
Delete all files in the current directory
As mentioned before safest way would be to navigate to the directory and delete files from the current directory.
1. Navigate to the 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.
2. List directory contents
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).
3. Delete all files in the Directory
Once we confirm we are in the right directory, we can delete all files in the current directory using rm -rv *.
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.
rm -rv .*
The individual directories . (indicates current directory) and .. (indicates parent directory ) will be still there - which cannot be deleted. No harm in keeping it.
Delete all files in directory without prompt
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:
rm -rf *
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.