I'm running eclipse on windows and would like to automatically backup my src direcories for all my projects. I installed cygwin and tried to make a simple backup script but I am having trouble properly specifying the destination filename. The backups should be in the current directory.directory of the script.


for f in /cygdrive/c/Documents\ and\ Settings/USERNAME/My\ Documents/Eclipse\ Workspace/*
        echo "in folder \"$f\""
        cp -a "$f"/src/ ./????.bak


I would like ???? to be the project folder (not the absolute path). Thanks.

  • 10
    rsync is a good tool to maintain backups. It only copies the files which are changed. instead of the whole bunch of files. linux.die.net/man/1/rsync – Pitt Oct 24 '12 at 16:00
  • There are also incremental backup tools (based on rsync) like e.g. rdiff-backup. But rather follow @Gilles's and @sparticvs's advice: revision control! – fheub Oct 25 '12 at 6:36

I think the shell construct you're looking for is dirname.

for f in /cygdrive/c/Documents\ and\ Settings/USERNAME/My\ Documents/Eclipse\ Workspace/*
    cp -a "$f/src/" "./$(dirname "$f").bak"

But really, this is not a good backup solution. Every time you run the copy, the previous state is erased. And it doesn't provide any history at all. Use version control. Use anything, even CVS (the useful functions of which can be learned in under an hour). Whichever version control system you choose:

  1. Create a repository.
  2. Check in or import your existing files.
  3. Whenever you've done a little chunk of work, commit.
  • 2
    People only start using version control after a bad experience with a backup script :) – Bernhard Oct 25 '12 at 5:49
  • I would normally set up a git repo but I wanted to practice my shell scripting. – vavix Oct 25 '12 at 23:30
  • in the end I settled with cp -a "$f"/src/ "./backup/$(basename "$f").bak" – vavix Oct 25 '12 at 23:51

I would recommend using a source code management system like git or hg (mercurial). If they are private projects BitBucket has a nice private project storage for free (given its less than 10 people on the project). Git works well on Windows and there is a plugin for eclipse called EGIT which would allow you to sync to a repo elsewhere. For programming, I would definitely recommend this over writing backup scripts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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