Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 02-12-2006, 10:16 AM   #1
LQ Newbie
Registered: Feb 2006
Distribution: Gentoo and Debian
Posts: 8

Rep: Reputation: 0
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.
Old 02-12-2006, 01:02 PM   #2
Registered: Jun 2002
Location: Netherlands - Amsterdam
Distribution: RedHat 9
Posts: 549

Rep: Reputation: 30
find /path/to/your/directory -name *substring*|xargs rm
Old 02-12-2006, 01:04 PM   #3
Senior Member
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 333Reputation: 333Reputation: 333Reputation: 333
Sounds like someone got their homework done for them.
Old 02-12-2006, 02:14 PM   #4
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911
find - "*substr*" -exec rm "{}" \;
would be quicker ;} ... no pipe, no xargs.


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.
Old 02-12-2006, 04:11 PM   #5
LQ Newbie
Registered: Feb 2006
Distribution: Gentoo and Debian
Posts: 8

Original Poster
Rep: Reputation: 0
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.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script to Delete line if pattern exists topcat Programming 22 08-23-2011 05:58 AM
unix shell script:How to delete the first line in a file?? rche3252 Programming 6 03-03-2010 08:32 AM
Making shell script to 'locate' m4a files and delete them bd1308 Linux - Software 3 02-12-2006 11:44 AM
delete mail via shell script kevin_obtiva Programming 1 12-10-2005 10:24 AM
bash shell script appends '?' to filenames sceptreofjudah Linux - Newbie 3 03-18-2004 09:54 AM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:48 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration