This tutorial is about How to Remove Directory in Linux. Recently I updated this tutorial and will try my best so that you understand this guide. I hope you guys like this blog, How to Remove Directory in Linux. If your answer is yes after reading the article, please share this article with your friends and family to support us.
Check How to Remove Directory in Linux
Linux is a popular open source operating system, the features of which are often available in your development environment. Learning the basic commands will make your life as a developer easier. Everything is a file on Linux, including the directory. A directory is nothing more than a group of files. You can use the following commands to delete the directory on Linux. The directory is also known as a folder in Macintosh OS X / macOS and Microsoft Windows operating systems.
How to delete files
To delete (or delete) a file in Linux from the command line, use the rm (delete) or unlink command. The unlink command allows you to delete only one file, while with rm, you can delete multiple files at once.
Be very careful when deleting files or directories, because once the file is deleted, it cannot be easily recovered.
- To delete a single file, use the rm or unlink command followed by the file name:
unlink filename Copy filename
If the file is write-protected, you will be prompted for confirmation, as shown below. To delete the file, type y and hit Enter. Otherwise, if the file is not write-protected, it will be deleted without asking.
rm: delete write-protected regular empty file ‘filename’?
- To delete multiple files at once, use the rm command followed by the file names separated by a space. -Rm filename1 filename2 filename3
- You can also use a wildcard
- and regular expansions to match multiple files. For example, to remove all .pdf files in the current directory, use the following command: rm * .pdf
- When using regular expansions, first list the files with the ls command so that you can see which files will be removed before running the rm command.
- Use rm with the -i option to commit each file before deleting: rm -i filename (s)
- To delete files without prompting, even if the files are write-protected, pass the -f (force) option to the rm command: rm -f filename (s)
You can also combine rm options. For example, to delete all .txt files in the current directory without a message in verbose mode, use the following command: rm -fv * .txt
How to delete directories (folders) #
On Linux, you can remove / remove directories with rmdir and rm.
- rmdir is a command line utility for removing empty directories, while with rm you can remove directories and their contents recursively.
- To remove an empty directory, use rmdir or rm -d followed by the directory name:
- rm -d directoryname Copyrmdirectory
- To remove non-empty directories and all files within them, use the rm command with the -r (recursive) option: rm -r dirname
- If a directory or a file within the directory is write-protected, you will be prompted to confirm the deletion.
- To remove non-empty directories and all files without being prompted, use rm with the -r (recursive) and -f options: rm -rf dirname
- To remove multiple directories at once, use the rm -r command followed by the space-separated directory names rm -r directoryname1 directoryname2 directoryname3
As with files, you can also use a wildcard and regular expansions to match multiple directories.
Final remarks: How to Remove Directory in Linux
I hope you understand this article, How to Remove Directory in Linux. If your answer is no, you can ask anything via the contact forum section related to this article. And if your answer is yes, please share this article with your friends and family to give us your support.