- Linux delete recursive files
- NAME
- SYNOPSIS
- DESCRIPTION
- OPTIONS
- AUTHOR
- REPORTING BUGS
- COPYRIGHT
- SEE ALSO
- Delete Files Recursively in Linux
- Use the rm Command to Delete Files in Linux
- Use the -r Command to Delete Files Recursively in Linux
- Use Wildcard * to Delete Files With Similar Filenames in Linux
- Use Wildcard * to Delete Files With Same Extension in Linux
- Use the find Command to Delete Files Recursively in Linux
- Related Article — Linux File
- Unix Recursively delete files and folders
- 3 Answers 3
Linux delete recursive files
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
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.
Delete Files Recursively in Linux
This article explains how to delete files in Linux. Then, we will elaborate on the topics below.
- Delete files recursively.
- Delete files with the same extension.
- Delete files with similar filenames.
- Delete files recursively with the same extension / similar filenames.
The sample files and directories we will use throughout the article are below.
Use the rm Command to Delete Files in Linux
After the rm command, type the filename(s) you want to remove.
Use the -r Command to Delete Files Recursively in Linux
The -r flag allows you to recursively remove directories and their contents. Type the directory name you want to delete after the rm -r command. The use of a slash / after the directory name is optional.
Use Wildcard * to Delete Files With Similar Filenames in Linux
The asterisk * is called wildcard, and it gives every file that starts with the specified name as a parameter to the rm command.
We want to remove all files with the name file1 , even if the extension is different. We use a wildcard instead of specifying the extension at the end of the filename.
Use Wildcard * to Delete Files With Same Extension in Linux
This time, we want to remove all files with the same extension, even if their names are different. We use a wildcard instead of the filename then write the extension.
Use the find Command to Delete Files Recursively in Linux
We can use the find command to find and delete files recursively with similar extensions or filenames from a directory and its sub-directories.
We can use the find command with the -delete .
find . -type f -name '*.txt' -delete
Alternatively, it can be used with the exec .
Yahya Irmak has experience in full stack technologies such as Java, Spring Boot, JavaScript, CSS, HTML.
Related Article — Linux File
Copyright © 2023. All right reserved
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.