This content has been archived, and is no longer maintained by Indiana University. Information here may no longer be accurate, and links may no longer be available or reliable.
To remove a directory that you own, use the rmdir
command. Forexample, to remove a subdirectory named mydir
that existsin your current working directory, at the Unix prompt,enter:
rmdir mydir
If mydir
exists, and is an empty directory, it will beremoved. If the directory is not empty or you do not have permissionto delete it, you will see an error message.
To remove a directory that is not empty, use the rm
commandwith the -r
option for recursive deletion. Bevery careful with this command, because using the rm -r
command will delete not only everything in the named directory, but alsoeverything in its subdirectories. If you don't use this command verycautiously, you risk deleting all or most of your files.
If the subdirectory mydir
exists in your currentdirectory and it is not empty, you can delete it by entering at theUnix prompt:
rm -r mydir
For more information on the rmdir
and the rm -r
commands, see their man pages. At the Unix prompt, enter:
man rmdir
Related documents
Introduction to Unix commandsList the files in a directory in UnixCreate a new directory in UnixDetermine your current working directory in UnixUse the Unix man command to read manual pagesGet a recursive directory listing in Unix
This is document abet in the Knowledge Base.
Last modified on 2018-01-18 08:51:27.
FAQs
How do I remove a directory in Unix? ›
To remove a directory and all its contents, including any subdirectories and files, use the rm command with the recursive option, -r . Directories that are removed with the rmdir command cannot be recovered, nor can directories and their contents removed with the rm -r command.
How do I completely remove a directory in Linux? ›- Use the rmdir or rm -d command to remove empty directories.
- Use the rm -r command to remove non-empty directories.
To remove a directory that is not empty, use the rm command with the -r option for recursive deletion. Be very careful with this command, because using the rm -r command will delete not only everything in the named directory, but also everything in its subdirectories.
How files in a directory can be removed? ›...
The procedure to remove all files from a directory:
- Open the terminal application.
- To delete everything in a directory run: rm /path/to/dir/*
- To remove all sub-directories and files: rm -r /path/to/dir/*
The rm command removes complete directories, including subdirectories and files. The rmdir command removes empty directories.
What is the fastest way to delete a directory in Linux? ›To delete files in Linux, the most commonly used command is rm command. Let's see some example's of rm command. -f used in the above command, will delete the file forcefully without asking for a confirmation. The above command rmdir, will only delete the directory if its empty.
Why I Cannot remove a directory in Linux? ›- Open the terminal application on Linux.
- The rmdir command removes empty directories only. Hence you need to use the rm command to remove files on Linux.
- Type the command rm -rf dirname to delete a directory forcefully.
- Verify it with the help of ls command on Linux.
- To remove an empty directory, use either rmdir or rm -d followed by the directory name: rm -d dirname rmdir dirname.
- To remove non-empty directories and all the files within them, use the rm command with the -r (recursive) option: rm -r dirname.
The non-empty directory means the directory with files or subdirectories. We can delete the directory by using the Delete() method of the Directory class.
How do I delete multiple files in a directory in Linux? ›To delete multiple files at once, simply list all of the file names after the “rm” command. File names should be separated by a space. With the command “rm” followed by multiple file names, you can delete multiple files at once.
How to remove all files in a directory in Linux except one? ›
- To delete all files in a directory except filename, type the command below: $ rm -v !("filename") Delete All Files Except One File in Linux.
- To delete all files with the exception of filename1 and filename2: $ rm -v !("filename1"|"filename2") Delete All Files Except Few Files in Linux.
How to find and delete directory recursively on Linux or Unix-like system. I type ' find . -type d -iname foo -delete ' command to find all foo directories and delete them.
Which command is used to delete a file from directory? ›Use the rm command to remove files you no longer need. The rm command removes the entries for a specified file, group of files, or certain select files from a list within a directory. User confirmation, read permission, and write permission are not required before a file is removed when you use the rm command.
Which command is used to delete the directory that is important? ›rm -r will recursively delete a directory and all its contents (normally rm will not delete directories, while rmdir will only delete empty directories).
How do I delete a directory more than 30 days in Unix? ›- ls -l LinuxHintDirectory/
- sudo find LinuxHintDirectory -type f -mtime +30 -delete.
- sudo find ~/LinuxHintDirectory -type f -mtime +30 -delete.
- ls -l LinuxHintDirectory/
- sudo find ~/LinuxHintDirectory -type d -mtime +30 -delete.
Delete a Directory ( rm -r )
To delete (i.e. remove) a directory and all the sub-directories and files that it contains, navigate to its parent directory, and then use the command rm -r followed by the name of the directory you want to delete (e.g. rm -r directory-name ).
Right-click the Shell (folder) key, select New, and click on Key. Name the key Quick Delete and press Enter.
Which command is are used to remove directories in Linux? ›There are two ways to remove directories in Linux: the rm and rmdir commands. The TL;DR of both commands is that rm deletes directories that may contain content such as files and subdirectories, while rmdir ONLY deletes empty directories.
How remove all files from a directory in Unix? ›- To delete the file named myfile, type the following: rm myfile.
- To delete all the files in the mydir directory, one by one, type the following: rm -i mydir/* After each file name displays, type y and press Enter to delete the file. Or to keep the file, just press Enter.