Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

How to Delete Files and Directories Safely in Linux

Use rm for files, rmdir for empty directories, and interactive recursive removal for folders whose contents you have checked.

Table of Contents

Use rm to delete a file, rmdir for an empty directory, and rm -ri to review deletions inside a nonempty directory. These commands normally bypass the desktop Trash. Before pressing Enter, check the path and make sure you have a backup of anything you want to keep.

Delete one file or a short list of files

Use pwd to check your current folder and ls -l -- "file.txt" to check the target. Then use the interactive option:

rm -i -- "file.txt"
rm -i -- "file one.txt" "file two.txt"

Quotes keep names with spaces together. The -- ends option parsing, so a file name beginning with a hyphen is handled as a name. Ordinary rm may remove a file without asking, while -i requests confirmation.

How to delete files and folders using Linux command line Picture 1

How to delete files and folders using Linux command line Picture 2

The older examples below illustrate single-file and multi-file removal. For a manual cleanup, use the interactive commands above so you can review each target.

How to delete files and folders using Linux command line Picture 3

How to delete files and folders using Linux command line Picture 4

Remove an empty or nonempty directory

rmdir -- "empty-folder"
rm -ri -- "old-test-folder"

rmdir stops if the directory still contains files. For a nonempty directory, first inspect it with ls -la -- "old-test-folder" so you can see hidden files too. Use the recursive command only if you intend to remove the folder and its contents.

The older screenshot shows a force option. The example above deliberately asks for confirmation and uses a clearly named local folder.

How to delete files and folders using Linux command line Picture 5

If you expect to recover the item later, use your file manager’s Move to Trash command instead. Avoid adding administrator privileges or broad wildcards just to get past a permission error.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.