How to Remove (Delete) Files in Linux

Updated on

5 min read

Delete (Remove) Files and Directories Using Linux Command Line

The command line is a powerful tool that allows you to perform daily tasks more quickly and efficiently than a graphical interface. But, it can also be dangerous if used incorrectly. This is especially true when removing unnecessary files or directories from the system.

Desktop users can use the GUI to easily delete files, and restore those files out of the Trash if needed. However, if you are working on a headless server, the command line utilities allow you to remove multiple files and directories at once using wildcards or other pattern matches.

In Linux, there are several tools that can be used to delete files and directories. Be extra careful when removing files or directories because once the file is deleted it cannot be easily recovered, even with recovery software.

This article goes through several different tools that allow you to remove files and directories in Linux. We will explain how to use the rm, unlink, shred, and rmdir commands.

How to Remove Files in Linux

To remove (or delete) a file in Linux from the command line, you can use rm, shred, or unlink commands.

The unlink command allows you to remove only a single file, while with rm and shred, you can remove multiple files at once.

File names with a space in them must be escaped with a backslash (/).

Using rm command

To delete a single file, invoke the rm or [unlink] command followed by the file name:

rm filename

If you don’t have write permissions on the parent directory, you will get an “Operation not permitted” error.

You will be prompted for confirmation if the file is write-protected, as shown below. To remove the file, type y, and hit Enter. Otherwise, if the file is not write-protected, it will be deleted without prompting.

 
rm: remove write-protected regular empty file 'filename'?

To delete multiple files at once, invoke the rm command followed by the name of the files you want to remove, separated by a space:

rm filename1 filename2 filename3

You can also match multiple files with a wildcard (*) and regular expansions.For example, to remove all .pdf files in the current directory, you would use the following command:

rm *.pdf

When using regular expansions, first list the files with the ls command so that you can see what files will be deleted before running the rm command.

Use the rm with the -i option to confirm each file before deleting it:

rm -i filename(s)

To remove files without prompting, even if the files are write-protected, pass the -f (force) option to the rm command:

rm -f filename(s)
  • You can also combine the rm command options. For example, to remove all .txt files in the current directory without a prompt in verbose mode, use the following command:
rm -fv *.txt

Using shred command

When a file is removed in Linux, the actual data is not deleted; only the reference inode (metadata about a file) is removed, and the corresponding data blocks are marked as available for reuse. The content of the file will stay on the disk until the space is needed for new data.

The shred overwrites a file to hide its contents, thus making it more difficult or impossible to recover the deleted file. Use this command to protect your privacy and ensure no one can restore your removed files.

To overwrite and delete a file with shred, invoke the command with the -u option followed by the file name:

shred -u filename

The file will be overwritten and then truncated and removed from the system.

You can also pass multiple files separated by a space:

shred -u filename1 filename2 filename3

To remove a file with unlink, invole the command foolowed bythe name of the file you want to remove:

unlink -u filename

On success, the command doesn’t produce any output and returns zero.

How to Remove Directories (Folders)

In Linux, you can remove/delete directories with the rmdir and rm.

rmdir is a command-line utility for deleting empty directories, while with rm, you can remove directories and their contents recursively.

Using rmdir command

rmdir is a command-line utility used to delete empty directories. It comes in handy when you want to delete a directory only if it is empty, without checking whether the directory is empty or not.

To remove an empty directory, use the rmdir command with the directory name as argument:

rmdir dirname

If the directory is not empty, you will get an error message.

Using rm command

By default, when used without any option, rm cannot remove directories.

To remove an empty directory, with rm, invoke the command with the -d option:

rm -d dirname

To remove non-empty directories and all the files within them, use the rm command with the-r (recursive) option:

rm -r dirname

If a directory or a file within the directory is write-protected, you will be prompted to confirm the deletion.

To remove non-empty directories and all the files without being prompted, use rm with the -r (recursive) and -f options:

rm -rf dirname

To remove multiple directories at once, use the rm -r command followed by the directory names separated by space.

rm -r dirname1 dirname2 dirname3

As with files, you can also use a wildcard (*) and regular expansions to match multiple directories.

Conclusion

By now, you should have a good understanding of how to use the Linux rm, rmdir and, unlink commands, and you should be able to safely remove files and directories using the command line.

Feel free to leave a comment if you have any questions.