user2370590
user2370590

Reputation: 847

what is the diff between rm -rf $TIGER/${LION}/${RABBIT}/* and rm -rf $TIGER/${LION}/${RABBIT}?

I need to understand the diff between rm -rf $TIGER/${LION}/${RABBIT}/* and rm -rf $TIGER/${LION}/${RABBIT} so that putting this in script won't produce the disaster making it to delete everything it can from root in case that the variables are not set. what is the safe way to use rm -rf in csh/ksh?

Thanks for help !!

Upvotes: 0

Views: 2647

Answers (3)

suspectus
suspectus

Reputation: 17258

This recursively deletes all non-hidden files inside the ${RABBIT} directory - ${RABBIT} directory is not deleted:

 rm -rf $TIGER/${LION}/${RABBIT}/* 

Note hidden files (aka dot files) have filenames beginning with .. These are not matched with typical * expansion unless shell dotglob option is set.

So to delete all files (including hidden files) you could use shopt thus:

 shopt -s dotglob                   # turns shell option dotglob ON
 rm -rf $TIGER/${LION}/${RABBIT}/*  # Now deletes all (including hidden) files
 shopt -u dotglob                   # FYI - unsets or turns  dotglob OFF

This recursively deletes everything including the ${RABBIT} directory.

 rm -rf $TIGER/${LION}/${RABBIT}

Upvotes: 4

Meet Mehta
Meet Mehta

Reputation: 4909

putting /* at the end will delete contents inside that directory

while only "/" will delete the directory itself as well as contents inside it.

Upvotes: 0

Charles Duffy
Charles Duffy

Reputation: 295272

Either of those commands would create a disaster if the variables were all unset; they differ only in whether they delete the directory itself, or its non-hidden contents.

If you want to be safe against deleting recursively from the root directory, explicitly test for that case and cancel:

[[ $TIGER && $LION && $RABBIT ]] || {
  echo "TIGER, LION and RABBIT must all be set; script exiting"
  exit 1
}
rm -rf ...

Upvotes: 8

Related Questions