Skip to Content

2 ways to Delete Files in Linux

In Linux, deleting a file is a pretty simple process. However, there are a few things you need to know in order to delete files safely and securely.

In this blog post, we will discuss  2 ways to delete files in Linux – using rm command and unlink command.

We will cover everything from using the command to deleting files permanently. So, whether you’re a beginner or an experienced Linux user, this post is for you!

Delete files with rm command in Linux

You can quickly delete files in Linux using rm command. Open the terminal and type “rm” followed by the name of the file that you want to delete.

Get Your Free Linux training!

Join our free Linux training and discover the power of open-source technology. Enhance your skills and boost your career! Learn Linux for Free!

For example, if you wanted to delete a file called “file.txt”, you would type “rm file.txt”.

If you want to delete multiple files, use the “rm” command followed by the name of each file, separated by spaces “rm file1.txt file2.txt file3.txt”.

rm command removes each specified file.  By default, it does not remove directories.

Procedure to delete files in Linux

  • Open the terminal.
  • Type rm filename and press Enter to delete a specific file.
  • Type rm -r dirname and press Enter to delete a specific directory and all its contents.
  • Type rm -f filename and press Enter to force delete a file without prompting for confirmation.
  • Type rm -rf dirname and press Enter to force delete a directory and all its contents without prompting for confirmation.

 

Which options can be used in Linux rm command?

The rm command in Linux can be used to delete files and directories. There are a few different options that can be used with the rm command, including -r,-f and -i.

Option Description
-i Interactive Deletion: Prompts for confirmation before removing each file. Requires user confirmation by pressing ‘y’.
-f Force Deletion: Overrides write protection and forcefully removes files without prompting for confirmation.
-r Recursive Deletion: Deletes files and sub-directories recursively, removing everything found within the parent directory.
Description Command
Delete files with confirmation prompt: $ rm -i file1.txt file2.txt
Delete files forcefully without confirmation prompt: $ rm -f file1.txt file2.txt
Delete a directory and its contents recursively: $ rm -r directory/
Delete a directory without confirmation prompt: $ rm -rf directory/

Disadvantages of rm command in Linux

The rm command is very simple and easy to use, but it also has some disadvantages. One disadvantage of the rm command is that it cannot be undone. Once a file has been deleted with the rm command, it is gone forever and can’t be recovered.

Additionally, the rm command can be dangerous if used incorrectly.

Delete directory with rm command in Linux

The rm command can be used to delete folders in Linux. To delete a folder, type “rm -r foldername”. This will delete the folder and all of its contents. Note that this command must be run from the terminal window.

Be careful when using this command because it can permanently delete your files. The rm command can be dangerous if used incorrectly because it can permanently delete your files. Therefore, it is important to be careful when using the rm command.

Delete hidden files with rm command in Linux

We can use the find command to search for and delete hidden files. To do this, type “find . -type f -name ‘.*’ -exec rm {} \;”. This will search for all files with a “.hidden” extension and then delete them.

Note that this command must be run from the terminal window.

Delete files Forcefully in Linux

To remove file or directory forcefully, you can use the option -f force a deletion operation without rm prompting you for confirmation.

$ rm -f howtouselinux.txt

When you combine the -r and -f flags, it means that you recursively and forcibly remove a directory (and its contents) without prompting for confirmation.

Delete files with unlink command in Linux

The unlink command can be used to delete files from a Linux system. The basic syntax for the unlink command is:

unlink FILENAME

This will delete the file named FILENAME from the system. If the file is not found, the command will return an error.

Example of how to use the unlink command

To demonstrate how the unlink command works, let’s create a file named test.txt:

touch test.txt

We can now delete this file using the unlink command:

unlink test.txt

If we try to access the file, we will receive an error:

cat test.txt

Output
cat: test.txt: No such file or directory

It’s important to note that the unlink command does not delete directories, only files. If you attempt to use the unlink command on a directory, you will receive an error.

To delete an empty directory, you can use the rmdir command. The basic syntax for the rmdir command is:

rmdir DIRECTORY

This will delete the directory named DIRECTORY from the system.

If the directory is not empty, the rmdir command will also return an error. In order to delete a non-empty directory, you can use the rm -r command.

The rm -r command will delete a directory and all of its contents. The basic syntax for the rm -r command is:

rm -r DIRECTORY

This will delete the directory named DIRECTORY, as well as all of its contents.

Tips you should know before delete files in Linux

It is important to be careful when using the rm command because it can permanently delete your files. To ensure that you are deleting the correct files, always double-check the name of the file before deleting it.

 Additionally, make sure that you are in the correct directory before running the rm command. Running the rm command from the wrong directory can result in accidental file deletion.

Always be caution when using any type of terminal command. Mistyping a command can have disastrous consequences.

If you’re not sure whether or not a file is safe to delete, you can use the ls -lh command to get more information about the file. This will show you the size and permissions of the file.

If you still aren’t sure whether or not it’s safe to delete, you can ask for help on an online forum or chat room. You can always back up your files with cp command before you delete them.

Unfortunately, once a file has been deleted with the rm command, it is gone forever and cannot be recovered.

One way to ensure that you’re not going to delete any important files by mistake when using the rm or rm -r commands is to use the “rm -i” command. This will prompt you for confirmation before deleting each file.