LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 02-12-2006, 09:16 AM   #1
Drack
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, 12:02 PM   #2
pk21
Member
 
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, 12:04 PM   #3
macemoneta
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: 326Reputation: 326Reputation: 326Reputation: 326
Sounds like someone got their homework done for them.
 
Old 02-12-2006, 01:14 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,965
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
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.
 
Old 02-12-2006, 03:11 PM   #5
Drack
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.
 
  


Reply


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 04:58 AM
unix shell script:How to delete the first line in a file?? rche3252 Programming 6 03-03-2010 07:32 AM
Making shell script to 'locate' m4a files and delete them bd1308 Linux - Software 3 02-12-2006 10:44 AM
delete mail via shell script kevin_obtiva Programming 1 12-10-2005 09:24 AM
bash shell script appends '?' to filenames sceptreofjudah Linux - Newbie 3 03-18-2004 08:54 AM


All times are GMT -5. The time now is 03:50 PM.

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