Linux remove dir recursive

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.

Читайте также:  Linux команды терминала путь

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.

Источник

Linux remove dir recursive

NAME

rm - remove files or directories

SYNOPSIS

DESCRIPTION

This manual page documents the GNU version of rm. rm removes each specified file. By default, it does not remove directories. If the -I or --interactive=once option is given, and there are more than three files or the -r, -R, or --recursive are given, then rm prompts the user for whether to proceed with the entire operation. If the response is not affirmative, the entire command is aborted. Otherwise, if a file is unwritable, standard input is a terminal, and the -f or --force option is not given, or the -i or --interactive=always option is given, rm prompts the user for whether to remove the file. If the response is not affirmative, the file is skipped.

OPTIONS

Remove (unlink) the FILE(s). -f, --force ignore nonexistent files and arguments, never prompt -i prompt before every removal -I prompt once before removing more than three files, or when removing recursively. Less intrusive than -i, while still giving protection against most mistakes --interactive[=WHEN] prompt according to WHEN: never, once (-I), or always (-i). Without WHEN, prompt always --one-file-system when removing a hierarchy recursively, skip any directory that is on a file system different from that of the corresponding command line argument --no-preserve-root do not treat '/' specially --preserve-root do not remove '/' (default) -r, -R, --recursive remove directories and their contents recursively -d, --dir remove empty directories -v, --verbose explain what is being done --help display this help and exit --version output version information and exit By default, rm does not remove directories. Use the --recursive (-r or -R) option to remove each listed directory, too, along with all of its contents. To remove a file whose name starts with a '-', for example '-foo', use one of these commands: rm -- -foo rm ./-foo Note that if you use rm to remove a file, it might be possible to recover some of its contents, given sufficient expertise and/or time. For greater assurance that the contents are truly unrecoverable, consider using shred.

AUTHOR

Written by Paul Rubin, David MacKenzie, Richard M. Stallman, and Jim Meyering.

REPORTING BUGS

Report rm bugs to bug-coreutils@gnu.org GNU coreutils home page: http://www.gnu.org/software/coreutils/> General help using GNU software: http://www.gnu.org/gethelp/> Report rm translation bugs to http://translationproject.org/team/>
Copyright © 2013 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html>. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.

SEE ALSO

unlink(1), unlink(2), chattr(1), shred(1) The full documentation for rm is maintained as a Texinfo manual. If the info and rm programs are properly installed at your site, the command info coreutils 'rm invocation' should give you access to the complete manual.

© 2019 Canonical Ltd. Ubuntu and Canonical are registered trademarks of Canonical Ltd.

Читайте также:  Линукс в домашних условиях

Источник

Remove Directory Recursively without Prompting for Confirmation in Linux

At times, you may have more than one directory within a single directory. This is known as a subdirectory, defined as a directory within a directory. Usually, the subdirectories within a directory are closely related to that directory. This means that whenever you feel like you do not need a particular directory anymore, then you also will not need its subdirectories further. So, the question arises, “How do I get rid of all the files and directories within a directory?”

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.

Источник

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