Remove Directory (or Delete) on linux computer system can be performed via terminal using command line. While directory also can be created, renamed, modified, grown, and shrunk (truncated). In most cases, software programs that are executed on the computer handle these operations, but the user of a computer can also modify directories if necessary.
For instance, directories are normally removed by program in response to user commands, but the user can also delete these directorie directly by using a file manager program such as Windows Explorer (on Windows computers) or via terminal with command line (CLI) in linux system.
How To Remove Directory on Linux?
You can easily remove or delete directory or folder on linux with the following methods rm, rmdir, and Information via terminal on your system with the command line.
rm
“rm” command is used to delete objects such as files, directories, symbolic links and the contents within them from the linux system. You should be careful with this, because once the directory is deleted, it is hardly to recover.
To remove a directory and all its contents, including any sub-directories and files on linux:
- Open “Terminal” on linux system
- Keep on the current working directory or go to the specific directory (use “cd” to change directory) that you want to remove the directory inside it
- Use “rm” command and must followed with “option” and specify “directory-name”
- The syntax for using the “rm” command as shown below
rm [option] [directory-name]
- Remove a directory on linux in the current directory
rm -r Docs
It will delete Directory called “Docs” and all its contains.
- Remove multiple directories on linux in the working directory
rm -r Docs Pics Musics
It will delete Directories called “Docs”, “Pics” and “Musics”.
- Remove a directory in linux system
rm -r /home/username/Documents/docs
It will only delete the last directory called “docs” and all its contains.
- “-r” means attempt to remove the directory hierarchy, recursively remove sub-directories and files from the specified directory.
- Remove a directory on linux in the current directory
- Done, now you’ve already removed the directories.
rmdir
“rmdir” command is used to delete a directory or folder. However, it only allows you to delete empty directories in the file system.
To remove directory on linux:
- Open “Terminal” on linux system
- Keep on the current working directory or go to the specific directory (use “cd” to change directory) that you want to remove the directory inside it
- Use “rmdir” command and specify “directory-name”
- The syntax for using the “rmdir” command as shown below
rmdir [option] [directory-name]
- Remove a directory on linux in the current directory
rmdir mydirectory
- It will remove a directory called mydirectory.
- If a directory is not empty you will get an error message that read as follows.
rmdir: failed to remove 'Doc': Directory not empty
- Remove a directory on linux in the current directory
- Done, now you’ve already removed the empty directory
More Information
“rm” command related information can be obtained with “–help”.
rm --help
Usage: rm [OPTION]… [FILE]…
Remove (unlink) the FILE(s).
-f, –force ignore nonexistent files and arguments, never prompt.
-i prompt before every removal.
-I prompt once before removing more than three files, or when removing recursively; less intrusive than -i, while still giving protection against most mistakes.
–interactive[=WHEN] prompt according to WHEN: never, once (-I), or always (-i); without WHEN, prompt always.
–one-file-system when removing a hierarchy recursively, skip any directory that is on a file system different from that of the corresponding command line argument.
–no-preserve-root do not treat ‘/’ specially.
–preserve-root[=all] do not remove ‘/’ (default); with ‘all’, reject any command line argument on a separate device from its parent.
-r, -R, –recursive remove directories and their contents recursively.
-d, –dir remove empty directories.
-v, –verbose explain what is being done.
–help display this help and exit.
–version output version information and exit.
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.
To remove a file whose name starts with a ‘-‘, for example ‘-foo’, use one of these commands:
rm — -foo
rm ./-foo
Note that if you use rm to remove a file, it might be possible to recover some of its contents, given sufficient expertise and/or time. For greater assurance that the contents are truly unrecoverable, consider using shred.
In general, a directory can contain either a list of files or a list of links to files. Within this definition, it is of paramount importance that the term “file” includes “directories”. This permits the existence of directory hierarchies, i.e., directories containing sub-directories.
A name that refers to a file within a directory must be typically unique. In other words, there must be no identical names within a directory. However, in some operating systems, a name may include a specification of type that means a directory can contain an identical name for more than one type of object such as a directory and a file.