Remove files and folders in the Linux terminal

Learn to safely remove files and folders in the Linux terminal.
27 readers like this.
Removing files

CC BY-SA Seth Kenlon

To remove a file on a computer using a graphical interface, you usually drag a file or a folder to a "trash" or "recycle" bin. Alternately, you might be able to select the file or folder you want to remove, right-click, and select Delete.

When removing a file or folder in the terminal, there is no trash bin, at least by default. On a graphical desktop, the Trash is a protected directory so that users don't accidentally trash the Trash, or move it from its default location and lose track of it. The Trash is just a highly managed folder, so you can make your own Trash folder for use in your terminal.

Setting up a trash bin for the terminal

Create a directory called Trash in your home directory:

$ mkdir ~/Trash

Removing a file

When you want to remove a file or folder, use the mv command to move a file or directory to your Trash:

$ mv example.txt ~/Trash

Deleting a file or folder permanently

When you're ready to remove a file or folder from your system permanently, you can use the rm command to erase all of the data in your Trash folder. By directing the rm command to an asterisk (*), you delete all files and folders inside the Trash folder without deleting the Trash folder itself. If you accidentally delete the Trash folder, however, you can just recreate it because directories are easy and free to create.

$ rm --recursive ~/Trash/*

Removing an empty directory

Deleting an empty directory has the special command rmdir, which only removes an empty directory, protecting you from recursive mistakes.

$ mkdir full
$ touch full/file.txt
$ rmdir full
rmdir: failed to remove 'full/': Directory not empty

$ mkdir empty
$ rmdir empty

Better trash

There are commands for trashing files that aren't included by default in your terminal, but that you can install from a software repository. They make it even easier to trash files, because they manage and use the very same Trash folder you use on your desktop.

$ trash ~/example.txt
$ trash --list
$ trash --empty


What to read next
Seth Kenlon
Seth Kenlon is a UNIX geek, free culture advocate, independent multimedia artist, and D&D nerd. He has worked in the film and computing industry, often at the same time.

Comments are closed.

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.