howtouselinux

2 ways to Delete Files in Linux

Table of Contents

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 10 must-know tips for deleting files in Linux.

We will cover everything from using the rm 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. 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”.

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

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.

1. -i (Interactive Deletion): Like in cp, the -i option makes the command ask the user for confirmation before removing each file, you have to press y for confirming deletion, any other key leaves the file un-deleted.

Example:
$ rm -i howtouselinux.txt
rm: remove regular empty file ‘howtouselinux.txt’? y

2. -f (Force Deletion): rm prompts for confirmation removal if a file is write protected. The -f option overrides this minor protection and removes the file forcefully.

Example:
$ rm -i howtouselinux.txt
rm: remove write-protected regular empty file ‘howtouselinux.txt’? n
$ ls
howtouselinux.txt
$ rm -f howtouselinux.txt
$ ls

3. -r (Recursive Deletion): With -r(or -R) option rm command performs a tree-walk and will delete all the files and sub-directories recursively of the parent directory. At each stage it deletes everything it finds. Normally, rm wouldn’t delete the directories but when used with this option, it will delete.

Example:
Below is the tree of directories and files:
$ ls
B C
$ rm *
rm: cannot remove ‘B’: Is a directory
rm: cannot remove ‘C’: Is a directory
$ rm -r *
$ ls

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.

 

David Cao
David Cao

Hey there! I am David, a Cloud & DevOps Enthusiast and 18 years of experience as a Linux engineer. I work with AWS, Git & GitHub, Linux, Python, Ansible, and Bash. I am a technical blogger and a Software Engineer, enjoy sharing my learning and contributing to open-source.