How to Delete Files and Folders Using Terminal
In the last two sections, we have learned how to create folders and how to create files using command line interface or Terminal on Linux. As always, what can be created must be erasable.
In this Learn Terminal tutorial we will learn how to delete a folder and a file. First, we will see how to delete a file first.
How to Delete Files Using Terminal – rm
To delete a file, you can use the command rm which stands for remove. You just need to add the name of the file you want to delete after the command rm the.
For example, if you want to delete a file called “teks.txt” that we created in the previous How to Create a File Using Terminal tutorial, you can do this by using a command like the following:
rm teks.txt
Then the file named “text.txt” will be deleted. To be sure, you can check by using the command ls as in the image below:
If you usually delete files via Graphic User Interface (GUI) and the deleted files will move to the Trash, and can be downloadedrestore if you change your mind after that, then it’s different from the order rm.
Order rm will permanently delete the file, not move it to the Trash, so be sure to use this command with care.
How to Delete Folders Using Terminal – rmdir
To delete a folder, the command used is rmdir.
Like the long meaning remove directoriesthis command is used to delete an empty directory by using command line interface or Terminals.
For example, if you want to delete a folder named “folder4” that we have created as in the previous section on how to create a folder, you can do this by using a command like the following:
rmdir folder4
Then the folder named “folder4” will be deleted. Just like before, to make sure whether “folder4” has been deleted or not, you can check it by using the command ls like the following picture:
Same as on command CD and orders ls before, command mkdir can also be used with the system reference to delete a folder.
For example, you have a “folder1”, in that folder there is a folder called “sub” and in it there is another sub folder named “child”. To delete the “child” folder, you can do this by using the command:
rmdir folder1/sub/child
Then the “child” folder will be deleted, to make sure you can use the command ls to check whether the “child” folder is still available or has been successfully deleted, as in the image below:
You can delete a folder by: reference using command rmdir This is on a folder that has a longer, unlimited directory structure, provided that the folder is empty.
Then how to delete a folder that is not empty?
How to Delete a Non-Empty Directory Using Terminal
To delete a folder that is not empty or has contents in it, you can use the command rm like the command used to delete the file above.
In order to be able to use the command rm to delete a directory that has contents in it, you must add a parameter “-rf” after command rm the.
For example, if you want to delete a folder called “folder3” which contains several files in it, you can do this by using the command:
rm -rf folder3
Then the folder named “folder3” will be deleted along with its contents. To make sure whether the folder has been successfully deleted or not, you can check it by using the command ls like the picture below:
That’s it, how to delete files and folders using command line interface or Terminal on Linux.
In the next part of the tutorial, we will learn how to duplicate and rename a file using command line interface or Terminal on Linux.