How to delete a file using SSH

In this guide we are going to learn how to delete a file via SSH. We can do this with 1 simple command. With the rm command we can delete files on Linux system. You can also use the unlink command, but it has fewer options.

Deleting a file via SSH is especially useful if you can’t delete it via FTP for example. Let’s look at a few examples to delete files and directories. Be sure to have a backup of the files if you don’t know exactly what you are doing. When you use the rm command there is no way to recover your files.

Rm doesn’t delete folders if you don’t explicitly tell it to do. By default, it only removes files you specify.

Examples of deleting files by SSH

Delete one file, with the name file.txt

rm file.txt

Delete multiple files, with the names file1.txt and file2.txt

rm file.txt file2.txt

You can also use wildcards with the rm option so you can be flexible about what files you want to delete.

For example, this will delete all txt files in the current directory

rm *.txt

How to delete a folder with an SSH command

You can also delete a folder with one single command via the SSH shell. For this action, we can use rm -r or the rmdir command. By default, the rm command won’t delete folders. However, if you specify the -R (recursive) option, it will. Empty directories can be deleted with specifying -d.

Delete the folder with name folderName

rmdir folderName/

How to fix ‘rm: cannot remove ‘folderName/’: Is a directory’

Use the following command to delete a directory with all the files and subdirectories.

rm -rf folderName/

If you want to delete all the files and directories in the current directory, but keep the directory, use the following command.

rm -rf *

FAQ, summary and other SSH commands

How do I delete files in a folder?

To delete all files in a folder you can use this command.

rm /folderName/*

If you also wish to delete all subdirectories and files use the following command.

rm -r /folderName/*

How do I delete a file from my server?

To delete a single file from your server you can use the rm command.

rm file.php

How do I delete a file in PuTTY?

To delete a file via putty you can use the same command (rm) as above.


How do you delete the contents of a folder in Linux?

rm /path/name/*

Use the -r option to also delete all subdirectories and their content
How to rename a file in SSH?

How to list all files in SSH?

To list all files in your SSH client, you can use the ls command. The standard ls command will output all files and directories in the current directory without any formatting.

Use ls -a to also show hidden folders and directories.

ls -a

With these commands, you have successfully learned how to delete a file over SSH. Hopefully, you also understand now how you can delete folders with files with these SSH commands. It is also possible to remove all files in a folder by SSH.