In our previous article, we showed you how you can delete a file, folder or even sub-folders using SSH. And we also saw that in each of the commands we provided, you will be required to provide confirmation before the deletion is completed. However, sometimes, we want to delete files, folders or even sub-folders using SSH without providing confirmation. There are those times when it is practically unfeasible to keep providing confirmation. One scenario is when we are deleting many files or recursively deleting the content of a folder.
In this article, we shall show you how you can delete files, folders or sub-folders using SSH without being prompted for confirmation. We have sub-divided this article into the following sections:
- Introduction
- Deleting a Single File in a Given Folder
- Deleting a Single File in the Current Working Directory
- Deleting a Single Folder in a Given Directory
- Deleting a Single Folder Plus all its Content (Sub-folders and Files) in a Given Folder
- Deleting a Single Folder in the Current Working Directory
- Deleting a Single Folder Plus all its Content (Sub-folders and Files) in the Current Working Directory
- Deleting all the Content of the Current Working Directory
- Conclusion
Introduction
When using the rm
command to delete a file or folder using SSH, you will be prompted for confirmation before the deletion is executed. However, this is sometimes not what we want. We may not want to keep providing confirmation for each of the files, folders, and sub-folders.
To delete a file, folder or contents of a folder recursively using SSH without providing confirmation, we use the -f
(force) option within the rm command.
Below, let's see how to apply this to different situations.
Delete A Single File in a Given Folder
To delete a single file within a given directory using SSH without confirmation, we use the command;
rm -f path/to/directory/filename.extension
For example, to delete a file named myfile.txt
located within the home/user/
directory, we shall use;
rm -f home/user/myfile.txt
Deleting a Single File in the Current Working Directory
Deleting a single file within the current working directory using SSH without confirmation is pretty much easier. We simply skip the directory part of the command above. We shall thus use the command
rm -f filename.extension
For instance, to delete a file named myfile.txt
located within our current working directory using SSH without being asked for confirmation, we type
rm -f myfile.txt
Deleting a Single Folder in a Given Directory
Similar to deleting a file within a given directory, to delete a folder located within a given directory without providing confirmation, we use the command
rm -f path/to/diretory/foldername
For example, to delete a folder named myfolder
located in the home/user/
directory without confirmation, simply use;
rm -f home/user/myfolder
Deleting a Single Folder Plus all its Content (Sub-folders and Files) in a Given Folder
Just like the other scenarios above, deleting a folder and all its content (recursively) using SSH without being prompted for a confirmation is simple. We simply use
rm -rf path/to/directory/foldername
For example, to delete a folder named myfolder
located in the home/user/
directory recursively without confirmation, simply use;
rm -rf /home/user/myfolder
Deleting a Single Folder in the Current Working Directory
When deleting a folder located in the current working directory using SSH with no confirmation, we use the command in the above section and simply leave out the path/to/directory/ part. We simply enter the folder name. Thus, the command will come to:
rm -f foldername
For example, if the folder we want to delete is named myfolder
, then the command will be:
rm -f myfolder
Deleting a Single Folder Plus all its Content (Sub-folders and Files) in the Current Working Directory
To recursively delete a folder with all its content, with the folder located in the current directory, we use the command:
rm -rf foldername
That means if the folder name is myfolder
, then the command to delete it with all its files and sub-folders without having to enter confirmation will be:
rm -rf myfolder
Deleting all the Content of the Current Working Directory
To delete all content (files, folders, and sub-folders) of the current folder or directory of operation without confirmation, simply use the command
rm -rf *
That's it. Nothing more.
Conclusion
You now know how to delete files, folders, and sub-folders using SSH without being prompted for a confirmation.
It's worth mentioning that you should be extra careful when using the -rf *
options. This is because these options will cause the deletion of all the files, folders and sub-folders in the provided directory without asking you for confirmation.
If you would like to delete files, folders or sub-folders using SSH while providing confirmation for each of the items, simply skip the -f
option. Or better, visit our dedicated article here for more details.