To delete a file, files, or a folder in Linux, the Linux command to use is either
rmdir. The rm command stands for “remove” and is used to delete files and directories. By using various options, you can remove files, directories, and even the contents of directories. For example,
rm filename will delete a file, while
rm -r directoryname will delete a directory and all its contents.
rmdir command stands for “remove directory” and is specifically used to delete empty directories. If a directory contains any files or subdirectories, the
rmdir command will not remove it, and an error message will be displayed. In contrast, the
rm command with the
-r option can delete non-empty directories. Essentially,
rm is more versatile, while
rmdir is more specialized for removing empty directories.
In this post, we look at different ways to use
How to use
1. Delete a file
rm command removes a single file. To do this, type
rm followed by the name of the file you want to delete.
The following command is used to remove a specific file named
example.txt located within a directory named
Here’s an example of how it works:
- Suppose you have a directory named
myfolderand inside that directory, there’s a file named
- You run the command
- The file
myfolderwill be deleted, and there will be no output message displayed in the terminal by default.
- If you try to access the file
example.txtagain, you’ll get an error message like
No such file or directory.
Before running the command, if you had:
myfolder/ └── example.txt
After running the command, the structure would be:
2. Remove files without confirmation
rm -f [file]
This option allows users to remove write-protected files without confirmation.
Suppose you have a file named
file1.txt in your current directory, and you want to delete it. You can run the following command:
rm -f file1.txt
-f option is used, there will be no confirmation prompt, and the file will be deleted immediately. There will be no output displayed in the terminal if the operation is successful. If you try to view the contents of the directory afterward, you will find that
file1.txt is no longer there.
3. Delete multiple files
rm [file1] [file2] [file3]
rm command with filenames as arguments to remove multiple files at once.
Before running the command, let’s say you have the following files in your directory:
file1.txt file2.txt file3.txt otherfile.txt
You run the command
rm file1.txt file2.txt file3.txt.
After running the command, the files
file3.txt are deleted, and your directory now looks like this:
4. Display output message
rm -v [filename]
-v (verbose) option allows you to get information about what is being removed.
When you execute the command
rm -v example.txt, here’s the output you will get:
In this example, the file
example.txt is deleted, and the system prints a message confirming that the file has been removed. If the file does not exist, an error message like
rm: cannot remove 'example.txt': No such file or directory would be displayed instead.
5. Prompt for confirmation before deleting a file
rm -i [filename]
This option is used to request confirmation before deleting a file. Typing
y (yes) confirms, typing
n (no) stops.
Let’s say you have a file named
example.txt and you run the command:
rm -i example.txt
The system will prompt you with a message like:
remove regular file 'example.txt'?
You’ll then need to type
y (for yes) or
n (for no) to confirm or deny the deletion. If you type
y and press Enter, the file
example.txt will be deleted. If you type
n, the file will remain untouched.
remove regular file 'example.txt'? y
example.txt will be deleted if you confirmed with
How to use
This command removes directory as well as files within the directory. There isn’t significant difference with the
rm -r command except that it can not be used to remove a file.
General syntax for
$ rmdir [OPTION...] [DIRECTORY...]
1. Remove a directory
Use this command to remove a directory, but it will only be removed if it is empty.
Suppose you have a directory named
myfolder and it’s empty. When you run the command:
myfolder will be deleted, and there will be no output message if the operation is successful.
myfolder is not empty or does not exist, you will receive an error message. For example, if
myfolder contains files or subdirectories, you might see:
rmdir: failed to remove 'myfolder': Directory not empty
myfolder does not exist:
rmdir: failed to remove 'myfolder': No such file or directory
2. Delete multiple directories
rmdir [folder1] [folder2] [folder3]
This command allows you to delete several directories at once, but they all must be empty.
rmdir folder1 folder2 folder3 command in Linux attempts to remove the directories named
folder3. This command will only succeed if all three of these directories are empty; otherwise, an error message will be displayed.
Here’s an example of how this might work:
folder3 are all empty directories, the command will remove them, and there will be no output message.
If any of these directories are not empty or do not exist, an error message will be displayed for each problematic directory.
Example of the output if
folder2 is not empty:
rmdir: failed to remove 'folder2': Directory not empty
In this case,
folder3 would still be removed if they were empty, but
folder2 would remain untouched.
3. Remove parent directories
rmdir -p [directory-path]
-p option removes the specified directory and its parent directories.
Here’s an example of how the command
rmdir -p folder_a/folder_b would work:
Suppose you have a directory structure like this:
folder_a └── folder_b
folder_b are empty, running the command
rmdir -p folder_a/folder_b will remove
folder_b first, and then, since
folder_a becomes empty, it will remove
folder_a as well.
If there were any files or subdirectories inside
folder_b, the command would not remove them, and you would receive an error message like:
rmdir: failed to remove 'folder_a/folder_b': Directory not empty
In the successful case, there would be no output, and both directories would be deleted.
More Linux commands:
|File System Operations||
|Search and Text Processing||
|System Information and Management||
|User and Session Management||