To delete a file or folder using SSH, we use the rm
command accompanied by some options as the circumstance may require. Below, we show you the different ways and options of deleting a file or folder using SSH.
Delete a Single File Using SSH
To delete a single file using SSH, use the command below:
rm path/to/file/filename.extension
For example, if the file is named myfile.txt
and is located in the home/user/
directory, then use the command below.
rm home/user/myfile.txt
Of course, if the file is located within the current directory of operation, we don't need to type the full path to the file. Instead, we simply use the file name without the path, that is;
rm filename.extension
For example
rm myfile.txt
Delete a Single Folder Using SSH
To delete a single folder using SSH, we use a similar syntax to that of deleting a single file, that is,
rm path/to/folder
For example, to delete a folder named coconuts
located in the home/user/
directory, we use
rm home/user/coconuts
Just like for file deletion, if the folder we want to remove is located within the current directory of operation, then, we simply use the folder name without the full path, that is;
rm folder
For example, if the folder name is coconuts, then we simply use
rm coconuts
Delete a Single Folder and All Its Content (Sub-folders and Files) Recursively
To delete a single folder and all its sub-folders and files (recursively), we shall use the rm
command together with the -r
option, that is;
rm -r path/to/folder
For example, to delete a folder named coconuts
located in the home/user/
directory together will all its files and sub-folders, we shall use;
rm -r home/user/coconuts
Similarly, just like discussed earlier, if the folder is located within the current directory, we shall simply use the folder name without the path, that is;
rm -r folder
For example, if the folder is named coconuts
, then we simply use
rm -r coconuts
Delete All Files, Folders, and Sub-folders Within the Current Folder or Directory
Like you must be already aware, we use *
to refer to all. That means, if we use the rm command with * in place of the file/folder path, then we are commanding it to remove all. Therefore, to delete all files, folders, and sub-folders within the current directory, we shall simply use
rm -r *
Conclusion
You now know how to delete files and folders using SSH or shell access. We hope you had a nice read! It's important to note that in each of the commands above, you will be prompted for confirmation before the command is executed. If you would like to read our other article on how to remove files and folders using SSH without being prompted for a confirmation, please find it here.