There are many ways to clean up or remove files, including using commands such as rm (remove), rmdir (remove directory), shred, and unlink. Credit: Pressmaster / Shutterstock 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 oddfile rm: 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-to How to examine files on Linux Linux provides very useful options for viewing file attributes, such as owners and permissions, as well as file content. By Sandra Henry Stocker Oct 24, 2024 6 mins Linux how-to 8 easy ways to reuse commands on Linux Typing the same command again and again can become tiresome. Here are a number of ways you can make repeating commands – or repeating commands but with some changes – a lot easier than you might expect. By Sandra Henry-Stocker Oct 15, 2024 5 mins Linux news SUSE Edge upgrade targets Kubernetes and Linux at the edge SUSE Edge 3.1 includes a new stack validation framework and an image builder tool that are aimed at improving the scalability and manageability of complex Kubernetes and Linux edge-computing deployments. By Sean Michael Kerner Oct 15, 2024 6 mins Edge Computing Linux Network Management Software how-to Lesser-known xargs command is a versatile time saver Boost your Linux command line options and simplify your work with xargs, a handy tool for a number of data manipulation tasks. By Sandra Henry Stocker Oct 11, 2024 6 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe