- 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:
- 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
- How to remove files and directories quickly via terminal (bash shell) [closed]
- 4 Answers 4
- How do I remove all sub-directories from within a directory?
- 6 Answers 6
- How to remove all files from a directory?
- 9 Answers 9
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.
Источник
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
Источник
How to remove files and directories quickly via terminal (bash shell) [closed]
Want to improve this question? Update the question so it’s on-topic for Stack Overflow.
Closed 6 years ago .
From terminal window:
When I use the rm command it can only remove files.
When I use the rmdir command it only removes empty folders.
If I have a directory nested with files and folders within folders with files and so on, is there any way to delete all the files and folders without all the strenuous command typing?
If it makes a difference, I am using the mac bash shell from terminal, not Microsoft DOS or linux.
4 Answers 4
-r «recursive» -f «force» (suppress confirmation messages)
Would remove everything (folders & files) in the current directory.
But be careful! Only execute this command if you are absolutely sure, that you are in the right directory.
Yes, there is. The -r option tells rm to be recursive, and remove the entire file hierarchy rooted at its arguments; in other words, if given a directory, it will remove all of its contents and then perform what is effectively an rmdir .
The other two options you should know are -i and -f . -i stands for interactive; it makes rm prompt you before deleting each and every file. -f stands for force; it goes ahead and deletes everything without asking. -i is safer, but -f is faster; only use it if you’re absolutely sure you’re deleting the right thing. You can specify these with -r or not; it’s an independent setting.
And as usual, you can combine switches: rm -r -i is just rm -ri , and rm -r -f is rm -rf .
Also note that what you’re learning applies to bash on every Unix OS: OS X, Linux, FreeBSD, etc. In fact, rm ‘s syntax is the same in pretty much every shell on every Unix OS. OS X, under the hood, is really a BSD Unix system.
Источник
How do I remove all sub-directories from within a directory?
This question is kind of a phase II to the first question I posted at here
I have a directory that contains a bunch of sub-directories, .zip files, and other random files not contained within a sub-directory.
I’d like a command line script to remove all sub-directories from within the parent directory, but keep all zip files and loose files that don’t belong to any sub-directories. All of the sub-directories have content, so I believe I’d need to force their deletion with the -f command.
So basically, a command that looks inside the parent directory (or the current directory), deletes all folders from within it, but keeps all other content and files that are not a folder or contained within a folder.
I understand that deleting items from the command line requires special care, but I have already taken all necessary precautions to back up remotely.
6 Answers 6
In BASH you can use the trailing slash (I think it should work in any POSIX shell):
Note the — which separates options from arguments and allows one to remove entries starting with a hyphen — otherwise after expansion by the shell the entry name would be interpreted as an option by rm (the same holds for many other command line utilities).
Add the -f option if you don’t want to be prompted for confirmation when deleting non-writeable files.
Note that by default, hidden directories (those whose name starts with . ) will be left alone.
An important caveat: the expansion of */ will also include symlinks that eventually resolve to files of type directory. And depending on the rm implementation, rm -R — thelink/ will either just delete the symlink, or (in most of them) delete the content of the linked directory recursively but not that directory itself nor the symlink.
If using zsh , a better approach would be to use a glob qualifier to select files of type directory only:
to include symlinks to directories (but because, this time, the expansion doesn’t have trailing / s, it’s the symlink only that is removed with all rm implementations).
With bash , AT&T ksh , yash or zsh you can do:
to strip the trailing / .
In addition to the wildcard way, you can also use find (at least GNU find) to do this:
As with other find lines, you can run the first part ( find -mindepth 1 -maxdepth 1 -type d ) to see a list of directories that will be removed.
Portably (POSIXly), you’d use the -exec cmd <> + syntax instead of -print0 | xargs -r0 , and -prune do limit the depth:
(and remove the echo to actually do it).
A safer option (here also assuming GNU mv ) is to do something like this:
any of which will move all the stuff into ../to-rm instead of deleting it. You can verify it did what you wanted, them rm -Rf that directory at your leisure.
You might want to create a script for some of these suggestions, especially rm -R — */ , and keep it in your /usr/local/bin folder; or create an alias in your
/.bashrc file. Since it’s so easy to mistype a command and break your system — even a single letter and/or the order of letters can result in catastrophic consequences — this would serve as a somewhat more robust solution than having to type the different options and arguments each time you want to accomplish this task.
Also, you may want to include the -i or —interactive=once or -I or —interactive=always option to your script/command which will serve as another tool to avoid the unintended deletions.
Furthermore, something like derobert suggested would be best; just copy/paste the script into a file/terminal-editor and adjust it to your specific needs, and the files/directories will be moved into a single directory (the contents of which you can check/verify) that you can simply remove by issuing the rm -rf command.
Another option is to use a GUI-application, such as your file manager, and simply select all applicable files/folders you want to delete. Check your distribution’s manual-pages if you don’t have permissions.
Finally, if the folders are empty — essentially simple filenames — you can use the rmdir command to delete them. It doesn’t work for everything you may want to delete, but it will come in handy at times when you want to do some «house-cleaning». **You may want try the -p —ignore-fail-on-non-empty option, which will allow you to delete certain sub-directories as well as their empty «parents»—the directories in which they reside.
Источник
How to remove all files from a directory?
The closest I’ve gotten is
but that doesn’t work for files that don’t have an extension.
9 Answers 9
Linux does not use extensions. It is up to the creator of the file to decide whether the name should have an extension. Linux looks at the first few bytes to figure out what kind of file it is dealing with.
To remove all non-hidden files* in a directory use:
However, this will show an error for each sub-directory, because in this mode it is only allowed to delete files.
To remove all non-hidden files and sub-directories (along with all of their contents) in a directory use:
* Hidden files and directories are those whose names start with . (dot) character, e.g.: .hidden-file or .hidden-directory/ . Note that, in Bash, if the dotglob option (which is off by default) is set, rm will act on hidden files too, because they will be included when * is expanded by the shell to provide the list of filename arguments.
To remove a folder with all its contents (including all interior folders):
To remove all the contents of the folder (including all interior folders) but not the folder itself:
or, if you want to make sure that hidden files/directories are also removed:
To remove all the «files» from inside a folder(not removing interior folders):
Warning: if you have spaces in your path, make sure to always use quotes.
is equivalent to 2 separate rm -rf calls:
To avoid this issue, you can use ‘ single-quotes ‘ (prevents all expansions, even of shell variables) or » double-quotes » (allows expansion of shell variables, but prevents other expansions):
- rm — stands for remove
- -f — stands for force which is helpful when you don’t want to be asked/prompted if you want to remove an archive, for example.
- -r — stands for recursive which means that you want to go recursively down every folder and remove everything.
Источник