- How to remove non empty Directory in Linux
- Procedure to remove non empty directory in Linux
- Examples for removing non empty directory under Linux
- How to get visual confirmation about deleting directory
- How to get confirmation prompt before every removal of a dir
- Conclusion
- Delete / Remove a Directory Linux Command
- Commands to remove a directory in Linux
- rmdir command syntax to delete directory in Linux
- Delete directory Linux Command
- How to see a diagnostic message for every directory processed
- Removing directories with rmdir and wildcards
- Linux remove entire directory including all files and sub-directories command
- Are you getting permission denied error message while removing directories?
- Use find command to delete unwanted directories
- How to find and remove all empty directories
- Conclusion
- Remove Directory Recursively without Prompting for Confirmation in Linux
- Step 1: List Contents of Directories
- Step 2: Remove a Single Directory Recursively without Prompting the User for Confirmation
- Step 3: Remove Multiple Directories Recursively without Prompting the User for Confirmation
- Step 4: Verify Deletion of Specified Directories
- Conclusion
- About the author
- Aqsa Yasin
- How to delete a non-empty directory in Terminal?
- 4 Answers 4
- Use the below command :
- In case user doesn’t have the permission to delete the folder:
How to remove non empty Directory in Linux
The following commands works with CentOS, RHEL, Fedora, Alpine, Arch, Debian, Ubuntu and all other Linux distros. Let us see some examples.
Procedure to remove non empty directory in Linux
We use the rm command to delete a directory that is not empty. The syntax is:
rm -rf dir-name
rm -rf /path/to/dir/name
Be careful when you use the rm command with -r and -f options. The -r option remove directories and their contents recursively including all files. The -f option to rm command ignore nonexistent files and arguments, never prompt for anything. There is no undo option. So you have to be very careful with rm -rf command. Let us see some examples.
Examples for removing non empty directory under Linux
Trying to remove trip-pictures directory with the rmdir command in Linx:
rmdir trip-pictures
Sample outputs:
To see files inside the directory use ls command ls -l trip-pictures
ls trip-pictures
To delete all files inside trip-pictures including folder itself run the following rm command:
rm -rf trip-pictures
How to get visual confirmation about deleting directory
Pass the -v to the rm command:
rm -vrf dir1
rm -vrf dir1 dir2
Sample outputs:
How to get confirmation prompt before every removal of a dir
You need to pass the -i option to the rm command:
rm -ir foo
Sample outputs:
To get prompt once before removing more than three files, or when removing recursively; less intrusive than -i , while still giving protection against most mistakes pass the -I option:
rm -Ir bar
Sample outputs:
Want to get info on all rm and rmdir switches? Try:
rm —help
rmdir —help
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
Conclusion
You learned how to remove non empty directory under Linux or Unix-like operating systems using command line options. For more information see rm command and rmdir command command man pages by typing the following man command command:
man rm
man rmdir
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Источник
Delete / Remove a Directory Linux Command
Commands to remove a directory in Linux
There are two command to delete a folder in Linux:
- rmdir command – Deletes the specified empty directories and folders in Linux.
- rm command – Delete the file including sub-directories. You can delete non-empty directories with rm command in Linux.
Let us see some examples and usage in details delete the directories.
rmdir command syntax to delete directory in Linux
The rmdir command remove the DIRECTORY(ies), if they are empty. The syntax is:
rmdir directory-name
rmdir [option] directory-name
Open the terminal application and run command to delete given directory. For example, delete a folder named dir1:
rmdir dir1
Delete directory Linux Command
Open a command line terminal (select Applications > Accessories > Terminal), and then type the following command to remove a directory called /tmp/docs:
rmdir /tmp/docs
If a directory is not empty you will get an error message that read as follows:
rmdir letters
Sample outputs:
You can cd to the directory to find out and list all files:
$ cd letters
$ ls
Delete those files or directories. In this next example, remove data, foo and bar if bar were empty, foo only contained bar and data only contained foo directories:
cd /home/nixcraft
rmdir -p data/foo/bar
Where,
- -p : Each directory argument is treated as a pathname of which all components will be removed, if they are empty, starting with the last most component.
How to see a diagnostic message for every directory processed
Pass the -v option to the rmdir command:
$ rmdir -v dir1
Sample outputs:
Removing directories with rmdir and wildcards
We can use wildcards such as ‘*’ and ‘?’ to match and delete multiple directories. For example:
$ ls -l dir*
We have three dirs named dir1, dir2, and dir3. To delete all directories starting with ‘dir’ in the current, you would use the following command:
rmdir -v dir*
Linux remove entire directory including all files and sub-directories command
To remove all directories and subdirectories use the rm command. For example, remove *.doc files and all sub-directories and files inside letters directory, type the following command:
Warning : All files including subdirectories will be deleted permanently when executed the following commands.
$ rm -rf letters/
Sample session:
Where,
- -r : Attempt to remove the file hierarchy rooted in each file argument i.e. recursively remove subdirectories and files from the specified directory.
- -f : Attempt to remove the files without prompting for confirmation, regardless of the file’s permissions
Are you getting permission denied error message while removing directories?
Only owners can delete their directories. However, a sysadmin can delete any directories created by anyone on the system. The syntax is:
sudo rmdir /path/to/dir/
sudo rm -rf dir2
When prompted, you need to provide root user or sudo user password.
Use find command to delete unwanted directories
Say you want to find out all directories named ‘session’ and delete them in the current directory, run:
find . -type d -iname ‘session’ -delete
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
How to find and remove all empty directories
Run:
find . -type d -iname ‘session’ -empty -delete
Where,
- -type d : Only search for directories and ignore all other files.
- -iname ‘session’ : Search directory named ‘session’. You can use wildcards here too. For example, -iname ‘dir*’ .
- -empty : Only match empty directories
- -delete : Deletes all found empty directories only
To delete all ‘.DS_store’ directories stored in /var/www/html, run:
sudo find /var/www/html/ -type d -name .DS_Store -exec rm <> \;
OR
sudo find /var/www/html/ -type d -name .DS_Store -exec rm <> +
The -exec option to the find command run an external command named rm to delete all files. The “ rm <> +/ ” is a better option as it uses one rm command to delete all .DS_Store directories.
Conclusion
This page showed how to delete a directory when it is empty. Further, it showed, how to remove folders using the rm and rmdir commands. See rm help page page for more info:
- For more information read man pages: rm(1)
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Источник
Remove Directory Recursively without Prompting for Confirmation in Linux
This is where the concept of recursive deletion comes into play. Recursive deletion aims to delete all the files and directories within a subdirectory. Generally, whenever you attempt to delete any file or a directory within any operating system, the OS prompts you to provide confirmation to prevent accidental deletion of important files or directories. However, if you are 100% sure of what you are going to delete, and there is a large number of files to be deleted, then you might find it troublesome to provide confirmation for every file or directory.
In this case, you can remove a directory recursively without being prompted by the OS for confirmation every time. This article explains how to remove a directory recursively without prompting the user for confirmation in Linux Mint 20.
To remove a directory recursively in Linux Mint 20 without prompting the user for confirmation, the following series of steps should be performed.
Step 1: List Contents of Directories
We have created two sample directories, namely, Directory1 and Directory2, in our Home directory to demonstrate this method of removing directories recursively in Linux Mint 20. Directory1 contains two subdirectories, named D1 and D2, whereas Directory2 contains the file named D5. We will show you the contents of our Home directory so that you can verify that Directory1 and Directory2 exist in our Home directory. To list the contents of the Home directory, we will run the following command in our terminal:
You can see from the output of this command that Directory1 and Directory2 exist in our Home directory, as highlighted in the image below. We performed this step so that you can easily verify the deletion performed in Step 4 of this method.
Next, we will show you the contents of our Directory1 by running the following command in the terminal:
Here, you can give the path of any directory of which the contents you would like listed.
The contents of Directory1 are shown in the image below:
Finally, we will show you the contents of our Directory2 by running the following command in the terminal:
Here, you can give the path of any directory of which the contents you would like listed.
The contents of Directory2 are shown in the image below:
Step 2: Remove a Single Directory Recursively without Prompting the User for Confirmation
To remove a single directory recursively without prompting the user for confirmation, run the following command in your terminal:
Here, replace “PathOfTheDirectoryToBeDeleted” with the exact path of the directory that you intend to delete. In our case, the directory is /home/aqsa_yasin/Directory1. The “-rf” flag, along with the “rm” command, removes a directory recursively without prompting the user for confirmation.
Step 3: Remove Multiple Directories Recursively without Prompting the User for Confirmation
If you wish to remove multiple directories recursively at a time without prompting the user for confirmation, then skip Step 2 and, instead, run the following command in your terminal:
Here, replace “Path1” and “Path2” with the exact paths of the directories that you intend to delete. In our case, we only wanted to delete two directories, i.e., Directory1 and Directory2. However, you can remove as many directories as you want using this command simply by stating the paths of the directories, separated by spaces, following the “rm –rf” command.
Step 4: Verify Deletion of Specified Directories
After executing the command in Step 3, ideally, our Directory1 and Directory2 should be removed, along with all their subdirectories, from our Home directory. We can always confirm whether the deletion process has successfully taken place by listing down the contents of our Home directory. We can do so by running the following command in the terminal:
This time, in the output of this command, we will no longer be able to see Directory1 and Directory2 in the Home directory, as shown in the image below. This indicates that the specified directories have been removed successfully.
Conclusion
By using the method prescribed in this article, you can remove a single directory or multiple directories recursively without prompting the user for confirmation in Linux Mint 20. With this method, you can get rid of all the traces of a directory at once, including all the subdirectories and files within it, without constantly needing the user to provide consent. In this way, you can easily and quickly free up your system’s storage space for more important files and directories. I hope that, by following this article, you are now in the position to delete directories recursively without prompting the user for confirmation.
About the author
Aqsa Yasin
I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.
Источник
How to delete a non-empty directory in Terminal?
How do I delete the following directory?
This error comes up:
Is there a command to delete all the files in the directory and delete the directory folder?
4 Answers 4
Use the below command :
It deletes all files and folders contained in the lampp directory.
In case user doesn’t have the permission to delete the folder:
Add sudo at the beginning of the command :
Otherwise, without sudo you will be returned permission denied. And it’s a good practice to try not to use -f while deleting a directory:
Note: this is assuming you are already on the same level of the folder you want to delete in terminal, if not:
FYI: you can use letters -f , -r , -v :
- -f = to ignore non-existent files, never prompt
- -r = to remove directories and their contents recursively
- -v = to explain what is being done
However, you need to be careful with a recursive command like this, as it’s easy to accidentally delete a lot more than you intended.
It is a good idea to always double-check which directory you’re in, and whether you typed the command correctly, before pressing Enter.
Safer version
Adding -i makes it a little safer, because it will prompt you on every deletion. However, if you are deleting many files this is not going to be very practical. Still, you can try this first.
Many people suggest using -f (combining it into -Rf or -rf ), claiming that it gets rid of annoying prompts. However, in normal cases you don’t need it, and using it suppresses some problems that you probably do want to know about. When you use it, you won’t be warned if your arguments supply a non-existing directory or file(s): rm will just silently fail to delete anything. As a general rule, try first without the -f : if there are problems with your arguments, then you’ll notice. If you start getting too many prompts about files without write access, then you can try it with -f . Alternatively, run the command from a user (or the superuser using sudo) that has full permissions to the files and directories you’re deleting to prevent these prompts in the first place.
Источник