Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What is the code to make a script run on startup that binds the home folders with other folders depending on if a drive is present?

Because most of my files are kept in separate drives and places than /home/John/Folder, I use Terminal code to bind the Home folders for different places. For example, I bind the /Documents folder with /Dropbox/Documents so it's constantly backed up. I've found out how to do this through the terminal, and read somewhere that you can put code in the /etc/rc.local script and that that script (rc.local) runs on start up. I've put the code into that script, but I'm not sure how to get that to run on startup. Right now I have to wait till the computer has started up, then run the code in the terminal. The full code in that script I've set is below.

sudo mount -o bind /media/Storage/Music ~/Music && sudo mount -o bind /media/storage/downloads ~/Downloads && sudo mount -o bind /media/2TB/Videos ~/Videos && sudo mount -o bind /home/john/dropbox/documents ~/Documents && sudo mount -o bind /home/john/dropbox/Photos ~/Pictures && exit 0

The exit 0 is needed for the script to run, according to a comment in the script.

Since this on a laptop and I keep my video files on an external drive, is there anyway to add a simple "If is present, also bind ~/Videos to /media//Videos," and if it's not, skip over it? Or put that code at the end so it doesn't affect the above execution?

share|improve this question
up vote 2 down vote accepted

There are multiple issues with your script:

  1. You are chaining (&&) unrelated commands, a failure on one will skip the rest.
  2. You are using "~" in the path name, the script is invoked with root, whicih means it will be translated into /root, you must use the full path name
  3. You don't need to use sudo because it's running with root.

The folllowing script will do what you have requested:

[[ -d /media/Storage/Music ]] &&  mount -o bind /media/Storage/Music /home/your_user/Music 
[[ -d /media/storage/downloads ]] && mount -o bind /media/storage/downloads /home/your_user/Downloads
[[ -d /media/2TB/Videos ]] && mount -o bind /media/2TB/Videos /home/your_user/Videos 
mount -o bind /home/john/dropbox/documents /home/your_user/Documents 
mount -o bind /home/john/dropbox/Photos /home/your_user/Pictures
exit 0
share|improve this answer
Thanks a lot, this worked perfectly! I feel silly now. – John Feb 6 '11 at 17:38

In a BASH script...

#check for existence of a directory
if [ ! -d /media/Videos ]; then
    // do nothing or echo "not available"
    // action

#check for existence of a file
if [ ! -f /media/Videos/i_am_mounted ]; then
    // do nothing or echo "not available"
    // action

You can also stop using the && this way. (If you have command1 && command2, command2 is executed if and only if command1 exited with exit code 0 (success).) And you have some local, some network. The network portion might belong in scripts placed in /etc/network/if-up.d and /etc/network/if-down.d. The local portion might belong in /etc/rc.local - to stop using sudo command.

share|improve this answer
There is an error on your script, unless I got it wrong, the idea is to perform the actions when the dir or file are available, which means you want to use if [ -d, without the negation (!) . – João Pinto Feb 3 '11 at 18:56
Eh - well, there's no concrete answer. Replace the 'fi' with 'else' and then perform the actions there. – user8290 Feb 3 '11 at 19:05
Thank you for your help. :3 – John Feb 6 '11 at 17:38

Your Answer


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.