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 04-09-2010, 06:45 AM   #1
pinga123
Member
 
Registered: Sep 2009
Posts: 684
Blog Entries: 2

Rep: Reputation: 36
Rm command help


How would i delete all the folders access one i specify.

for example if a there are 4 folders namely a b c and d.

How would i prevent deleting folder b .Whatever folders other than b should be deleted?
 
Old 04-09-2010, 07:05 AM   #2
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
Code:
ls -l | grep '^d'  | awk '{print $NF}' | grep -v '\b<dir_to_skip>\b' | xargs -i rm -rf {}
 
1 members found this post helpful.
Old 04-09-2010, 07:23 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,438

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
Code:
ls -l | grep "^d" | awk '!/^b$/{print "rm -rf "$0}'
 
1 members found this post helpful.
Old 04-09-2010, 07:30 AM   #4
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
grail option to use awk to skip the dir is more elegant,


Code:
ls -l | grep "^d" | awk '!/^b$/{print "rm -rf "$NF}'

@grail

My understanding says $0 should be $NF.
 
1 members found this post helpful.
Old 04-09-2010, 08:23 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,438

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
@PMP 100% correct ... that will teach me for fudging my testing, also forgot to put the pipe in to get it to actually
do the task as well (must have been asleep )

Code:
ls -l | grep "^d" | awk '!/^b$/{print | "rm -rf "$NF}'
 
1 members found this post helpful.
Old 04-09-2010, 08:36 AM   #6
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
I thought you skipped it intentionally just to avoid accidental deleting of directories
 
1 members found this post helpful.
Old 04-09-2010, 10:12 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,438

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
Yes I did with my testing (as I need my directories ) but still not the finished product for the OP who may not know
it was required. Thanks again for your help
 
1 members found this post helpful.
Old 04-09-2010, 06:28 PM   #8
cola
Senior Member
 
Registered: Sep 2007
Location: Dhaka,Bangladesh
Distribution: Debian
Posts: 1,031

Rep: Reputation: 63
Quote:
Originally Posted by pinga123 View Post
How would i delete all the folders access one i specify.

for example if a there are 4 folders namely a b c and d.

How would i prevent deleting folder b .Whatever folders other than b should be deleted?
Mark the thread as "[SOLVED]",this option is under the Thread Tools above.
 
1 members found this post helpful.
Old 04-12-2010, 01:00 AM   #9
pinga123
Member
 
Registered: Sep 2009
Posts: 684
Blog Entries: 2

Original Poster
Rep: Reputation: 36
Quote:
Originally Posted by grail View Post
@PMP 100% correct ... that will teach me for fudging my testing, also forgot to put the pipe in to get it to actually
do the task as well (must have been asleep )

Code:
ls -l | grep "^d" | awk '!/^b$/{print | "rm -rf "$NF}'
Sorry for the late reply .I m just curious to know what does !/^b$/ syntax do ?

what if directory name to be excluded is a variable?
How would i include that ?
 
0 members found this post helpful.
Old 04-12-2010, 01:25 AM   #10
pinga123
Member
 
Registered: Sep 2009
Posts: 684
Blog Entries: 2

Original Poster
Rep: Reputation: 36
Quote:
Originally Posted by pinga123 View Post
Sorry for the late reply .I m just curious to know what does !/^b$/ syntax do ?

what if directory name to be excluded is a variable?
How would i include that ?
It is not doing as i want it to be done.

You can see 3 directories in my system namely a b and c.
PHP Code:
[root@TESTOVM test]# dir
a  b  c 
Whenever i fire up the command mentined here i get all the directory deleted what is wrong here?
PHP Code:
[root@TESTOVM test]# ls -l | grep "^d" | awk '!/^b$/{print | "rm -rf "$NF}'
[root@TESTOVM test]# dir 
 
Old 04-12-2010, 01:34 AM   #11
pinga123
Member
 
Registered: Sep 2009
Posts: 684
Blog Entries: 2

Original Poster
Rep: Reputation: 36
I m using a simple approach here.


Quote:
ls -l | grep "^d" | awk '{print $NF}'| grep -v b | awk '{print | "rm -rf "$NF}'
Is there anything wrong here?
 
Old 04-12-2010, 01:41 AM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,438

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
Hi pinga

As you can see from my replies I ddin't really have my head on straight that day and I do apologise

The issue is that as we are using ls -l the directory name is no longer at the beginning but rather at the end, hence why we
had to use $NF in the remove, sooo, it should be:

Code:
ls -l | grep "^d" | awk '!/b$/{print | "rm -rf "$NF}'
To explain "!/b$/", this means do what is in the following braces only to the lines that do NOT (! = not)
contain a 'b' at the end of the line ($ = end of line character)

As for using variables, you would need to indicate if you want to use awk variables or bash ones, ie is this
going to be an awk script or bash script implementing awk expression inside it??
 
Old 04-12-2010, 02:40 AM   #13
bakdong
Member
 
Registered: Apr 2009
Posts: 214

Rep: Reputation: 44
Just to add a bit of variety......

find . -type d -maxdepth 1 -not -name b -exec rm {} \;
 
Old 04-12-2010, 04:32 AM   #14
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,438

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
So talk about getting side tracked where you don't need to, bakdong has probably the most direct solution.
That being said I am also going to put up one more condensed version for awk:

Code:
ls -l | awk '/^d/&&!/b$/{ print | "rm -rf "$0}'
 
  


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
how to copy drive using dd and tee command parallely? source code of dd command mdfakkeer Linux - Software 1 02-10-2010 01:31 PM
URGENT! Is there any command to get a history command lines and time in SUSE Linux.? igsoper Linux - Software 5 06-25-2009 02:14 AM
LXer: The Linux Command Shell For Beginners: Fear Not The Command Line! LXer Syndicated Linux News 0 12-22-2008 06:30 PM
Translating windows pscp command to linux scp command help robward Linux - General 2 01-17-2008 06:02 AM
startx command in FC7 reverts me back to the command prompt sriram87 Linux - Newbie 5 01-03-2008 11:18 PM


All times are GMT -5. The time now is 04:08 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration