0

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.

4
  • 3
    rm cannot create a directory. Double quotes prevent the wildcard expansion.
    – choroba
    Commented Aug 20, 2013 at 15:21
  • But it somehow was created! I still don't understand it!
    – BrainStone
    Commented Aug 20, 2013 at 15:26
  • It was probably created earlier in the scripter.
    – choroba
    Commented Aug 20, 2013 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
    Commented Aug 20, 2013 at 15:40

1 Answer 1

3

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
    
5
  • The folder itself got removed too!
    – BrainStone
    Commented Aug 20, 2013 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
    Commented Aug 21, 2013 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
    Commented Aug 21, 2013 at 8:05
  • @BrainStone Could you check the update to my answer?
    – slhck
    Commented Aug 21, 2013 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
    Commented Aug 21, 2013 at 11:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .