Quick Answer
Warning: removed files do not go to a trash folder on a Linux server. Check where you are and what is inside before you delete anything.
pwd
ls -la old-site
rmdir empty-folder
rm -ri old-site
pwd prints the folder you are in, and ls -la shows what the directory contains, hidden files included. rmdir removes a folder only when it is empty. rm -ri removes a folder and its contents and asks before each file and folder.
Check the Path First
- Run
pwd. A relative path such asold-sitemeans a different folder depending on where you are. - Run
ls -laon the exact path you will delete, and read the list. - Prefer full paths, such as
/var/www/old-site, over relative ones. - For anything you might need again, take a snapshot or copy the folder elsewhere first. See snapshot vs backup.
Remove an Empty Directory with rmdir
rmdir uploads-old
rmdir -p a/b/c
rmdir refuses to delete a folder that still has something in it, which makes it the safe choice. -p also removes the parent folders if they become empty.
Remove a Directory and Its Contents
Before this step, repeat the check above: rm -r deletes the folder, every file inside, and every folder below it.
rm -ri /var/www/old-site
rm -I -r /var/www/old-site
rm -r /var/www/old-site
-i: asks before each removal. Good for small folders.-I: asks once before a recursive removal or before removing more than three files. A sensible minimum for large folders.-v: prints each file as it is removed.
What rm -rf Does
-f (force) removes write-protected files without asking, ignores paths that do not exist, and cancels an -i given before it (of the two, the later option wins). That makes rm -rf silent: one wrong character in the path and it deletes the wrong tree without a question. Use it only after ls shows the exact path, and only when the folder contains write-protected files that you have checked.
ls -la /var/www/old-site
rm -rf /var/www/old-site
rm refuses to work recursively on / itself, but that protection covers nothing else: /etc, /var, or your home folder are deleted like any other path. In scripts, never build the path from a variable that can be empty, and quote every path.
Remove the Contents but Keep the Folder
find /var/www/cache -mindepth 1 -print
find /var/www/cache -mindepth 1 -delete
-mindepth 1 leaves the folder itself in place. Read the output of the first command before you run the second. A pattern such as cache/* would miss hidden files. The find command guide explains -print and -delete in more detail.
Fix Common Errors
- Directory not empty: rmdir found files.
ls -Ashows hidden ones you may have missed. Userm -rif you do want the contents gone. - Is a directory: plain
rmdoes not remove folders; add-r. - Permission denied: deleting needs write permission on the parent folder, not on the file. Check the owner with
ls -ldand usesudoif the folder belongs to another user. - Operation not permitted: even as root, a file with the immutable attribute cannot be deleted.
lsattrshows aniflag; find out who set it before you remove it withsudo chattr -i. - Device or resource busy: the folder is usually a mount point.
findmntshows it. Note thatrm -rgoes into mounted file systems and deletes their files before it fails on the mount point; check withfindmntbefore a recursive delete, or add--one-file-systemto skip them. - Names with spaces or a leading dash: quote the name (
rm -r "old site"), or use--to end the options:rm -r -- -tmp.
If a large deletion does not show up as disk space afterward, a running process may still hold files open; how to check disk space in Linux shows how to find it.