Register
It is currently Wed Apr 23, 2014 11:26 pm

Delete a folder/directories contents?


All times are UTC - 6 hours


Post new topic Reply to topic  [ 7 posts ] 
Author Message
 PostPosted: Fri May 18, 2007 5:45 am   

Joined: Wed May 09, 2007 4:07 pm
Posts: 6
Hi I want to be able to delete all the files in a directory but still retain the directory i can't seem to figure out how to give the fm command a complete wildcard for the file name.

Or is there another/better way.


Top
 Profile  
 PostPosted: Fri May 18, 2007 5:53 am   

Joined: Wed May 09, 2007 4:07 pm
Posts: 6
OK I found that

rm ./*


will do it but I think that may be a little scary as what happens for example here...


rm /Uses/Shared* *Items/Backups/./*


I basically want to remove the contents of the "/Uses/Shared* *Items/Backups" folder but if the folder is not there or nothing in it for the script to stop.

hope that better explains my requirement - its a weekly cleanup script to remove the previous backups


Top
 Profile  
 PostPosted: Fri May 18, 2007 7:59 am   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Yeah, "rm ./*" scares me a lot, because I've seen a lot of people forget the "." and you can imagine what happens then... especially if you use the "-rf" flags...

I'm a little confused though. is "/Uses/Shared* *Items/Backups" a single folder?

Basically, what's the full path to the folder you want to look in, and what do you expect to find there?


Top
 Profile YIM  
 PostPosted: Fri May 18, 2007 10:01 pm   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
i personally would never suggest that command in a script.
if you want to delete the contents of a folder... specify the exact path or at the very least, use this one:
Code:
cd <dir>
rm *

no / to forget the . for :wink:


Top
 Profile  
 PostPosted: Thu May 31, 2007 4:00 am   

Joined: Wed May 09, 2007 4:07 pm
Posts: 6
OK

I want to delete the contents of a directory it is the resulting Backups folder below but I want to keep the Folder.

/Users/Shared Items/Backups


Whats the safest way to do this, i.e if it doesn't exist exit my script if it does then delete everything in there.

Also this applies to something else I want to do...

I want to delete a specific file if it exists, if it doesn't I want it to continue with the rest of my script so like this...

rm /Backup/archive.zip

#if file doesn't exist still carry on with my script.


Top
 Profile  
 PostPosted: Thu May 31, 2007 8:49 am   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
digitaljunkie wrote:
OK

I want to delete the contents of a directory it is the resulting Backups folder below but I want to keep the Folder.

/Users/Shared Items/Backups


Whats the safest way to do this, i.e if it doesn't exist exit my script if it does then delete everything in there.

Also this applies to something else I want to do...

I want to delete a specific file if it exists, if it doesn't I want it to continue with the rest of my script so like this...

rm /Backup/archive.zip

#if file doesn't exist still carry on with my script.


Right on, thanks for the clarification!

Okay, for your file that you want to delete "if it exists" you'll want to use "if" with the "-e" (exists) OR the "-f" (is a file). -e should work fine in this case:

Code:
#!/bin/bash

## Let's make the file name into a variable (Makes it
## easier to adapt this script to other purposes later)
FILE="/Backup/archive.zip"

## Let's check to see if the file exists
if [ -e "$FILE" ]; then
  ## remove it if it does exist
  rm -v $FILE
else
  ## or say that it doesn't exist
  echo "$FILE doesn't exist"
fi



For your original question, removing all the files in a directory, you'll want to do some checks to make sure the script doesn't do anything silly, like rm -rf /* (I once wrote a script that was supposed to put directories in a variable, but I made a typo. the script assumed that since the variable was empty, I meant "/"... it was ugly... almost got fired over it...). There

Code:
#!/bin/bash

## Let's make the directory a variable (Makes it
## easier to adapt this script to other purposes later)
DIR="/Users/Shared Items/Backups"

## Let's make sure that the directory exists
## AND that it's a directory (Probably overkill,
## maybe unnecessary)
if [ -e "$DIR" ] && [ -d "$DIR" ]; then
  ## remove everything in the directory
  for i in `ls "$DIR"`; do
    rm -v "$DIR"/"$i"
  done
else
  ## or say that the dir doesn't exist
  echo ""$DIR" doesn't exist"
fi



That should do it... I didn't add a check to see if the directory was empty, so if there's nothing there, you get nothing returned... might add that just for the sake of completeness...


Top
 Profile YIM  
 PostPosted: Tue Jun 05, 2007 3:55 am   

Joined: Wed May 09, 2007 4:07 pm
Posts: 6
Coolio Thanks very much, now I have appended it to an existing script as follows...


Code:
#!/bin/bash

## Let's make the file name into a variable (Makes it
## easier to adapt this script to other purposes later)
FILE="/ClientBackup/archive.zip"

## Let's check to see if the file exists
if [ -e "$FILE" ]; then
  ## remove it if it does exist
  rm -v $FILE
else
  ## or say that it doesn't exist
  echo "$FILE doesn't exist"
fi


# Now we will backup the files to the Clients SharePoint

# only change the next two lines of this script to suit your installation

source="/Library/FileMaker Server/Data/Backups/"
destination="/ClientBackup/"

# source defines the folder you want to archive. Only archive
# FileMaker Server backups, not the master files hosted by FMServer.

# destination is the place you want the archives sent
# ( best this be a separate volume )

# suffix is the time stamp to append to the destination

suffix="archive.zip"

# create symlink files, in the tmp folder, to deal with file paths with spaces

sourcelink="/tmp/sumware001.ln"

ln -s "$source" $sourcelink

destinationlink="/tmp/sumware002.ln"

ln -s "$destination$suffix" $destinationlink

# copy with pkzip compression and preserve resources

ditto -c -k --rsrc $sourcelink $destinationlink

# clean up the symlinks we created earlier

rm -f $sourcelink
rm -f $destinationlink



What I want to know is...

1) Can I remove the else statement from the initial script so that if the file exists it deletes it, but regardless it will still do the backup?

2) If "destination" doesn't exist can I create it before setting it as a variable? (pointless but a good learning excercise for something else I want to do)

3) If the script fails can one get it to email me? (complex probably but just a thought), also I notice the use of symbolic links previously I have commented out spaces using * * but would really love to understand SymLinks and how to use them, where could I learn more about them?

Going back to the emptying a directory could I and if so be so bold as to ask how...
Delete all contents of a directory safely that are over a week old?

Many Thanks


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP