LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Other *NIX Forums > Solaris / OpenSolaris
User Name
Password
Solaris / OpenSolaris This forum is for the discussion of Solaris, OpenSolaris, OpenIndiana, and illumos.
General Sun, SunOS and Sparc related questions also go here. Any Solaris fork or distribution is welcome.

Notices


Reply
  Search this Thread
Old 03-08-2011, 05:42 AM   #1
mahmoodn
Member
 
Registered: May 2010
Posts: 427

Rep: Reputation: 16
excluding a folder from "rm -r" in solaris


How can I remove all folders in a specific directory except one folder (solaris)?
for example I have these folders:
/home/mahmood/a
/home/mahmood/b
/home/mahmood/c
/home/mahmood/d
/home/mahmood/e

How can I use "rm -r" to remove b/ c/ d/ e/ but preserve a/ ??
there is no --exclude-dir o something similar.
 
Old 03-08-2011, 05:55 AM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
How about something like 'find /<some_dir> ! -path "<exclude_dir_glob>" | xargs -Ideleteme rm -rf deleteme' ? .. (not tested)

cheers
 
Old 03-08-2011, 08:05 AM   #3
mahmoodn
Member
 
Registered: May 2010
Posts: 427

Original Poster
Rep: Reputation: 16
Code:
bash-3.00# ls
a  b  c  d  e
bash-3.00# find ./ ! -path "a" | xargs -Ideleteme rm -rf deleteme 
find: bad option -path
find: [-H | -L] path-list predicate-list
bash-3.00#
 
Old 03-08-2011, 08:22 AM   #4
mahmoodn
Member
 
Registered: May 2010
Posts: 427

Original Poster
Rep: Reputation: 16
I found it
Code:
find . ! -name "a" -exec rm -rf {} +
will remove b, c, d, e and preserve a
 
Old 03-08-2011, 08:25 AM   #5
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
Indeed the -path predicate is not available in Solaris, but you can use -name as well and the -prune predicate to exclude an entire branch of the directory tree:
Code:
find /home/mahmood/* -type d -name a -prune -o -exec echo rm -r {} \;

Last edited by colucix; 03-08-2011 at 04:45 PM.
 
Old 03-08-2011, 11:27 AM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Originally Posted by mahmoodn View Post
I found it
Code:
find . ! -name "a" -exec rm -rf {} +
will remove b, c, d, e and preserve a
Beware that it will remove any file and directory under a.
 
Old 03-08-2011, 04:45 PM   #7
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
Quote:
Originally Posted by jlliagre View Post
Beware that it will remove any file and directory under a.
Good catch, jlliagre! I was testing with an empty directory. At this point I think the -prune solution is the only way to achieve the task.
 
Old 03-11-2011, 10:42 PM   #8
mahmoodn
Member
 
Registered: May 2010
Posts: 427

Original Poster
Rep: Reputation: 16
I din't receive any emails afte my last post...

Quote:
Originally Posted by jlliagre View Post
Beware that it will remove any file and directory under a.
Yes you are right

Quote:
Originally Posted by colucix View Post
Indeed the -path predicate is not available in Solaris, but you can use -name as well and the -prune predicate to exclude an entire branch of the directory tree:
Code:
find /home/mahmood/* -type d -name a -prune -o -exec echo rm -r {} \;
This is the best command. Thanks
 
Old 03-13-2011, 12:53 PM   #9
Blinker_Fluid
Member
 
Registered: Jul 2003
Location: Clinging to my guns and religion.
Posts: 683

Rep: Reputation: 63
you guys are overthinking this...
rm -rf /home/mahmood/[bcde]
 
  


Reply



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
[SOLVED] There is no "menu.lst" in my Ubuntu "/boot/grub/" folder. msbstar Linux - Newbie 18 09-15-2012 01:26 PM
How does one "zip" a folder with Linux .? How does Windows user "unzip" same ? brjoon1021 Linux - Software 5 12-19-2010 12:10 AM
BASH:find out if volume "foo"/folder "goo" can be written to SilversleevesX Programming 2 08-28-2010 10:03 AM
After installing XAMPP for linux i have seen the folder "htdocs" belongs to "nobody". tirengarfio Linux - General 6 03-17-2010 01:41 PM
"rm -rf /tmp", now Evolution shows "(no folder displayed" Ed-MtnBiker Linux - Software 7 04-10-2006 04:05 PM

LinuxQuestions.org > Forums > Other *NIX Forums > Solaris / OpenSolaris

All times are GMT -5. The time now is 02:13 AM.

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