In Unix, how do I remove a directory? (2023)

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.

(Video) How to delete or remove non empty directory in Unix

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 -rcommand 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.

(Video) How to delete a directory in Unix

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 -rcommands, see their man pages. At the Unix prompt, enter:

 man rmdir

This is document abet in the Knowledge Base.
Last modified on 2018-01-18 08:51:27.

(Video) Linux Command Line (11) Delete Files and Directories

(Video) How to Remove a Directory and all its contents in UNIX

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? ›

To permanently remove a directory in Linux, use either the rmdir or rm command:
  1. Use the rmdir or rm -d command to remove empty directories.
  2. Use the rm -r command to remove non-empty directories.
Dec 6, 2022

Can you remove a directory that is not empty? ›

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? ›

Another option is to use the rm command to delete all files in a directory.
...
The procedure to remove all files from a directory:
  1. Open the terminal application.
  2. To delete everything in a directory run: rm /path/to/dir/*
  3. To remove all sub-directories and files: rm -r /path/to/dir/*
Sep 11, 2022

Which method is used to delete a directory? ›

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? ›

Here is how to forcefully delete a folder 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.
Nov 2, 2020

How to remove non-empty directory in Linux without prompt? ›

How to Remove Directories (Folders)
  1. To remove an empty directory, use either rmdir or rm -d followed by the directory name: rm -d dirname rmdir dirname.
  2. To remove non-empty directories and all the files within them, use the rm command with the -r (recursive) option: rm -r dirname.
Aug 10, 2021

What is the function to remove a non-empty directory? ›

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? ›

  1. To delete all files in a directory except filename, type the command below: $ rm -v !("filename") Delete All Files Except One File in Linux.
  2. To delete all files with the exception of filename1 and filename2: $ rm -v !("filename1"|"filename2") Delete All Files Except Few Files in Linux.
Oct 25, 2016

How do I find and delete a directory 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? ›

In my case, the path is the LinuxHintDirectory directory located under the current directory.
  1. ls -l LinuxHintDirectory/
  2. sudo find LinuxHintDirectory -type f -mtime +30 -delete.
  3. sudo find ~/LinuxHintDirectory -type f -mtime +30 -delete.
  4. ls -l LinuxHintDirectory/
  5. sudo find ~/LinuxHintDirectory -type d -mtime +30 -delete.

How do I delete a full directory in terminal? ›

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 ).

How can I speed up deleting a folder? ›

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? ›

Deleting files (rm command)
  1. To delete the file named myfile, type the following: rm myfile.
  2. 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.

Videos

1. Linux Command Line (11) Delete Files and Directories
(TheFrugalComputerGuy)
2. How to Remove an Empty Directory in #UNIX #LINUX
(Linux Vasanth)
3. How to force delete a directory in Unix
(Sagar S)
4. How to delete multiple files and directories in Unix
(Sagar S)
5. How to change directories in UNIX/Linux
(Computer Programming University)
6. 11 - Unix | Linux Shell - Remove Files | Remove Non Empty Directory | RM Command
(Mohammedi Computers)
Top Articles
Latest Posts
Article information

Author: Clemencia Bogisich Ret

Last Updated: 11/19/2022

Views: 5828

Rating: 5 / 5 (60 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Clemencia Bogisich Ret

Birthday: 2001-07-17

Address: Suite 794 53887 Geri Spring, West Cristentown, KY 54855

Phone: +5934435460663

Job: Central Hospitality Director

Hobby: Yoga, Electronics, Rafting, Lockpicking, Inline skating, Puzzles, scrapbook

Introduction: My name is Clemencia Bogisich Ret, I am a super, outstanding, graceful, friendly, vast, comfortable, agreeable person who loves writing and wants to share my knowledge and understanding with you.