Delete folder in linux with all files

In Unix, how do you remove everything in the current directory and below it?

But how do you delete everything in the current directory as well as every subdirectory below it and the contents of all of those subdirectories?

10 Answers 10

Practice safe computing. Simply go up one level in the hierarchy and don’t use a wildcard expression:

The two dashes — tell rm that is not a command-line option, even when it begins with a dash.

Because you are specifically matching a named directory and are thus less likely to delete something that you don’t intend to delete.

doesn’t it delete the directory itself too? You have to do mkdir afterwards. But then any hardlink referring to that directory will be a distinct directory afterwards.

hmm. i thought he wanted to delete everything in the current directory, but not the directory itself. how do we do that?

Will delete all files/directories below the current one.

If you want to do the same with another directory whose name you have, you can just name that

If you want to remove not only the sub-directories and files of it, but also the directory itself, omit -mindepth 1 . Do it without the -delete to get a list of the things that will be removed.

I needed to delete all the files in sub-directories, but did not want to delete the sub-directories themselves. find -mindepth 2 -delete worked great!

You need -mindepth 1 if you are specifying a directory ( find -mindepth 1 -delete ). Otherwise Johannes is right it will not delete the current working directory (when using find -delete ).

I tried: find -mindepth 1 -delete but i got illegal option — m but it worked great when i removed the mindepth option find . -delete

and then hit ESC-*, and bash will expand the * to an explicit list of files and directories in the current working directory.

  • I can review the list of files to delete before hitting ENTER.
  • The command history will not contain «rm -rf *» with the wildcard intact, which might then be accidentally reused in the wrong place at the wrong time. Instead, the command history will have the actual file names in there.
  • It has also become handy once or twice to answer «wait a second. which files did I just delete?». The file names are visible in the terminal scrollback buffer or the command history.

In fact, I like this so much that I’ve made it the default behavior for TAB with this line in .bashrc:

bind TAB:insert-completions 

Update: The . stands for current directory, but we cannot use this. The command seems to have explicit checks for . and .. . Use the wildcard globbing instead. But this can be risky.

A safer version IMO is to use:

(this prompts you for confirmation before deleting every file/directory.)

Читайте также:  Основные свойства системы linux

When doing things like this, I’ve found a quick ls -r . first lets you see what you are going to delete. Useful to give a quick idea that you aren’t going to delete the whole disk.

@Yen — because if you do it in the wrong place you can get disastrous results. Using a specific name in the wrong place can only go wrong if the same subdirectory happens to exist there.

It is correct that rm –rf . will remove everything in the current directly including any subdirectories and their content. The single dot ( . ) means the current directory. be carefull not to do rm -rf .. since the double dot ( .. ) means the previous directory.

This being said, if you are like me and have multiple terminal windows open at the same time, you’d better be safe and use rm -ir . Lets look at the command arguments to understand why.

First, if you look at the rm command man page ( man rm under most Unix) you notice that –r means «remove the contents of directories recursively». So, doing rm -r . alone would delete everything in the current directory and everything bellow it.

In rm –rf . the added -f means «ignore nonexistent files, never prompt». That command deletes all the files and directories in the current directory and never prompts you to confirm you really want to do that. -f is particularly dangerous if you run the command under a privilege user since you could delete the content of any directory without getting a chance to make sure that’s really what you want.

On the otherhand, in rm -ri . the -i that replaces the -f means «prompt before any removal». This means you’ll get a chance to say «oups! that’s not what I want» before rm goes happily delete all your files.

In my early sysadmin days I did an rm -rf / on a system while logged with full privileges (root). The result was two days passed a restoring the system from backups. That’s why I now employ rm -ri now.

Источник

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

Читайте также:  What is acpi in linux

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.

Источник

How to Delete Folders in Ubuntu Command Line

Deleting folders in Ubuntu command line

You probably already know how to delete files in the command line with the rm command.

The same rm command can also be used to delete folders (called directories in Linux terminology).

rm -r directory_name_or_path

The -r option is important and I’ll explain it in the later section of this article.

Creating a new folder in Ubuntu is simple. Deleting them might not be that straightforward, especially if the folders are not empty.

There is a dedicated rmdir command for removing directories. However, it can only delete empty directories. This is why you have to use the rm command for deleting folders.

Let’s see more about removing directories in the command line in Ubuntu Linux.

Delete folders using rm command

On a fundamental level, directories are just special files that have information about the location of files stored in the memory.

This means you can almost use the rm command to remove them, just like regular files. Almost because there is a slight twist here.

If you try to delete directories the same way as files, you may encounter the «rm: cannot remove XYZ is a directory»:

$ rm no_strings_on_me rm: cannot remove 'no_strings_on_me': Is a directory

This is just a failsafe so that you don’t accidentally delete a folder with multiple files inside it.

Читайте также:  Linux screen recorder mint

To purposely delete a folder with the rm command, you have to use the recursive option -r :

This way, you tell the Linux system that you know that you are deleting a directory recursively with all its content. The contents are deleted first and the directory is removed.

Force remove a directory and its contents

A directory may have numerous files and if some of those files are write-protected, you’ll be prompted to confirm the deletion.

$ rm -r new_dir rm: remove write-protected regular empty file 'new_dir/b.txt'?

It’s okay for a couple of files but if it keeps on asking this question for too many files, it will be tiring.

This is when the -f option comes to the rescue so that you can force delete the files. You won’t get promoted anymore.

rm -rf directory_name_or_path

If you use rm -rf dir_name/* it will delete the contents of the dir_name folder but the dir_name will still exist. Why? Because you are explicitely telling the system to delete the contents of directory, not the directory.

Removing empty directories with rmdir command

If you accidentally delete the wrong file, only one file is deleted. If you accidentally delete one directory, multiple files, and even nested directories will be deleted. Which would be a terrible loss and an irreversible one.

The rmdir command exists for this sole purpose. When you use this command, the directory will be removed only if it is found to be empty. If the directory contains any files or sub-directories, the command will stop with an error that the directory is not empty.

Let us first take a look at its syntax:

I have a non-empty directory and an empty directory, as shown below:

$ ls * my_empty_dir: non_empty_dir: string1 string2 string3 string4

Let us see how it looks when I try to remove both of these directories using the rmdir command.

$ rmdir -v my_empty_dir rmdir: removing directory, 'my_empty_dir' $ rmdir -v non_empty_dir rmdir: removing directory, 'non_empty_dir' rmdir: failed to remove 'non_empty_dir': Directory not empty

The rmdir command prevented me from removing ‘non_empty_dir’ because it is not empty.

If you want to stick to using the rm command, it has a «safe» delete option as well. The rm command, when paired with -d option will exit when the directory is not empty.

Since rmdir did not delete ‘non_empty_dir’, let’s see if rm -d deletes it.

$ rm -d non_empty_dir rm: cannot remove 'non_empty_dir': Directory not empty

Conclusion

To summarize what you just learned about deleting folders in Ubuntu command line:

Command Description
rm -r dir Delete the dir folder
rm -rf dir Force delete the dir folder
rm -rf dir/* Force delete the contents but not dir itself
rmdir dir Delete dir if it is empty

The rm command is what I personally prefer, as it is used for the removal of files as well as directories, but rmdir command can be useful too, depending on the scenario.

Do let me know if you have any queries or confusion 🙂

Источник

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