Remove all folders in directory linux

How to delete a non-empty directory in Terminal?

I’m unable to remove a directory like «New Folder» using all the above detailed commands. It’s double worded. But I want to remove that directory. Any suggestions will be welcomed. T.Divakara, Bengaluru, India

Its the blank space in the file name, try using ‘quotes’ > rmdir ‘New Folder’ < then the folder disapers, or use escape characters for non-vissible characters.

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:

sudo rm -r /path/to/folderName 

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

In my humble opinion, it’s a good practice never to add the «f» on first attempt. Its purpose is to ignore certain warning prompts that may be important, especially if you’ve accidentally done it on/from the wrong directory. In my opinion it’s good to try without the «f» first, then only if you are encountering a lot of warning prompts and you’re sure it’s OK to ignore them all, Ctrl+C out of it and repeat the command with the «f».

@thomasrutter . Agree. A file «xxx» owner: root and group: root can BE deleted with the -f switch; and without sudo. This is the message without -f: «rm: remove write-protected regular file ‘/home/william/.cache/netbeans/v08.01/tmp/xxx’? _». _Tread gently.

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.

Читайте также:  Man head in linux

Источник

How to remove files and directories quickly via terminal (bash shell) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.

From a 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 a 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 a terminal, not Microsoft DOS or Linux.

Just in case you wish to restore the files in future , don’t use «rm» for such cases . Use «rm-trash» : github.com/nateshmbhat/rm-trash

4 Answers 4

-r «recursive» -f «force» (suppress confirmation messages)

+1 and glad you added the «Be careful!» part. definitely a «Sawzall» command that can quickly turn a good day into a bad one.. if wielded carelessly.

@itsmatt: You know what they say. give someone a Sawzall, and suddenly every problem looks like hours of fun!

On a Mac? Do this instead: brew install trash then trash -rf some_dir This will move the unwanted directory into your trashbin instead of just vanishing Prestige-style into the ether. (source)

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.

Читайте также:  Gpu test benchmark linux

Источник

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:

rm -R -- *(/) # or *(D/) to include hidden ones 

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:

Beautiful! This works perfectly. I didn’t realize it was so easy to do. Would you mind elaborating on the -r vs. -R and what those commands do? Many thanks.

Just be careful not to change the order of the * and the /. rm -R — /* does something totally different that you may not want to happen 😐

In addition to the wildcard way, you can also use find (at least GNU find) to do this:

find -mindepth 1 -maxdepth 1 -type d -print0 | xargs -r0 rm -R 

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.

Читайте также:  Linux list all timezones

Portably (POSIXly), you’d use the -exec cmd <> + syntax instead of -print0 | xargs -r0 , and -prune do limit the depth:

find . ! -name . -prune -type d -exec echo rm -R <> + 

(and remove the echo to actually do it).

A safer option (here also assuming GNU mv ) is to do something like this:

find -mindepth 1 -maxdepth 1 -type d -print0 | xargs -r0 mv -i -t ../to-rm # or (but beware of symlinks!) mv -i -t ../to-rm -- */ # or mv -i -- */ ../to-rm 

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.

+1 for the «CLI-Trash bin» to-rm and find which nicely handles directories beginning with a dot (in addition to any other cumbersome escaping). 🙂

Thanks for the tip. I like the idea of moving to a separate directory before deleting. I’m working via Terminal on Mac OS X, and I believe I’d need to install some kind of GNU package to use the -mindepth and -maxdepth commands, correct?

@Evster not sure if Mac OS X find supports those. I’d guess fink (is that still around?) could install a find that does. Or you could omit them, and then filter the produced file list using grep. Definitely use the mv option on that, istead of rm.

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.

Источник

Оцените статью
Adblock
detector