- How to Remove (Delete) a File or Directory in Linux
- How To Remove or Delete Linux Files
- rm Command Options
- How to Delete a Directory in Linux
- Remove Directory Linux with rm Command
- Remove Directories in Linux with rmdir Command
- How to Delete Files and Directories in Linux?
- 1. Remove file by using “unlink”:
- 2. Delete a single file:
- 3. Multiple files can be deleted:
- 4. Delete the archive:
- 5. Locate and delete files:
- 6. Empty files should be found and deleted:
- 7. Permissions are used to locate and delete files:
- How to Remove a Directory in Linux
- How to Remove a Directory in Linux?
- rm Command
- rmdir Command
How to Remove (Delete) a File or Directory in Linux
Home » SysAdmin » How to Remove (Delete) a File or Directory in Linux
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.
- 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:
The rm command can be used to delete more than one file at a time:
Wildcards can be used with this command.
For example, to delete all files with the .bmp filename, enter:
This method is also used to delete all files that contain a string of characters:
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:
Or you can specify the file location in a single command directly:
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:
Confirm the deletion of files by typing ‘yes’ or ‘no.’
To display the progress of the deletion with the v or verbose command:
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:
To use sudo privileges for a file that says Access denied and delete it:
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:
This will prompt you for confirmation before deleting.
To remove a directory without confirmation:
Also, you can delete more than one directory or folder at a time:
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:
Additionally, you can delete multiple empty directories at once by typing:
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.
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 Delete Files and Directories in Linux?
Linux comes with several tools that can assist us in removing files. We always need to delete many files and folders based on a set of requirements. To complete our mission quickly, knowing a few basic commands and their variations is beneficial.
- Use caution when using the commands below, particularly those that use regular expressions or search patterns with the find command. An incorrect expression or pattern will result in the deletion of important data/system files and non-intended files.
- Often have a current copy of critical data and device files.
- Use caution when running those commands, particularly if you’re using Sudo or as the superuser (root).
1. Remove file by using “unlink”:
Not so well-liked. We may use the unlink command to permanently delete a single file.
2. Delete a single file:
The rm command, which facilitates deleting one or more files simultaneously, is a more widely used command for removing files.
If the file is write-protected, rm will ask you to validate its deletion; otherwise, it will delete it without prompting. Using the “-i” flag to force rm to prompt for confirmation before removing a file:
The rm command deletes files without showing any messages. Using the rm command with the -v flag to see what the rm command is currently doing.
Using the -f flag to remove write-protected files without asking for clarification.
3. Multiple files can be deleted:
Bypassing multiple filenames as arguments to rm, you can delete multiple files.
Regular expressions are also supported by rm. If you want to delete all files with the name file-name-*, type:
Regular expressions may also be used to define different directories. We can use something like to delete three files that fit file-name-1, file-name-2, and file-name-3.
4. Delete the archive:
The rm command with the -d flag can be used to remove an empty directory.
Supported options for file deletion can also be combined with deleting the directory with the -d flag.
Using the -r flag to deleting a non-empty directory.
If you do not want a prompt before deleting the directory and its contents, use the -rf flag. This will remove everything inside the directory, including the directory itself, without any confirmation. Use caution especially when using as a root.
5. Locate and delete files:
We can use the locate command with various choices for more complicated specifications. To delete all files in a path specified by that follow a pattern .
Example:
We may slightly change the above command to delete everything that fits the sequence
Internally, modern implementations of the find command support the delete feature. The -delete flag is used to override the rm instruction, while the –depth flag tells find to process the contents of the directory before the directory itself:
6. Empty files should be found and deleted:
You may use the following command to remove all empty directories within a given path dir-to-search:
Instead, use the following command to remove all empty files within a given path dir-to-search:
7. Permissions are used to locate and delete files:
We can now remove files based on special permissions, such as:
Consider the following scenario:
Easy (unlink), (rm), and (rmdir) commands are available in Linux, and they can be quickly expanded with regular expressions. For more specialized needs, you should use a variety of techniques such as (find) to accomplish your goals. Aside from the examples in this post, you can configure your quest by using find with any of the available flags.
Often run find commands without the rm or -delete flags and examine the output to determine which files or folders may be affected by the execution of a program. Backup setup and procedure are beneficial not just in the event of unintentional deletions, but also in the event of hardware errors and cyber-attacks.
How to Remove a Directory in Linux
Home » SysAdmin » How to Remove a Directory in Linux
Removing a directory in Linux is a pretty simple task if you are using the GUI. However, if you don’t have access to the GUI, you can also remove directories using terminal commands.
In this tutorial, we will show you how to remove a directory in Linux via commands in the terminal window or command line.
- A system running a Linux distribution.
- An account with sudo privileges.
- Access to the terminal window or command line.
Note: For other Linux directory management articles, see How to Move Directories in Linux and How to Rename a Directory in Linux.
How to Remove a Directory in Linux?
There are two Linux commands you can use to remove a directory from the terminal window or command line:
- The rm command removes complete directories, including subdirectories and files.
- The rmdir command removes empty directories.
It is important to note that the rm and rmdir commands permanently remove directories without moving them to the Trash directory. This means that you cannot restore a directory removed using these commands.
Note: Even though rm and rmdir permanently remove files and directories, users with enough skill and time still have a chance of restoring some of the removed files. If you want to learn more about removing files permanently, have a look at our shred command tutorial.
rm Command
The rm command in Linux removes files and directories.
It uses the following syntax:
Note: To remove multiple files or directories using the rm command, add multiple file or directory names, separated by blank spaces.
The different rm command options include:
- — f : Forces the removal of all files or directories.
- -i : Prompts for confirmation before removing.
- -I : Prompts once before removing more than three files or when removing recursively.
- -r : Removes directories and their content recursively.
- -d : Removes empty directories.
- -v : Provides a verbose output.
- —help : Displays the help text.
- —version : Displays the command version.
Trying to use the rm command without any options to remove a directory results in an error message:
If you want to remove an empty directory, add the -d flag to the rm command:
Note: If you want to remove a directory whose name starts with a hyphen (—), use the rm — [directory name] or rm ./[directory name] syntax.
The example below shows that the rm command with the — d flag removes the Example directory:
Use the -r flag to delete a directory that contains subdirectories and files.
The image below shows the tree hierarchy of the Example directory, which contains Dir1 and Dir2 subdirectories, with multiple text files in each:
Using the -r flag removes the entire directory, including subdirectories and files, while the -v flag lists each step of the process as the output:
The -i option displays a prompt asking you to confirm directory removal. Type Y and press Enter to confirm.
Write-protected directories require user input when deleting. Create such a directory with:
To remove the directory, use:
Type Y and press Enter to confirm deletion. To avoid the confirmation, use the -f flag or elevate the command privileges to sudo:
If the write-protected directory contains other files and directories, use the following command:
Note: rm -rf / is a dangerous Linux command that forces a recursive deletion on the root directory, rendering your system unusable.
Avoid using -f and sudo while removing directories, unless you know what you’re doing. To learn more about this command, visit our article What Is sudo rm -rf in Linux and Is It Dangerous.
rmdir Command
The Linux rmdir command removes empty directories only. The command uses the following syntax:
The rmdir command includes the following options:
- —ignore-fail-on-non-empty : Doesn’t show an error message when trying to remove a non-empty directory.
- -p : Removes the directory along with its parent in the hierarchy.
- -v : Provides a verbose output.
- —help : Displays help text.
- —version : Displays the command version.
Using the rmdir command on a non-empty directory produces an error:
In this case, the Example directory contains the Test subdirectory:
To remove these directories using the rmdir command, add them in reverse order of hierarchy. Using the -v option lists each step of the process as the output:
A simpler method of doing this is to use the -p option with the subdirectory’s name. This removes both the subdirectory and its hierarchical parent:
The rmdir command allows you to remove multiple directories with similar names using wildcards. For instance, if you want to remove directories named Example1, Example2, and Example3:
After reading this tutorial, you should be able to remove directories in Linux using commands in the terminal window or command line.
To learn more about other commands in Linux, check out our Linux commands cheat sheet.