- 2 ways to Delete Files in Linux
- Delete files with rm command in Linux
- Procedure to delete files in Linux
- Delete directory with rm command in Linux
- Delete hidden files with rm command in Linux
- Delete files Forcefully in Linux
- Delete files with unlink command in Linux
- Tips you should know before delete files in Linux
- How to Remove Files and Directories in Linux Command Line [Beginner’s Tutorial]
- How to delete files in Linux
- 1. Delete a single file
- 2. Force delete a file
- 3. Remove multiple files
- 4. Remove files interactively
- How to remove directories in Linux
- 1. Remove an empty directory
- 2. Remove directory with content
- 3. Force remove a directory and its content
- 4. Remove multiple directories
- Summary
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.
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.
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:
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:
We can now delete this file using the unlink command:
If we try to access the file, we will receive an error:
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:
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:
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 is a Cloud & DevOps Enthusiast. He has years of experience as a Linux engineer. He had working experience in AMD, EMC. He likes Linux, Python, bash, and more. He is a technical blogger and a Software Engineer. He enjoys sharing his learning and contributing to open-source.
howtouselinux.com is dedicated to providing comprehensive information on using Linux.
We hope you find our site helpful and informative.
How to Remove Files and Directories in Linux Command Line [Beginner’s Tutorial]
Learn how to delete files and remove directories with rm command in Linux.
How to delete a file in Linux? How to delete a directory in Linux? Let’s see how to do both of these tasks with one magical command called rm.
How to delete files in Linux
Let me show you various cases of removing files.
1. Delete a single file
If you want to remove a single file, simply use the rm command with the file name. You may need to add the path if the file is not in your current directory.
If the file is write protected i.e. you don’t have write permission to the file, you’ll be asked to confirm the deletion of the write-protected file.
rm: remove write-protected regular file 'file.txt'?
You can type yes or y and press enter key to confirm the deletion. Read this article to know more about Linux file permissions.
2. Force delete a file
If you want to remove files without any prompts (like the one you saw above), you can use the force removal option -f.
3. Remove multiple files
To remove multiple files at once, you can provide all the filenames.
rm file1.txt file2.txt file3.txt
You can also use wildcard (*) and regex instead of providing all the files individually to the rm command. For example, if you want to remove all the files ending in .hpp in the current directory, you can use rm command in the following way:
4. Remove files interactively
Of course, removing all the matching files at once could be a risky business. This is why rm command has the interactive mode. You can use the interactive mode with the option -i.
It will ask for confirmation for each of the file. You can enter y to delete the file and n for skipping the deletion.
rm: remove regular file 'file1.txt'? y rm: remove regular file 'file2.txt'? n
You just learned to delete files in the terminal. Let’s see how to remove directories in Linux.
How to remove directories in Linux
There is a command called rmdir which is short for remove directory. However, this rmdir command can only be used for deleting empty directories.
If you try to delete a non-empty directory with rmdir, you’ll see an error message:
rmdir: failed to remove 'dir': Directory not empty
There is no rmdir force. You cannot force rmdir to delete non-empty directory.
This is why I am going to use the same rm command to delete folders as well. Remembering the rm command is a lot more useful than rmdir which in my opinion is not worth the trouble.
1. Remove an empty directory
To remove an empty directory, you can use the -d option. This is equivalent to the rmdir command and helps you ensure that the directory is empty before deleting it.
2. Remove directory with content
To remove directory with contents, you can use the recursive option with rm command.
This will delete all the contents of the directory including its sub-directories. If there are write-protected files and directories, you’ll be asked to confirm the deletion.
3. Force remove a directory and its content
If you want to avoid the confirmation prompt, you can force delete.
4. Remove multiple directories
You can also delete multiple directories at once with rm command.
Awesome! So now you know how to remove directory in Linux terminal.
Summary
Here’s a summary of the rm command and its usage for a quick reference.
Purpose | Command |
---|---|
Delete a single file | rm filename |
Delete multiple files | rm file1 file2 file3 |
Force remove files | rm -f file1 file2 file3 |
Remove files interactively | rm -i *.txt |
Remove an empty directory | rm -d dir |
Remove a directory with its contents | rm -r dir |
Remove multiple directories | rm -r dir1 dir 2 dir3 |
I hope you like this tutorial and learned to delete files and remove directories in Linux command line. If you have any questions or suggestions, please leave a comment below.