I need to remove all *.html files. But, I have a parent folder that has so many child folders in it and an HTML file is present in every single child folder. I can able to remove files by visiting each child's folder. But, I need to remove every HTML file directly from a parent folder
2 Answers
Easiest method would be:
find . -type f -name '*.html' -delete
Do this from the directory where you want to start it from.
findis a command to search for something.means from the directory you currently are infmeans only search for files (dis for directories)- and search for
nameending in.html - and find has an option that
deletes them
- 309,379
Before giving you a paste & run ready solution, there are some best practices and automation patterns for linux shell I would like to explain. Same concepts are also applied in other IT fields:
- divide & conquer: split a problem into (isolated) parts to solve easily
- separation of concerns: here delete as command; find as query (CQRS)
- pipes and filters: the combination of best tools in a chain
Preview and pipe explained
Although the following recipe doesn't work for subdirectories (recursive), it illustrates a safe and modular method combining commands.
a) simple list and remove (preview first before action):
list all HTML files (just list, not revursive)
ls *.htmlthen remove
rm *.html
This is a safety first approach. You preview and check the list of files before removing. When sure to proceed, just alter the command command.
Alternatively you can easily extend your workflow using pipe:
b) directly combined with xargs
ls *.html | xargs -r0 rm
The xargs is necessary because rm can't read from stdin.
See:
http://stackoverflow.com/questions/20307299/ddg#20307392
The -r0 combines two options: -0 will deal with cornercase filenames (having newlines, spaces or quotes like it's done.html in them) which more robustly are separated by NUL character. The -r will stop running if no input is given.
See: How can I recursively delete all files of a specific extension in the current directory?
and: https://unix.stackexchange.com/a/83712
Directly remove using glob
In some shells (bash since version 4, ksh, zsh) you can recursively remove files by using wildcards (globstar) in path.
See: https://unix.stackexchange.com/questions/23576/how-do-i-recursively-delete-directories-with-wildcard
and how to check/enable the globstar
directly remove (dangerous):
rm **/*.html
This direct remove commands are dangerous because a small typo will have adverse impact. For example when accidentally inserted a blank in rm *.html it will become rm * .html. Unfortunately it will remove all files and may print the error of not finding second path '.html'.
Combine: find and remove
find (search for files in a directory hierarchy) and remove
using pipe (POSIX compliant: runs on more systems)
find . -type f -name "*.html" | xargs -r0 rmdirectly executing a following command (like the trash-cli, for later undelete):
find . -type f -name "*.html" -exec trash {}directly using convenient actions (reliable: avoid spawning and race-conditions)
find . -type f -name "*.html" -print0 -delete
Explanation:
-type fonly looks for regular files (not directoryd, not symlinks)-namefilters for names like the given pattern (can also be extended by-orand alternatively-regex)-print0prints the found file-paths (robustly NUL separated) before-exec trash {}executes the given command (here trash) with found file as argument (substitutes the curly braces)-deletedirectly removes the found file (performant because in the same process, although not as compatible with all find versions)
See notable warnings: How can I recursively delete all files of a specific extension in the current directory?
More in the GNU manual for Find, Deleting Files
Caution with mass delete: Linux has no built-in undelete
So first check what's found before deleting in batch. Hence I recommend safety measures.
- interactive instead forcefully option: for rm
-iinstead of-f - list or find first, then check, then delete (e.g. by appending pipes or first running find without action)
- using trash-folder tools that support later undo: like trash-cli or gvfs-trash
- 257
- 2
- 10