8. Deleting and Cleaning Up: rm, rmdir
Caution is key when deleting files in the command line, as there is no 'Recycle Bin' in Termux.
1. Removing Directories (rmdir)
rmdir stands for Remove Directory. Important: It only works if the directory is completely empty.
bash
Create an empty directory
$ mkdir empty_folder
Remove the empty directory
$ rmdir empty_folder
If you try to remove a non-empty directory, it will fail
$ rmdir scripts rmdir: failed to remove 'scripts': Directory not empty
2. Removing Files and Non-Empty Directories (rm)
rm stands for Remove. It is the primary command for deleting files.
Removing Files:
bash
Delete a single file
$ rm old_log.txt
Delete multiple files
$ rm file1.tmp file2.tmp
Removing Directories (Recursive Deletion):
To delete a folder and all its contents (including subfolders and files), you must use the -r (recursive) flag.
bash
REMOVE WITH CAUTION! This deletes the 'scripts' folder and everything inside.
$ rm -r scripts
Forced Removal (Danger Zone!)
If you want to remove files without the system prompting for confirmation, you can use the -f (force) flag. Combining them is highly powerful and dangerous:
bash
Deletes 'projects' directory and all contents without asking
$ rm -rf projects
Always double-check your path before executing rm -rf!