When managing an Ubuntu Server, you may need to delete files and directories – for instance, to free up disk space or remove outdated or erroneous data.
Warning
This task should be approached with caution to avoid unintended data loss.
- Backup: Before proceeding with deletion, ensure that all important files and directories are backed up.
- Accuracy: Double-check all commands to ensure accuracy before executing them.
Pre-Requisites
- An Ubuntu Server
- Access to the terminal
Deleting Files
Single File Deletion
- Open the terminal on your Ubuntu server.
- Navigate to the directory containing the file you wish to delete.
- Run the following command:
rm filename.extension
Multiple File Deletion
- Navigate to the desired directory.
- Run the following command:
rm file1.extension file2.extension
Deleting Directories
Empty Directory Deletion
- Navigate to the directory above the one you wish to delete.
- Run the following command:
rmdir directoryname
Directory and All Contents Deletion
- Navigate to the directory above the one you wish to delete.
- Run the following command:
rm -r directoryname
Output
When a file or directory is deleted successfully, there will not be any output.
However, if there is an error, you will receive an error message.
For example, attempting to remove a non-empty directory with rmdir
will result in the following error message:
rmdir: failed to remove 'directoryname': Directory not empty
Be mindful that deletion actions are irreversible, and once executed, the data is permanently erased from the server. Hence, adhere to the warnings mentioned at the start of this article to prevent any unintended data loss.