Introduction
Removing files and directories in Linux is a common task that is often done through the command line. It is important to understand the different commands and options available for deleting files and directories in order to ensure that you are deleting the correct files and directories. This guide will provide an overview of the different commands and options available for deleting files and directories in Linux.
How to Remove (Delete) a File or Directory in Linux
1. To remove a file, use the rm command followed by the file name:
$ rm filename
2. To remove a directory, use the rmdir command followed by the directory name:
$ rmdir directoryname
3. To remove a directory and all its contents, use the rm -r command followed by the directory name:
$ rm -r directoryname
Introduction
How do I delete a file in Linux using the command line option? How can I remove a Linux directory?
Deleting files and directories is a necessary task when working with Linux. In this guide, learn how to remove files and directories from the command line in Linux using the RM Command.
Prerequisites
- A command line / terminal window (Ctrl-Alt-T or Ctrl-Alt-F2)
- A user account with sudo privileges (optional)
Note: If you feel that a directory is misplaced and you do not want to remove it, try moving it to a different place. To learn how, visit our post How to Move Directories in Linux.
How To Remove or Delete Linux Files
The rm
command deletes files in a Linux. The command unlinks the data from the file name, allowing the user to overwrite on that particular storage space.
To delete a single file, entering the following in the command line:
rm filename
The rm command can be used to delete more than one file at a time:
rm filename_1 filename_2 filename_3
Wildcards can be used with this command.
For example, to delete all files with the .bmp filename, enter:
rm *.bmp
This method is also used to delete all files that contain a string of characters:
rm *sample*.*
This will erase any file that has the word sample in the name.
The system will search the current directory for the file you want to remove.
To delete a file in a different directory, either switch to that directory first:
cd /tmp
rm filename
Or you can specify the file location in a single command directly:
rm /tmp/filename
Note: Once the rm
command has deleted a file, you will not be able to access it. The only way to retrieve a file would be to restore it from a backup (if one is available).
rm Command Options
You can adjust the way the rm command works by adding options. An option is a hyphen, followed by one or more letters that stand for commands.
If you’re deleting multiple files, add a confirmation prompt. Use the –i
option to use an interactive dialog:
rm –i *.key
Confirm the deletion of files by typing ‘yes’ or ‘no.’
To display the progress of the deletion with the v
or verbose
command:
rm –v *.txt
The output confirms that the file test.txt has been successfully removed.
To force the removal of a file that’s write-protected, use the –f
option:
rm –f filename
To use sudo privileges for a file that says Access denied and delete it:
sudo rm filename
Note: Read about sudo rm -rf and why it is a dangerous Linux command.
How to Delete a Directory in Linux
A linux directory (or folder) can be empty, or it can contain files. To remove a directory in Linux, use one of the following two commands:
- rmdir command – removes empty directories/folders
- rm command – removes a directory/folder along with all the files and sub-directories in it
Remove Directory Linux with rm Command
By adding the -r (-R) option to the rm
command, you can remove a directory along with all its contents.
To remove a directory (and everything inside of it) use the –r
option as in the command:
rm –r dir_name
This will prompt you for confirmation before deleting.
To remove a directory without confirmation:
rm –rf directory
Also, you can delete more than one directory or folder at a time:
rm –r dir_name1 dir_name2 dir_name3
Remove Directories in Linux with rmdir Command
Remember, the rmdir
command is used only when deleting empty folders and directories in Linux. If a specified directory is not empty, the output displays an error.
The basic syntax used for removing empty Linux folders/directories is:
rmdir [dir_name]
Additionally, you can delete multiple empty directories at once by typing:
rmdir [dir_name1][dir_name2][dir_name3]
If the command finds content in one of the listed directories, it will skip it and move on to the next one.
Note: To permanently delete a file in Linux by overwriting it, use the shred command.
Conclusion
With this tutorial, deleting files and directories in Linux was made easy. The rm and rmdir commands are flexible with many options available.
How to Remove (Delete) a File or Directory in Linux
Removing files or directories in Linux is a common task. Whether you are a system administrator or a regular user, you may need to delete files or directories from time to time. In this article, we will explain how to delete files and directories in Linux using the command line.
Deleting Files
To delete a file in Linux, use the rm
command. This command will delete the file specified as an argument. For example, to delete a file named myfile.txt
, you would use the following command:
rm myfile.txt
If the file is write-protected, you will be prompted to confirm the deletion. To bypass this prompt, use the -f
option:
rm -f myfile.txt
You can also delete multiple files at once by specifying multiple filenames as arguments:
rm file1.txt file2.txt file3.txt
Deleting Directories
To delete a directory in Linux, use the rmdir
command. This command will delete the directory specified as an argument. For example, to delete a directory named mydir
, you would use the following command:
rmdir mydir
If the directory is not empty, you will get an error. To delete a directory and all of its contents, use the rm
command with the -r
option:
rm -r mydir
You can also delete multiple directories at once by specifying multiple directory names as arguments:
rm -r dir1 dir2 dir3
Conclusion
In this article, we have explained how to delete files and directories in Linux using the command line. We hope you have found this information useful.