Inside a directory, how can I delete files that lack any of the words specified, so that only files that contain ALL the words are left? I tried to write a simple bash shell script using grep and rm commands, but I got lost. I am totally new to Linux, any help would be appreciated
Linux: Removing files that don’t contain all the words specified
bashfilelinuxshell
Related Question
- Bash – Capturing Groups From a Grep RegEx
- Linux – What characters are forbidden in Windows and Linux directory names
- How to delete from a text file, all lines that contain a specific string
- Linux – How to recursively find and list the latest modified files in a directory with subdirectories and times
- Linux – Pseudo-terminal will not be allocated because stdin is not a terminal
- Linux – How to find all files containing specific text on Linux
Best Solution
How about:
If a file does not contain
foo
, then the first line will remove it.If a file does not contain
bar
, then the second line will remove it.Only files containing both
foo
andbar
should be leftSee also @Mykola Golubyev's post for placing in a loop.