Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm currently writing a start script for a minecraft game server. In this script I included a backup script.

This backup script works almost perfect but instead of deleting a folders contents it just creates another one (similarilly named) and does not do anything with the targeted folder.

I'm using this command: rm -rf "$MINECRAFT_PATH/server.log backups/*"
This command is executed in a bash script.

The variable $MINECRAFT_PATH does contain the correct path. The folder created has this name: server.log.backups (Note the dot between log and backups). But the targeted folder has this name: server.log backups. This folder does exist and this is not the problem.

share|improve this question
3  
rm cannot create a directory. Double quotes prevent the wildcard expansion. –  choroba Aug 20 '13 at 15:21
    
But it somehow was created! I still don't understand it! –  BrainStone Aug 20 '13 at 15:26
    
It was probably created earlier in the scripter. –  choroba Aug 20 '13 at 15:33
    
The weird thing about this is that now the folder is not created! (I changed the script according to the solution.) –  BrainStone Aug 20 '13 at 15:40

1 Answer 1

up vote 2 down vote accepted

Double quotes in Bash preserve most literal values of the content, so your * glob is passed literally to rm, which will try to remove a file or folder called *. The $MINECRAFT_PATH is substituted though.

What you probably want is:

rm -rf "$MINECRAFT_PATH/server.log backups/"*

This will delete the contents of the server.log backups folder.

Some alternatives:

  • You could program your script more defensively, thus removing the -f option for rm. In that case, it would fail if the server.log backups folder was empty, because the glob would not be expanded and passed on literally, and there would be no file called server.log backups/*.

  • You could simply rm -rf the entire folder and mkdir -p it afterwards.

  • Or, you could also simply do a depth-first deletion of all the files and folders inside the server.log backups folder. -mindepth 1 prevents the parent from being deleted:

    find "$MINECRAFT_PATH/server.log backups" -mindepth 1 -depth -delete
    
share|improve this answer
    
The folder itself got removed too! –  BrainStone Aug 20 '13 at 15:39
    
I cannot follow, if the folder is empty, the * should be passed to the command directly: mkdir foo; echo foo/* returns foo/*. So rm foo/* should try to delete the file * inside foo, which does not exist, but doesn't return an error neither due to -f. But the folder foo/ shouldn't be removed. (At least with default bash 4 on my debian box, is this different on OSX?) –  mpy Aug 21 '13 at 8:00
    
@mpy You're right that this shouldn't happen. I was wrong here. However, it also depends on the shell on what exact error is being thrown. Ideally, the script should be programmed defensively without using the -f option for rm to see if something goes wrong. –  slhck Aug 21 '13 at 8:05
    
@BrainStone Could you check the update to my answer? –  slhck Aug 21 '13 at 8:14
    
I double checked it. And it looks like I forgot the *. Now it works. Also thank you for the expanded answer! –  BrainStone Aug 21 '13 at 11:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.