Recursive delete folder linux

Unix Recursively delete files and folders

So I am working on a program that delete files and folders recursively. For some reason I am stucked in endless loop. Please point out where I am wrong.

for file in $1 do if [ -f $file ] #If it is a file just delete it then echo "delete $file" elif [ -d $file ] #if it is a folder then #look inside and see if it is empty or not if [ "$(ls -A $file)" ] then recursive $file #recursively call else #if the folder is empty just delete it echo "delete $file" fi fi done 

Is there a reason you don’t want to use rm -rf /path/to/folder ? The details of what -rf does can be found with rm —help .

@Prix: I think using for file in $1 will not include hidden files (like . and .. ) by default, right?

@justhalf well you can use -rf to recursively delete an entire folder so unless he gives us more information.

3 Answers 3

recursive $file #recursively call 

This effectively turns into repeating

That is, it keeps repeating for same first dir entry. You need to think the whole function again, either pass different parameter in recursive call, or do not use for over $1 . Simplest may be to change the for (untested):

Also add test for case where $file is . or .. , or remove $file/.* part from my for .

About debugging bash scripts, following two set options are often useful, you can for example add them to start of your script.

set -x # print every final command line when it is executed set +x # turn -x off set -v # print script lines as they are read set +v # turn -v off 

I used for file in $(tree -ifa -L 1 $1 —noreport | sort -r) and still get in infinite unless I actually delete the files instead of echoing out. Is there like a better way to rewrite it? I am pretty much out of idea for now

@WaiHein Well, hard for me to debug without actually writing a script and running it, but see the edit about doing the debugging yourself. But most likely the first item in for is the directory itself. to avoid that, you use tail to filter out first line, untested example: $(tree -ifa -L 1 $1 —noreport | tail -n+2 | sort -r)

If I understand what you’re trying to achieve, I suspect you’d be better off using the find command instead of rolling your own shell script.

For example, the following command should delete «just files» named foo.txt recursively starting at the current directory, and show you the list of files being deleted.

find . -name foo.txt -type f -exec rm -v <> \; 

You can man find for details on how to use this tool, but basically the options are along the lines of:

Читайте также:  Разбит диски под линукс

where path is one or more start directories for your recursive search, and expression is a list of conditions or actions to perform.

In the example above, we have two obvious conditions — -name foo.txt denotes the filename, and -type f says «this is a normal file». The third option in the expression executes a command line terminated with an escaped semicolon, replacing <> with the name of each file found.

Also, when processing multiple filenames, beware not to parse the output of ls . For details read ParsingLs from Greg’s wiki.

Lastly, if you’re trying to create a script that will take multiple filenames to delete, you could go with something like this:

#!/bin/sh for file in $@; do find . -type f -name "$file" -exec rm -v <> \; done 

Note that this will also work in simpler POSIX style shells, it doesn’t require bash.

Источник

Recursive delete folder linux

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.

Читайте также:  Linux mint светлая тема

Источник

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.

Читайте также:  Open source backup software linux

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