How to delete files and folders via SSH

You can delete files through SSH with the rm command. To remove a file that's in the current working directory just type the command and the file's name (including the file extension type). For example:

rm example.txt

will remove the text file example.txt from the current working directory. You can also use the full path to the file, if it's not in the current working directory and you don't want to change the working directory (e.g. rm /home/username/public_html/example.txt). When you type the command and you execute it by pressing Enter on your keyboard, you'll be asked for confirmation that you want to delete the file. Type y to confirm and press Enter on your keyboard. In case you don't want to be asked for confirmation use the -f option:

rm -f example.txt

You can also delete several files at the same time by listing them one after the other (separated with a single space). For example:

rm example.txt example.php example.html

will delete the three listed example files from the current working directory. You can use wildcards (*) to delete all the files from the same type. For example:

rm *.txt

will delete all text files in the current working directory.

To delete empty folders you can use the rmdir command. For example:

rmdir example_folder

will remove the directory example_folder from the current working directory. As we mentioned, the directory has to be empty. Another solution is to use the command rm -rf followed by the name of the folder(s). This command will remove the directory together with all the files and subdirectories in it without prompting you for confirmation. For example:

rm -rf example_folder

will remove example_folder with everything inside it from the current working directory.

As with other commands, if the file or folder has spaces in its name, put the name in quotation marks.

Was this answer helpful?

 Print this Article

Also Read