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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
02-12-2006, 09:16 AM
|
#1
|
LQ Newbie
Registered: Feb 2006
Distribution: Gentoo and Debian
Posts: 8
Rep:
|
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.
|
|
|
02-12-2006, 12:02 PM
|
#2
|
Member
Registered: Jun 2002
Location: Netherlands - Amsterdam
Distribution: RedHat 9
Posts: 549
Rep:
|
find /path/to/your/directory -name *substring*|xargs rm
|
|
|
02-12-2006, 12: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
|
Sounds like someone got their homework done for them. 
|
|
|
02-12-2006, 01:14 PM
|
#4
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
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.
|
|
|
02-12-2006, 03:11 PM
|
#5
|
LQ Newbie
Registered: Feb 2006
Distribution: Gentoo and Debian
Posts: 8
Original Poster
Rep:
|
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 11:01 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|