This post will address a common problem experienced by some users who need to remove undesired files and directories securely. This guide will cover the 8 methods to delete files, folders, or directories via SSH. Furthermore, we will also discuss the most effective recovery tool in case you accidentally lose or delete your data. Let’s get started!
Prerequisites
Before proceeding with deletion via SSH, it is crucial to ensure that you have proper permissions and access to the files, folders, and directories you wish to delete. It can be done by connecting to the server using an account with sufficient privileges. Additionally, it is recommended to back up important files and folders to avoid accidental deletion or data loss.
Follow these steps to delete files and directories using SSH:
1. Enable SSH Access
Step 1. Click Websites > Manage > SSH Access to enable SSH access for your account.
Step 2. Select Enable.
You're now able to link to your account using SSH.
2. Connect to Your Account Using SSH
After enabling SSH access, perform these actions.
Step 1. Copy the SSH command.
Step 2. See the table below for applications based on your device's operating system.
Linux |
macOS |
Windows |
For SSH connections, you can use Terminal. Click Ctrl + Alt + T or search Terminal to launch it. | You can also use Terminal. To access it, look for Terminal in Spotlight. | Any SSH client is OK. |
Step 3. Insert the command you copied in the first step into Terminal. Insert the PuTTY parameters as described in the tutorial.
For both contexts, the following will happen:
The authenticity of host '[185.185.185.185]:65002 ([185.185.185.185]:65002)' can't be established.
ECDSA key fingerprint is
SHA256:Eze2ImXw1MWfgAMbcWxfc2EQwAkgTHOEPsNwSnEAHAQ.
Type yes
to continue.
Step 4. The password will then be requested. Please enter your account’s password to connect it using SSH.
Note:
● The password will not be displayed for security reasons, but data is still inserted. Don't worry about missing symbols; enter the password and press Enter.
● Passwords can be copied and pasted by right-clicking on a computer mouse. In Windows, it immediately inserts the password; in Linux, it displays a menu of options.
● The SSH password is the same as your primary domain's FTP password. If you need help determining the exact value, you can create a new FTP and SSH password or an SSH-only password.
● If the terminal/console closes immediately after entering the password, disable SSH access, re-enable it, and reconnect.
3. Locate the File(s), Folder, or Directory You Want to Remove
After enabling SSH and connecting to your account, you can locate the data you want to delete.
Access the website root directory and execute the following command:
ls
If the desired content is in a subdirectory, reach it by entering the following:
cd path/to/files
Where "path/to/files" is replaced with the path to your files. For example, if your files are in public html/new/version1, the command should be "cd new/version1".
How To Remove File(s), Folder, or Directory via SSH
Now that the necessary prerequisites have been covered, let's move on to deleting files, folders, and directories via SSH. This process can be done using various terminal commands.
If you find it overwhelming to read the instructions, you can freely watch this video to delete files and folders via SSH.
1. Delete a Single File in a Given Folder via SSH
When using SSH and the rm command to remove a file or folder, you will be asked for confirmation before the deletion is carried out. However, this is only sometimes what we want. We might not want to confirm each file, folder, and sub-folder.
To recursively delete a file, folder, or the contents of a folder using SSH, we use the -f (force) option within the rm command.
To delete a single file within a specified directory without requiring confirmation, we’ll use this command:
rm -f path/to/directory/filename.extension
Here’s an example. To delete a file named myfile.txt located in the home/user/ directory, we will run the following syntax:
rm -f home/user/myfile.txt
2. Delete a Single File in the Current Working Directory via SSH
Deleting a single file within the current working directory is much easier using SSH without confirmation. We skip the directory part of the previous command.
As a result, we will employ the following command:
rm -f filename.extension
For example, assume that you want to delete a file named myfile.txt from the current working directory without being asked for confirmation. You must run this command:
rm -f myfile.txt
3. Delete Multiple Files at One Time via SSH
To delete multiple files in the current directory at one time via SSH, run the following syntax:
rm -rf *
4. Delete a Folder in a Given Directory via SSH
To delete a folder within a given directory without providing confirmation, we use the command similar to deleting a file within a given directory.
Run the command that follows:
rm -f path/to/diretory/foldername
For example, if you would like to delete a folder named myfolder in the home/user/ directory without prompting, type:
rm -f home/user/myfolder
5. Delete a Folder With All Its Contents in a Given Directory via SSH
Using SSH to delete a folder and all its contents (recursively) without being prompted for confirmation is as simple as the other scenarios described above.
We should employ:
rm -rf path/to/directory/foldername
For example, to recursively delete a folder named myfolder located in the home/user/ directory, enter this command:
rm -rf /home/user/myfolder
6. Delete a Folder in the Current Working Directory via SSH
We use the command in the preceding section and leave out the path/to/directory/ part when deleting a folder in the current working directory using SSH without confirmation.
In this part, we will enter the name of the folder. As a result, the command will be:
rm -f foldername
For example, if the folder to be deleted is called myfolder, the command will be:
rm -f myfolder
7. Delete a Folder With All Its Contents in the Current Working Directory via SSH
To remove a folder with all of its contents recursively, with the folder in the current directory, use the command:
rm -rf foldername
That is, if the folder is called myfolder, the command to delete it and all of its files and subfolders without prompting will be:
rm -rf myfolder
8. Delete All Contents of the Current Working Directory via SSH
Use the command below to delete all content (files, folders, and sub-folders) in the current folder or directory of operation without requiring confirmation.
rm -rf *
How To Recover Accidentally Deleted Files/Folder/Directory via SSH
Accidentally deleting files, folders, or directories via SSH is inevitable, yet a frustrating experience when you remove necessary files. So, are you sick of these scenarios? Let Wondershare Recoverit help you! Recoverit is a powerful Linux data recovery software that can help recover accidentally deleted files, folders, or directories from your server via SSH. This software has a high success rate in retrieving lost data and is user-friendly, making it easy for even those with little technical knowledge to recover their files. In the event of accidental file deletion on your server via SSH, Wondershare Recoverit can be a lifesaver by helping you recover your important files and folders.
Wondershare Recoverit – Leader in Data Recovery
5,481,435 people have downloaded it.
Ability to recover over 1000 file types from almost any storage media, such as Windows/Mac/Linux PCs, NAS Servers, USB drives, SD cards, external hard disks, digital cameras, etc.
Efficiently handle various data loss scenarios, including accidental deletion, emptied recycle bins or trash, formatting, lost partition, virus attack, RAW, and RAID.
An intuitive interface that lets you preview the files before recovery. No payment is required if the files are unrecoverable.
The Secure Shell Protocol (SSH) has been used for many years to access Unix, Linux, and BSD machines remotely. Although SSH can now be used on Windows and Mac, it is primarily a tool for Linux-based systems. In this context, it is crucial to know how to recover deleted files, folders, or directories on a Linux machine via SSH. That's where Wondershare Recoverit comes in. Below, we will show you how to use Wondershare Recoverit to recover deleted files, folders, or directories via SSH in Linux.
Step 1. Choose Linux Recovery
Launch the Wondershare Recoverit software after downloading and installing. Then, select the NAS and Linux options on the main interface's left side. To proceed, click Linux Recovery.
Step 2. Make a Remote Connection
Enter the necessary information in the new window that will appear to establish a remote connection. When you're finished, click Connect.
Once the connection is established, Recoverit will launch an automatic scan to look for your lost data on a Linux computer.
Step 3. Recover Deleted Files
The scanning procedure can take some time, depending on how much data is being scanned; the scanning status is shown in the bottom bar. You can pause scanning whenever a desired file is found.
When the scanning is finished, Recoverit allows you to preview the files to ensure they are the ones you want to recover. Choose the found files or folder and click Recover to save them to a secure location.
For Windows Vista/7/8/10/11
For macOS X 10.10 or later
Here’s a video clip for you to watch for a more in-depth tutorial.
For Windows Vista/7/8/10/11
For macOS X 10.10 or later
Conclusion
You now understand how to use SSH to delete files, folders, subfolders, and all contents without confirmation. These 8 methods will help you remove unnecessary files while choosing the desired actions. However, if you accidentally delete the wrong files, Wondershare Recoverit will come to rescue your data. Give it a try!