How to Delete a File or Directory via Terminal – Command to Remove a Folder and its Contents
Here are the steps to delete a directory in Linux using the rm command:
- Open the terminal: To delete a directory/file in Linux, you need to use the command line. Login to the Bluehost main account and select the server. Then browse the button or link “Server Console”.
- Server Login: Enter root login credentials, if asked
- Navigate to the directory you want to delete: Use the cd command to navigate to the directory you want to delete. For example, if the directory you want to delete is called my_directory and is located in your home/userdirectory_name/public_html folder, type cd /home/userdirectory_name/public_html and press "Enter".
The cd command is used to change the current working directory in a command-line interface.
Here's the basic usage:
- cd - Changes the working directory to the user's home directory.
- cd directory_name - Changes the working directory to the specified directory.
- cd .. - Moves up one level in the directory hierarchy (to the parent directory).
- cd /path/to/directory: Changes the working directory to the specified absolute path.
- Check the contents of the directory: Before deleting the directory, it is a good idea to check its contents to make sure you are deleting the right directory. Use the ls command to list the contents of the directory. For example, type ls -l and press "Enter" to see the files and folders inside the my_directory folder.
Here are some common usages:
- ls - Lists the files and directories in the current working directory.
- ls directory_name - Lists the contents of the specified directory.
- ls -l - Displays a detailed listing, including additional information such as file permissions, owner, size, and modification date.
- ls -a - Lists all files, including hidden files (those whose names start with a dot .).
- ls -h - Human-readable file sizes (e.g., KB, MB, GB).
- ls -R - Recursively lists the contents of subdirectories.
- ls -t - Sorts files by modification time, newest first.
- ls -S - Sorts files by size, largest first.
- Delete the directory and its contents: To delete the directory and all its contents, use the rm command with the -r option, which stands for recursive and it may prompt the user for confirmation before deleting each file and directory.
Type rm -r my_directory and press “Enter”. You will be prompted to confirm the deletion. Type y and press "Enter" to confirm.
Type rm -r * and press “Enter”. (This command will remove all files and directories in the current directory and its subdirectories.)
When combined, rm -rf can be used to forcefully and recursively delete directories and their contents without asking for confirmation.
Here's a breakdown of the command:
- rm - Stands for "remove."
- -r - Stands for "recursive," which means it will remove the specified directory and its contents, including subdirectories.
- -f - Stands for "force," which suppresses most error messages and prompts, making the removal non-interactive.
- * - Represents a wildcard character that matches any file or directory.
- Verify that the directory has been deleted: To verify that the directory has been deleted, use the ls -l command to list the contents of the parent directory. For example, if the my_directory folder was located in your public_html folder, type ls -l and press "Enter". The my_directory folder should no longer be listed.
Note: Be very careful when using the rm -r command, as it can delete files and directories irreversibly.