LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-11-2012, 06:34 AM   #1
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Remove sub-directories, yet preserve others


Hi,

I'm trying to write a bash script in which I want it to remove all sub-directories except two ('core' and 'java') within a particular directory.

Here's what I got:
Code:
for dir in ${DIRS[@]}; do
    pushd ${dir} >& /dev/null
    find . -maxdepth 1 -type d \( -name . -o -name core -o -name java \) -prune -o -print0 | xargs rm -rf
    popd >& /dev/null
done
I know that the problem is with the "-name ." in the exclusion list of the find-statement above.

If I remove the "-name . -o" from the find-statement above, then I get errors such as:
Code:
xargs: WARNING: a NUL character occurred in the input.  It cannot be passed through in the argument list.  Did you mean to use the --null option?
rm: cannot remove directory: `.'
With the original statement, as I have shown above, none of the unwanted sub-directories are being removed. Can someone please help me sort this issue out?
 
Old 12-11-2012, 06:44 AM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Hi, how about
Code:
for dir in "${DIRS[@]}"; do
    find "$dir" -maxdepth 1 -mindepth 1 ! -name java ! -name core | xargs rm -rf
done
also, don't forget to quote the variables.

Last edited by millgates; 12-11-2012 at 06:46 AM.
 
1 members found this post helpful.
Old 12-11-2012, 06:55 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Well, if you put . among the directories to exclude, nothing is printed out by the find command. Actually the rm command doesn't work on . and .. since they are a sort of aliases and not the real name of a directory. Anyway, if you want to be sure use -mindepth to exclude the current working directory:
Code:
find . -mindepth 1 -maxdepth 1 -type d
Moreover, in your command line you missed the -0 option of xargs to be used in conjuction with -print0, hence the "a NUL character occurred in the input" warning message. Looking at the complete message from your second command, it shows that it cannot remove directory . but it should have removed the other ones. Anyway, here is a correct command line:
Code:
find . -mindepth 1 -maxdepth 1 -type d \( -wholename ./core -o -wholename ./java \) -prune -o -print0 | xargs -0 rm -rf
or shortly
Code:
find . -mindepth 1 -maxdepth 1 -type d \( -wholename ./core -o -wholename ./java \) -prune -o -exec rm -rf {} \;
Hope this helps.
 
1 members found this post helpful.
Old 12-11-2012, 06:58 AM   #4
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Original Poster
Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by millgates View Post
Hi, how about
Code:
for dir in "${DIRS[@]}"; do
    find "$dir" -maxdepth 1 -mindepth 1 ! -name java ! -name core | xargs rm -rf
done
also, don't forget to quote the variables.
That works... thanks!!!
 
  


Reply

Tags
bash, find


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
recursively remove directories iAlta Linux - Software 7 10-06-2007 09:25 AM
Remove directories recursively with same name cboyd Linux - Newbie 5 01-14-2007 09:48 PM
Remove directory and sub directories newuser455 Linux - Newbie 5 06-01-2005 12:31 AM
how do I remove all empty directories? noisybastard Linux - General 2 08-10-2003 11:47 PM
How to remove directories? jameshao Linux - Newbie 2 09-13-2001 02:54 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:36 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