Remove contents of directory, but not directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove contents of directory, but not directory
# 8  
Old 12-04-2005
If there are a 1000 files in a directory, "rm -rf *" will not attempt to create 1000 processes. It will attempt to create a single process with a rather long command line. This will probably fail. With one million files, I believe that it is guaranteed to fail. Even if you write your own shell that can handle the command line, no kernel I know can exec() an arg list that large. A directory with one million files is a problem. It will take hours to empty that. And with most filesystems, directories grow as needed, but do not shrink. In general, emptying a directory is not wise. Remove the directory and recreate it. If the directory is super-sized, rename it, create a new empty directory, then remove the renamed directory. But this won't work with a mount point, such as /tmp. This is why your original question might make some sense.

However, additional constraints are arriving out of the blue...
"you'll still hit an error when find attempts to remove the "." directory"
"it's not easy to use from, say, an exec() call. You need the infrastructure of a shell."
So far, not only can we not use wildcards, but we must guarantee that no errors occur, it must be easy to use with exec(), and we cannot even use a shell. These are unreasonable additional contraints. The "unix way" involves connecting programs together. And yes, you should expect that the solutions we provide will require the use of a shell. Expected and harmless error messages can be ignored. Or you can use "2>/dev/null" to suppress them. If you are emptying a directory from a C program you should probably use opendir(), readdir(), and unlink() directly rather than exec'ing another program. If you must exec(), something like "ksh -c commandline" can be used with any valid command line.

For the record, my solution is:
cd directory
find . \( ! -name . -prune \) -exec rm -rf {} \+
The + with find is required by the Posix standard (unlike -delete) , but not everyone has it yet. Assuming the the user has permission to delete the files and subdirectories, this command actually may satisfy all of your currently stated constraints. If the filenames have no embedded newline characters,
cd directory
find . \( ! -name . -prune \) -print | xargs rm -rf
is nearly as good and will work with older versions of find. However it uses a pipe and thus violates your prohibition against using "infrastructure of a shell".
# 9  
Old 12-04-2005
Thanks for your solution.

Apologies if I didn't make my question clear. I was asking if there was any easy way of getting behaviour similar to a hypothetical "empty" command -- "$ empty dir" -- just a simple, sweet, atomic operation, without having to resort to something like `find', which seems, to my mind at least, to be a sledgehammer approach (I've always thought find was the antithesis of the "unix way" -- "a command should do one thing, and one thing well", but I'm not trying to start a flamewar.)

It wasn't for any particular purpose, but there are situations where deleting and recreating the directory might not be practical (with open file handles, mount points, etc.). As for not stating my command can't create errors, surely a "solution" that guarantees the occurrence of an error during execution is not the "unix way"!

Still, maybe my question was a bit misguided. It's not as if the operation of individually deleting a large number of files is particularly clean, simple or atomic, unlike unlinking a directory. I hadn't thought of the whole directory growth/shrinkage issue. Anyone with more FS expertise than me want to comment further? How do operating-systems that implement Recycle bins/trash directories/etc handle this?

Last edited by pdc; 12-04-2005 at 02:03 PM..
# 10  
Old 04-09-2009
It seems your rm may be aliased to rm -i, which overrides your -f option. Try
Code:
/usr/bin/rm -rf *

from the appropriate directory.

More safely, you can test with

Code:
prompt%> mkdir /foo
prompt%> cd /foo
prompt%> touch a.txt
prompt%> touch b.txt
prompt%> touch c.txt
prompt%> /usr/bin/rm -rf *

I realize this does not follow your constraint of not using shell wildcards, but I'm guessing that issue is caused by having to verify every delete 'interactively'
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Zipping contents without the actual directory

Hi , I want to zip files present in the directories listed under a parent directory without zipping the directory itself my parent directory path is /informatica/DGDMN/PowerCenter1011/server/infa_shared/SrcFiles/OTE/Final_Directory I have the below directories named as 1,2,3,4,5 listed... (9 Replies)
Discussion started by: paul1234
9 Replies

2. AIX

Restore directory and contents from tape

Hi, I have taken a backup of a directory on my tape in using below command cd /backup find * -print|backup -ivf '/dev/rmt0' '-U' |tee -a /syslogs/backup.log and output appear in below format. a 0 rman-before-08032014 a 58403323904... (2 Replies)
Discussion started by: m_raheelahmed
2 Replies

3. UNIX for Dummies Questions & Answers

What option will use for deleting directory with all its contents?

Hi How to completely delete directory with all it contents I try to use rmdir -r but it give error Thanks ---------- Post updated at 03:10 AM ---------- Previous update was at 02:52 AM ---------- Hi all I got the solution for my thread i use mkdir with the option -p Thanks (1 Reply)
Discussion started by: Tauatioti
1 Replies

4. Shell Programming and Scripting

Script that displays contents of a directory

Hello all! I am writing a script that takes in a directory name as input and if the directory exists, it shows the files inside the directory here is what I have so far (incomplete) (mostly like pseudocode) #/bin/sh echo Please enter the name of a directory read dir grep $dir... (2 Replies)
Discussion started by: subway69
2 Replies

5. Shell Programming and Scripting

Create a list of directory contents

Well I did a search and didn't anything for my specific case. I got a directory with a bunch of text file. All of them have the following pattern on the filename "ABCD_<As of Date>.txt" Example: ABCD_20110301.txt ABCD_20110302.txt ABCD_20110303.txt All I want to accomplish is a Korn... (3 Replies)
Discussion started by: Shark Tek
3 Replies

6. Solaris

Directory should not be deleted, But the contents can be

Hi Guys, I have an user's home directory set to /home/A And A contains the following directories B & C Is there some way in solaris by which i can prevent the directories B and C from getting deleted by the user but the contents of the directories B & C can be deleted ? Also i have... (2 Replies)
Discussion started by: localhost
2 Replies

7. UNIX for Dummies Questions & Answers

Best way to list a directory's contents?

Hey guys! I'm so glad I found this site, I've had so many questions and have been left alone for roughly a year scanning man pages but It's just not quite cutting it for some things. So, I often like to list directories when browsing around my local machine, a friend's machine, or my web... (6 Replies)
Discussion started by: bbilheimer
6 Replies

8. UNIX for Dummies Questions & Answers

list contents of directory

I want to list the contents of a directory, but I do not want to use the ls, is there another way?? (3 Replies)
Discussion started by: carl_vieyra
3 Replies

9. UNIX for Dummies Questions & Answers

Message trying to list contents of directory

I'm getting this return whenever I try to do anything on a directory root# ls -al /directory ls: .: Value too large to be stored in data type. total 0 I can change directory down two levels but can not list contents of the root of this directory. ANy one seen this? (1 Reply)
Discussion started by: sallender
1 Replies

10. UNIX for Dummies Questions & Answers

Unable to view contents of a directory

Hi, first post here be gentle. Very new to Unix. Using HP-UX 10.20 I CD into a remote directory on one machine $ cd /net/remote hostname yet when I do an ll in this directory none of the contents appear. It just is empty. when I do the same command from another machine, $ cd... (13 Replies)
Discussion started by: maddave
13 Replies
Login or Register to Ask a Question