LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Shell script: Delete filenames containing a substring? (https://www.linuxquestions.org/questions/linux-general-1/shell-script-delete-filenames-containing-a-substring-414525/)

Drack 02-12-2006 09:16 AM

Shell script: Delete filenames containing a substring?
 
Hello, I've got a great idea for a shell script but I don't know how to implement it. I'd like to make a shell script that walks through a directory and though its subdirectories and deletes all files with a specified substring in their file name.

I've read a bit about shell scripts, I'm sure bash could do this easily.

pk21 02-12-2006 12:02 PM

find /path/to/your/directory -name *substring*|xargs rm

macemoneta 02-12-2006 12:04 PM

Sounds like someone got their homework done for them. :)

Tinkster 02-12-2006 01:14 PM

And
find - "*substr*" -exec rm "{}" \;
would be quicker ;} ... no pipe, no xargs.


Cheers,
Tink


P.s.: Don't use -name arguments with wildcards without quoting!
You'll most likely get unexpected (unwanted?) results. An invocation
like find -name "*substr*" won't get globbed by the shell.

Drack 02-12-2006 03:11 PM

Thanks, works great.

Here I was thinking I would need to do a script to recursively walk through subdirs, find | grep, then rm, but it turned out to be a one liner.


All times are GMT -5. The time now is 04:50 AM.