Fundamentals

Deleting files and folders

Easy
10 min

You can use the command line at the bottom of the page to practice commands. Complete the given tasks at the end.

File deletion happens with the rm- command, which comes from the word remove.

rm file.txt

In the image below, we first remove two txt files using the rm command and finally remove the last remaining file. We could have also removed all files by executing rm * .

Deleting folders is slightly different from files, as simply using the rm-command does not allow deleting a folder. There are two ways to delete folders depending on the situation. You can delete an empty folder using the rmdir-command.

rmdir folder

If the folder is not empty, you can use the rm-command with the -r-option. -r-option removes the folder and all the folders and files inside it. The rm-command also has the -d-option, which removes the folder only if it is empty, similarly to rmdir.

rm -r folder # deletes the folder and everything inside the folder
rm -d folder # delete the folder if this is empty
rmdir folder # delete the folder if this is empty

In the picture below, we first remove a directory named kansio2 with the rmdir command. After that, we attempt to remove a directory named kansio and find out that the directory is not empty. Then we delete the directory and the file contained in it using the -r option.

The module lists some use cases for commands, but we recommend familiarizing yourself with and freely experimenting with commands. These can be safely tested in a lab environment.

Training

Practice the topics covered in the module.

Extra: The tasks can be completed with one command. Try to figure out how this would happen.

Exercises

Task 1

Delete the folder ~/folder1

Task 2

Delete the folder ~/folder2 and the file it contains.

hakatemia pro

Ready to become an ethical hacker?
Start today.

As a member of Hakatemia you get unlimited access to Hakatemia modules, exercises and tools, and you get access to the Hakatemia Discord channel where you can ask for help from both instructors and other Hakatemia members.