There are many ways to clean up or remove files, including using commands such as rm (remove), rmdir (remove directory), shred, and unlink.
The rm (remove) command is the most obvious way to remove files that you no longer need on your Linux system, and it includes some very useful options. There are a number of other options, too, however. This post covers many ways to clean up or remove files.
The rm command
The rm command simply removes a file and is one of the basic commands that just about every Linux user needs on a routine basis. There are complications, however. Where the file resides makes a difference, for example. You need proper access to both the file and the directory to remove files that it contains.
If you don’t have write permission to a file that is in your home directory (this is very unusual), you will likely be prompted for confirmation that you want to remove it. If you’re not the owner of the file, you can still remove it even if you have no permissions to the file at all. Here’s an example of a removing a file in one’s home directory:
$ ls -l oddfile-rw-------. 1 root root 100 Jul 9 15:52 oddfile$ rm oddfilerm: remove write-protected regular file 'oddfile'? y
It’s important to understand that when you remove a file, you are also modifying the directory, since it contains a reference to the file. This can sometimes be an issue. If you lack proper permissions to the directory, but you have the ability to use the sudo command to give yourself root authority, you can always get around permissions issues with a command like this:
$ sudo rm oddfile
Removing files with names containing blanks
Files with blanks in their names should be removed by enclosing their names in single quotes or double quotes or by escaping the blanks with backslashes like this:
$ rm some\ old\ file
Using wildcards
You can delete a group of files using wildcards. For example, if you enter the command “rm *.pdf”, you will remove every file with the .pdf extension. If you remove files using a command like “rm *q*, you would remove all files containing the letter “q” in their names regardless of its position.
Using the rmdir command
The rmdir command will remove a directory, but only if it’s empty. To remove the directory and its contents, you need to use a recursive command like that shown below.
Recursive rm
The rm command can be used with an option that allows you to remove an entire directory with a single command. This assumes, of course, that the files are all yours. The rm -r command will run through a directory, remove its contents, and then remove the directory itself.
Using the shred command
Removing a file from a directory does not wipe its contents from the disk even thought the file will no longer be visible or available. That disk space will in time likely be overwritten with other data. However, if you’re removing a file that contains sensitive information, it’s a good idea to use the shred command prior to removing it. That way, the content will have been left in an unrecoverable form.
Using >
Using a command like > myfile will empty a file without removing it from the system. You will then be able to fill it with new content. The cat /dev/null > myfile command will do the same thing, but using just > is easier.
You can also use > to add the output from a command to a file. This will overwrite any prior contents, so use >> if you want to add to the file instead.
$ > oldfile$ date >> oldfile
Using unlink
The unlink command will only remove a single file. Unlinked files cannot be fully recovered. This command accepts two options, but only –help and –version. Both are outlined in the man page.
--help display this help and exit --version output version information and exit
If you try to remove a symbolic link with the unlink command, the link is removed, but the file that it points to will remain. In other words, this works the same as with the rm command.
As with the rm command, you can’t remove a symbolic link with unlink unless you have write permission to the directory that contains the link. You will get a “permission denied” error if you try.
Being careful
Before you run the command, it’s a good idea to first list the files that you intend to delete to make sure that the list doesn’t include files you might still need.
Related posts
- Shredding files on Linux with the shred command
- Recovering deleted files on Linux with testdisk
- The Linux command-line cheat sheet
- Creating and removing directory structures on Linux
Related content
- how-toDisplaying dates and times on Linux Linux has a lot of options for displaying dates and times, even for past and future dates. This post explains many of these options and provides examples of what you can expect to see.By Sandra Henry StockerJul 15, 20247 minsLinux
- how-toHow to find files on Linux There are many options you can use to find files on Linux, including searching by file name (or partial name), age, owner, group, size, type and inode number.By Sandra Henry StockerJun 24, 20248 minsLinux
- opinionLinux in your car: Red Hat’s milestone collaboration with exida With contributions from Red Hat and critical collaborators, the safety and security of automotive vehicles has reached a new level of reliability.By Sandra Henry StockerJun 17, 20245 minsLinux
- PODCASTS
- VIDEOS
- RESOURCES
- EVENTS
NEWSLETTERS
Newsletter Promo Module Test
Description for newsletter promo module.