LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 09-03-2008, 02:31 PM   #1
ufmale
Member
 
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.

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

Code:
$ 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
BrianK
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:

Code:
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
CRC123
Member
 
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:

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

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

Try this instead:

Code:
rm -rf `find ./ -name '*_1'`
or
Code:
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:

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

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

Code:
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.
 
  


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 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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

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