removing the files but not the directory


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers removing the files but not the directory
# 1  
Old 04-09-2009
Bug removing the files but not the directory

How would i rmeove all the files in the directory but still keep the directory?
# 2  
Old 04-10-2009
try this
Code:
cd dirname
rm *

# 3  
Old 04-10-2009
more specific
Code:
find . -type d -name '*' -print | xargs rm -i

# 4  
Old 04-10-2009
why not this ?

Code:
cd dirname
find . -type f | xargs rm -f

this will clean all the files & the directory structure will remain intact.

otherwise just use
Code:
cd dirname; rm -rf *


Last edited by nirbhay; 04-10-2009 at 12:06 PM.. Reason: more precise
# 5  
Old 04-10-2009
or. . . . . .

Code:
/bin/rm dir_nm/*

or....

Code:
cd dir
for file in * ; do

  if [ -f $file ]; then
    /bin/rm $file
  fi

done

just addressing the fact that he didn't say "recursively".
# 6  
Old 04-11-2009
If you want to delete everything in the directory, including subdirectories, use

rm -r dirname/*

(This works in most shells, using basic pattern matching. '*' is a wildcard)

If you don't want to delete subdirectories, omit the '-r' flag.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compressing & removing files in a directory & subdirectory

Hi, I want a simple line of code that will compress files within a directory specified (parameter) and its subdirectories and also i want to remove files which are exactly 365 days old from the sysdate after this compression. Please help. Thanks, JD (8 Replies)
Discussion started by: Jesshelle David
8 Replies

2. UNIX for Dummies Questions & Answers

Removing directory with leading hyphen from root directory

I know that this basic question has been asked many times and solutions all over the internet, but none of the are working for me. I have a directory in the root directory, named "-p". # ls -l / total 198 <snip> drwxr-xr-x 4 root root 4096 Dec 3 14:18 opt drwxr-xr-x 2 root ... (2 Replies)
Discussion started by: edstevens
2 Replies

3. UNIX for Dummies Questions & Answers

Removing a range of files in a directory..

Hi all, Disclosure: I am very new to Unix, but eager to learn.. I've been tasked with transferring logs to a remote server. After I've verified these logs have transferred correctly I have to remove the source files. The naming scheme is: /directory/2012.05.01 /directory/2012.05.02 ..and... (1 Reply)
Discussion started by: JD3V
1 Replies

4. Shell Programming and Scripting

help with removing files from home directory

hey there folks! I cant figure out, for the life of me, how to procede in removing alll the files in my home directory that are not owned by me. would i have to list them, but after that what do i do. or is there some way I am not aware of. my employer heard i could script in unix, but i havent... (3 Replies)
Discussion started by: Ginkosu
3 Replies

5. Shell Programming and Scripting

Removing files older than one week in a directory

Hi, I need a shell script to remove the files older than a week in a directoy and if necessary to zip the files. (2 Replies)
Discussion started by: sudhakaryadav
2 Replies

6. Shell Programming and Scripting

removing old files from client directory: awk

I would help with removing old files from client directory and put the new files in there............. Below code gives me wrong outout, it's supposed to delete old files from client directory ............................ am I missing anything ? {removing *** Hash Passive mode: off;... (2 Replies)
Discussion started by: sambakamba
2 Replies

7. Filesystems, Disks and Memory

Removing a file from a directory

How do you remove a file named '-'? ('rm' does not work) (5 Replies)
Discussion started by: npatel
5 Replies

8. UNIX Desktop Questions & Answers

Problem Removing a Directory !!!

Hi All, When i remove a directory , the following error message comes : rm: cannot determine if this is an ancestor of the current working directory Is it something to do with the permissisons ? The path is /PBAMLftp/incoming/misc/July06 - (where i need to remove the July06 directory) And... (1 Reply)
Discussion started by: gauravsachan
1 Replies

9. Shell Programming and Scripting

Removing files automatically from a directory after 30 days.

Hello; I have a directory that is collecting log and act files. I need to write a script that will remove these files once they are 30 days old. I have read through a number of threads on this site that have given me a great deal of information. However I have what seems to be a unique... (7 Replies)
Discussion started by: justinb_155
7 Replies

10. UNIX for Dummies Questions & Answers

removing a directory

all - I'm trying to delete this directory with no success. This was placed in my folder by a test script I wrote for beginners... drwxr-xr-x 2 myaddress ssusr 512 Dec 22 16:10 $x+1 Here is the message I receive - I've tried every command possible to remove it: rmdir:... (7 Replies)
Discussion started by: da_curtain
7 Replies
Login or Register to Ask a Question