Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 09-03-2008, 02:31 PM   #1
Registered: Feb 2007
Posts: 386

Rep: Reputation: 30
delete certain subdirectory based on name

I am trying to delete certain directories based on its name
here I search the dir that i want to delete.

[ch@ad an4]$ find ./ -name '*_1'
Now, i want to delete them, so I tried

$ find ./ -name '*_1' | rm -rf
Well, it does not work. Is there a correct pipe syntax that I should do?
Old 09-03-2008, 02:34 PM   #2
Senior Member
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
you can use the exec option of find, so:

find ./ -name '*_l' -exec rm -rfv {} \;
(note that I added the -v option - you may or may not want that)

should do it.

edited to add:
If you're looking for how to use rm with find but without using -exec, you'd do something like:

rm `find . -name 'foo*'`

the backticks say to evaluate the expression & use the return as args.

Last edited by BrianK; 09-03-2008 at 02:36 PM.
Old 09-03-2008, 02:36 PM   #3
Registered: Aug 2008
Distribution: opensuse, RHEL
Posts: 374
Blog Entries: 1

Rep: Reputation: 32
I don't know if 'rm' command takes input from pipes.

Try this instead:

rm -rf `find ./ -name '*_1'`
rm -rf $(find ./ -name '*_1')
Old 09-03-2008, 09:06 PM   #4
Gentoo support team
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 404Reputation: 404Reputation: 404Reputation: 404Reputation: 404
Originally Posted by CRC123 View Post
I don't know if 'rm' command takes input from pipes.

Try this instead:

rm -rf `find ./ -name '*_1'`
rm -rf $(find ./ -name '*_1')
This can lead to potential problems, overall if the list is long enough.

BrianK's way is the correct one. Since find can do it all, let's use it. However, I would also add single quotation marks around the curly brackets:

find ./ -name '*_l' -exec rm -rfv '{}' \;
If you want to use pipes, use xargs:

find ./ -name '*_1' | xargs rm -rf
Or even

find ./ -name '*_1' | while read file; do rm -rf "$file"; done
Or even use redirection, however there's really no need to overcomplicate the things.

Last edited by i92guboj; 09-03-2008 at 09:08 PM.


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 Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Script to delete mail based on date gquiring Linux - General 8 05-08-2013 09:24 AM
Delete files based on current date -1 Winanjaya Linux - Newbie 3 06-23-2008 04:38 AM
delete files based on keywords pinoyskull Linux - Newbie 17 07-03-2007 01:44 PM
Delete files based on date stefaandk Linux - General 3 06-17-2005 02:20 AM
cron job to delete files based on attributes alpha21 Linux - General 3 11-09-2004 12:06 PM > Forums > Linux Forums > Linux - Newbie

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

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
Open Source Consulting | Domain Registration