Deleting Files and Folders via SSH: A Step-by-Step Guide

When managing files and folders on a server through SSH (Secure Shell), it’s crucial to know how to delete them properly. In this step-by-step guide, we’ll walk you through the process of deleting files and folders using SSH, ensuring a smooth and efficient workflow.

Step 1: Connect to the Server

Begin by establishing an SSH connection to the server where the files or folders you want to delete are located. Use your preferred SSH client or the terminal on your local machine to connect to the server.

Step 2: Navigate to the Target Directory

Once connected, navigate to the directory containing the files or folders you wish to delete. You can use the “cd” command to change directories. For example, if the target directory is “/var/www/mywebsite”, you would execute the following command:

cd /var/www/mywebsite

Step 3: Delete a File

To delete a file via SSH, use the “rm” command followed by the file’s name. For instance, if you want to delete a file named “example.txt”, execute the following command:

rm example.txt

This will permanently remove the file from the server.

Step 4: Delete an Empty Folder

Deleting an empty folder is a straightforward process. Utilize the “rmdir” command followed by the folder’s name. For example, to delete an empty folder named “docs”, execute the following command:

rmdir docs

Ensure that the folder is empty before attempting to delete it, as the “rmdir” command only works on empty directories.

Step 5: Delete a Folder and Its Contents

To delete a folder and its contents recursively, including all files and subdirectories, use the “rm” command with the “-r” flag. For example, if you want to delete a folder named “images” and its contents, execute the following command:

rm -r images

Exercise caution when using the “-r” flag, as it permanently deletes the folder and its contents without confirmation.

Conclusion

Deleting files and folders via SSH is a fundamental skill for managing server resources. By following the steps outlined in this guide, you can confidently delete files, empty folders, and entire directories with their contents. Remember to exercise caution and verify your actions to prevent accidental deletion. With this knowledge, you can efficiently manage your server’s file system through SSH.