The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > OS Specific Forums > Linux
Google UNIX.COM


Linux RedHat, Ubuntu, SUSE, Fedora, Debian, Mandriva, Slackware, Gentoo linux, PCLinuxOS. All Linux questions here!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
deleting specific lines from all files in a directory vrms UNIX for Dummies Questions & Answers 3 04-25-2008 08:08 AM
deleting empty files in a directory berlin_germany Shell Programming and Scripting 5 01-26-2007 11:47 AM
shell script: deleting files from a directory onlyc Shell Programming and Scripting 1 07-09-2006 03:41 AM
Accidentally deleting directory/files milhan UNIX for Advanced & Expert Users 4 08-27-2003 06:59 AM
Deleting A Directory! firefinger UNIX for Dummies Questions & Answers 2 10-19-2001 09:17 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-12-2008
Registered User
 

Join Date: May 2007
Posts: 54
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Question deleting only directory not files

Hi Guys,

I want to know wheather it is possible to delete directory not files,

Example:

In one directory there are 10 dirs and 100 files but my req is to delete only dirs not file

Wheather it is possible ?
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 04-12-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,203
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Code:
for d in dir/*/.; do
  rmdir "${d%/.}"
done
This may require minor modifications. ${var%suf} means remove "suf" from the value of $var -- try it with echo instead of rmdir to see what it does, and whether it's suitable for you.
Reply With Quote
  #3 (permalink)  
Old 04-13-2008
rubin's Avatar
Registered User
 

Join Date: Nov 2007
Posts: 122
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
One way of doing it:

Code:
ls -l | awk -F'[0-9][0-9]:[0-9][0-9]' '/^d/{print $NF}'| xargs -i rm -rf '{}' \;
Reply With Quote
  #4 (permalink)  
Old 04-13-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,203
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
However, it doesn't work if there are directory names with spaces in them.
Reply With Quote
  #5 (permalink)  
Old 04-13-2008
Registered User
 

Join Date: Sep 2006
Posts: 1,402
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Code:
ls -p|awk '/\/$/&&!/\./' | xargs -i rm -rf "{}"
Reply With Quote
  #6 (permalink)  
Old 04-13-2008
rubin's Avatar
Registered User
 

Join Date: Nov 2007
Posts: 122
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Hi,

Did you test it ... ? ?

- Tested on Fedora 8.
Reply With Quote
  #7 (permalink)  
Old 04-13-2008
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milan, Italy/Varna, Bulgaria
Posts: 1,320
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
What's wrong with:

Code:
rm -r */
Reply With Quote
  #8 (permalink)  
Old 04-13-2008
rubin's Avatar
Registered User
 

Join Date: Nov 2007
Posts: 122
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Quote:
Originally Posted by radoulov View Post

What's wrong with:
rm -r */

That's right, sometimes we miss the simple...
Reply With Quote
  #9 (permalink)  
Old 04-13-2008
Registered User
 

Join Date: Sep 2006
Posts: 1,402
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
yes sometimes we just miss the simplest things. however take note it still suffers from "argument too long" issues if there are too many directories ( maybe there's a workaround somewhere)
Code:
# ls -p | grep "/" | wc -l
31998
# rm -r */
bash: /bin/rm: Argument list too long
# ls -p|awk '/\/$/&&!/\./' | xargs -i rm -rf "{}"
# ls -p | grep "/" | wc -l
0
Reply With Quote
  #10 (permalink)  
Old 04-13-2008
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milan, Italy/Varna, Bulgaria
Posts: 1,320
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Quote:
Originally Posted by ghostdog74 View Post
yes sometimes we just miss the simplest things. however take note it still suffers from "argument too long" issues if there are too many directories ( maybe there's a workaround somewhere)
[...]
My post was based on the OP requirement (~10dirs).

Anyway, I don't think ls and awk are needed in this case
and I'd try to handle pathological dirnames (embedded spaces, newlines or other special characters) .
Reply With Quote
  #11 (permalink)  
Old 04-13-2008
Registered User
 

Join Date: Sep 2006
Posts: 1,402
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Quote:
Originally Posted by radoulov View Post
My post was based on the OP requirement (~10dirs).
yes, that's why rm -f */ is the simplest solution to his problem. No doubt about that.
Reply With Quote
  #12 (permalink)  
Old 04-13-2008
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milan, Italy/Varna, Bulgaria
Posts: 1,320
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Quote:
Originally Posted by ghostdog74 View Post
yes, that's why rm -f */ is the simplest solution to his problem. No doubt about that.

Just to make it clear,
I don't think ls and awk are needed in the case you describe (argument list too long).

Code:
for d in */;do rm -r "$d";done
Or better (needs to be adjusted for xargs that doesn't support the null option):

Code:
xargs -0n1000 rm -r < <(printf "%s\000" */)
If you have zsh:

Code:
autoload -U zargs
zargs *(/) -- rm -r

Last edited by radoulov; 04-13-2008 at 10:36 AM. Reason: corrected
Reply With Quote
  #13 (permalink)  
Old 05-02-2008
Registered User
 

Join Date: Apr 2008
Posts: 12
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
damn,
i was just experimenting rm -rf */
i just typed /*
Reply With Quote
  #14 (permalink)  
Old 05-02-2008
grial's Avatar
El UNIX es como un toro
 

Join Date: Jun 2006
Location: Madrid (Spain)
Posts: 531
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
find /path -type d -exec rm -rf {} \;
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 04:55 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101