Remove contents of directory, but not directory | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Remove contents of directory, but not directory

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-03-2005
pdc pdc is offline
Registered User
 
Join Date: Dec 2005
Last Activity: 12 December 2005, 2:14 AM EST
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Remove contents of directory, but not directory

What's the best way to delete everything in a directory, but not the directory itself, without using shell wildcards?
Sponsored Links
    #2  
Old 12-03-2005
BOFH BOFH is offline Forum Advisor  
Registered User
 
Join Date: Feb 2005
Last Activity: 7 May 2012, 4:35 PM EDT
Location: Longmont, CO
Posts: 411
Thanks: 1
Thanked 5 Times in 5 Posts
Quote:
Originally Posted by pdc
What's the best way to delete everything in a directory, but not the directory itself, without using shell wildcards?
Why can't you use shell wildcards?

Carl
Sponsored Links
    #3  
Old 12-03-2005
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
AFK
 
Join Date: Dec 2004
Last Activity: 28 July 2014, 9:39 AM EDT
Location: UK
Posts: 2,351
Thanks: 0
Thanked 6 Times in 6 Posts
Looks like homework to me, but here's a thought: why dont you look up the man pages of ls and for loop in sh/ksh.
    #4  
Old 12-03-2005
pdc pdc is offline
Registered User
 
Join Date: Dec 2005
Last Activity: 12 December 2005, 2:14 AM EST
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
It's not homework. I'm looking to know mostly out of curiosity, but you could argue that using wildcards is bad anyway. If there's a million files in the directory, that's a million rm processes (obviously not all simultaneous, but still). It's not bullet-proof:

mercury-2:~:0 mkdir foo
mercury-2:~:0 touch foo/.bar
mercury-2:~:0 rm -rf /foo/*
zsh: sure you want to delete all the files in /foo [yn]? y
zsh: no matches found: /foo/*
mercury-2:~:1


On top of that, it's not easy to use from, say, an exec() call. You need the infrastructure of a shell.

So my question stands.
Sponsored Links
    #5  
Old 12-03-2005
ppierald ppierald is offline
Registered User
 
Join Date: Nov 2005
Last Activity: 6 February 2006, 6:54 PM EST
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
find . -type f -exec rm {} \; -print
Sponsored Links
    #6  
Old 12-03-2005
pdc pdc is offline
Registered User
 
Join Date: Dec 2005
Last Activity: 12 December 2005, 2:14 AM EST
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Except that won't remove any subdirectories. Removing the "-type f" and specifying "rm -r" fixes this issue... but you'll still hit an error when find attempts to remove the "." directory. Incidentally, "find . -delete" is probably a nicer way of coding this.

Anyone else got suggestions? It seems like the kind of thing their should be a command, or flag to rm, for: a simple way to empty a directory.

Last edited by pdc; 12-03-2005 at 09:08 PM..
Sponsored Links
    #7  
Old 12-04-2005
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
AFK
 
Join Date: Dec 2004
Last Activity: 28 July 2014, 9:39 AM EDT
Location: UK
Posts: 2,351
Thanks: 0
Thanked 6 Times in 6 Posts
Quote:
Originally Posted by pdc
It's not homework. I'm looking to know mostly out of curiosity, but you could argue that using wildcards is bad anyway. If there's a million files in the directory, that's a million rm processes (obviously not all simultaneous, but still). It's not bullet-proof:

/*skipped the code*./

On top of that, it's not easy to use from, say, an exec() call. You need the infrastructure of a shell.

So my question stands.
I dont think you have that right.. a million processes is not a million rm processes. Its all those million files as arguments to one rm process. Now that might fail due to too many arguments (any command can handle only a finite number).
I will give you your second argument about not being easy to use from an exec() call.

Why don't you use a slightly modified form of the find command that ppierald gave:

Code:
find . -exec rm -rf {} \;

That, by the way, would be a million rm processes (one for each file/directory that the find command would output).
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Directory should not be deleted, But the contents can be localhost Solaris 2 12-16-2008 10:54 AM
Best way to list a directory's contents? bbilheimer UNIX for Dummies Questions & Answers 6 02-05-2008 09:05 AM
list contents of directory carl_vieyra UNIX for Dummies Questions & Answers 3 01-30-2007 02:24 PM
Message trying to list contents of directory sallender UNIX for Dummies Questions & Answers 1 10-19-2005 08:22 AM
Unable to view contents of a directory maddave UNIX for Dummies Questions & Answers 13 10-25-2001 01:50 AM



All times are GMT -4. The time now is 10:22 PM.