How To Remove Directory in Linux? Delete Folder and Files
By Aslam | January 19, 2023
When dealing with Linux, it is important to know how to remove directory in Linux along with files. Learn how to use the rm Command in Linux to remove files and folder from the command line in this guide.
rm – remove files or directories. If the -I or –interactive=once option is given, and there are more than three files or the -r, -R, or –recursive is given, then rm prompts the user for whether to proceed with the entire operation. If the response is not affirmative, the entire command is aborted.
Otherwise, if a file is unwritable, standard input is a terminal, and the -f or –force option is not given, or the -i or –interactive=always option is given, rm prompts the user for whether to remove the file. If the response is not affirmative, the file is skipped.
Command and option | Explanation |
rm | Remove files and directories using the rm command |
-f, –force | If we use –f, then it will ignore the nonexistent files and arguments. Also, it will not ask for any confirmation while processing, It will execute the command as it is. |
-i | It will ask for confirmation before each and every removal of a file or folder. |
-I | It will prompt only once even, if you are removing multiple files or when removing recursively and less intrusive than –i. |
–interactive[=WHEN] | prompt according to WHEN: never, once (-I), or always (-i); without WHEN, prompt always |
-r, -R, –recursive | It will remove directories and their contents recursively. |
-d, –dir
| Used to remove the empty directories |
-v, –verbose
| It will print the output removal process by default, rm does not remove directories. Use the –recursive (-r or -R) option to remove each listed directory, too, along with all of its contents. |
- -f, –force
If we use –f, then it will ignore the nonexistent files and arguments. Also, it will not ask for any confirmation while processing, It will execute the command as it is.
- -i
It will ask for confirmation before each and every removal of a file or folder.
- -I
It will prompt only once even, if you are removing multiple files or when removing recursively and less intrusive than –i.
- –interactive[=WHEN]
prompt according to WHEN: never, once (-I), or always (-i); without WHEN prompt always
- -r, -R, –recursive
It will remove directories and their contents recursively
- -d, –dir
Used to remove the empty directories
- -v, –verbose
It will print the output removal process by default, rm does not remove directories. Use the –recursive (-r or -R) option to remove each listed directory, too, along with all of its contents.
ALSO READ: What is LAMP Stack and How Does it Work?
Linux Files Category:
In Linux files are mainly categorized into three parts,
- Regular files
- Directory files
- Special files
The easiest way to find out file type in any operating system is by looking at its extension such as .txt, .sh, .py, etc.
Make sure that you remove the correct folder. In Linux, once removed, you will not able to revert the process. The table below provides a quick overview of the rm command,
Command and option | Explanation |
rm a* | * indicates multiple characters, using wildcard matching to remove files in bulk. The comment removes all files starting with the letter “a”. |
rm *.jpeg | For example, all .jpeg image files in this current working directory will be deleted when running the command. |
rm *.? | The question mark(?) symbol represent a single character which means you can remove the files that have single character extension like .O, .C, and .S. This command would delete all files that have a single-character extension. |
rm –i filename | The -i option to the rm command prompts confirmation before attempting to erase each file. |
rm -r sample | To delete the file named “sample” which is in the subdirectory folder, You will be asked to approve the deletion if the directory or any files contained inside it is write-protected. Use the -f option to delete a directory without being prompted. |
rm –rf sample | Use the rm command and the names of the directories separated by spaces to delete several directories at once. |
rm –r /directory/subdirectory/* | The symbol * will remove all files present inside that directory. |
rmdir directoryname | The command rmdir can be used to remove empty directories. |
ALSO READ: What is Linux and Easy Linux Commands to Learn
Regular files:
Regular files contain all readable files such as text files, docx files, programming files, Binary files, image files such as JPG, PNG, SVG, etc, and compressed files such as ZIP, RAR, etc.
rm command in linux:
To delete a single file in Linux. Example file name – “cyber.txt”. You can use the below command,
#rm cyber.txt
To delete multiple files in Linux, regardless of their extensions named demo.txt, demo.doc, and sample.mp4, will follow as below,
#rm demo.txt demo.doc sample.mp4
To recursively delete all files and directories in Linux, even if it is nonempty
#rm –rf sample
Wildcard symbols to remove directory in linux:
Wildcard symbols are used to delete multiple files with a single command in Linux. You can choose which groups of files to remove by using wildcards. The ? denotes a single character, and the * denotes multiple characters.
Here we have shared some examples to understand easily.
- #rm a*
Using wildcard matching for removing files in bulk. The above command will remove all the files that start with the letter “a”. For example, you are having apple.jpg and allinone.txt files. So using the “rm a*” command, all these files will be deleted under that directory.
- #rm *.jpeg
In this command, the current working directory’s .jpeg image extension files would all be deleted.
- #rm *.?
The question mark(?) symbol represent a single character which means you can remove the files that have single character extension like .O, .C, and .S
This command would delete all files that have a single-character extension.
- #rm –i filename
Pass the -i option to the rm command to prompt confirmation before attempting to erase each file.
Directory files:
The directory files are used to store the file names with their information. This type of file will be in blue color. It contains entries for each file and its sub-directories. For example, if you have 15 files in the directory then the directory has 15 entries. You can use cd command to navigate to each file or directory.
To navigate to the subdirectory in the directory in linux where you want to delete the file
#cd /directory/subdirectory
To delete the file which is in the subdirectory folder in Linux, where the file name is “sample”, use the below command,
#rm -r sample
To approve the deletion if the directory or any files contained inside it is write-protected. Use the -f option to delete a directory without being prompted:
#rm –rf sample
To delete several directories in Linux at once, Use the rm command and the names of the directories separated by spaces. The following command will delete all of the mentioned directories and their contents
#rm -r sample sample1 sample2
To ask for your confirmation before deleting each file and subfolder, you can -i option along with the rm command. This may get a bit tedious if the directory has a lot of files, so you could think about using the -I option, which will only prompt you once before deleting everything.
#rm –ri sample
Type Y and press Enter to delete the directory or press N to cancel the deletion.
If you are not working in the required directory, you can mention the directory path to remove the files like as below,
#rm –r /directory/subdirectory.file.txt
In the above example, the file.txt will be removed from the mentioned directory.
To avoid mistakes you can add i to prompt you to confirm the deletion.
#rm –i /directory/subdirectory.file.txt
To remove all the files in the directory in Linux, you can use the command as below,
#rm–r/directory/subdirectory/*
The symbol * will remove all files present inside that directory.
rmdir command in Linux:
Using rmdir to remove empty Directories
The command rmdir can be used to remove empty directories. It is helpful when you just want to remove a directory if it is empty and doesn’t want to check to see whether it is.
rmdir directory_name
or
rmdir [option] dirtecory_name
To delete directory in Linux, type the command rmdir followed by the name of the directory to delete it. For instance, you would enter the following to remove the directory folder1:
#rmdir folder_1
The following error message will appear if the directory is not empty:
“rmdir: failed to remove ‘folder_1’: Directory not empty”
Otherwise, the mentioned folder_1 will be deleted.
To remove multiple directories in Linux, use the below command,
#rmdir folder_1 folder_2 folder_3
To view the removal process verbose(v) is used. It will print the output removal confirmation.
#rmdir –v folder_1
And the output will be -> rmdir: removing directory, ‘folder_1’
To remove empty subdirectory with the parent directory in Linux
To remove the subdirectory, The “-p” will be used.
#rmdir –p
For example, #rmdir –p /directory/subdirectory
Here it will remove the subdirectory first and after removing the subdirectory, the directory is empty, it will delete the directory also.
To remove all files and folders (non-empty) from a directory in Linux, use the below command,
#rm –rvf /directory
This command will delete all the files in the directory folder
Note:
-r -> Recursively deleting directories with their content.
f -> force to delete without prompt. You have to be cautious while using it.
v -> show what is going on during the removal process
Special files
The special files are also called device files. The special files are block files, Character files, pipe files, symbolic link files, and socket files.
These files are used to communicate with the hardware components.
(Note: Do not remove these files without proper reason)