Click to See Complete Forum and Search --> : mkdir script that will not overwrite
Davno
04-08-2007, 01:05 PM
Hi, I want to make a (mkdir and ln -s) script that i will run (on a new home directory when re-installing an os). I have many scripts that depend on previous creation of directories and symbolink link to them. Making this script is not a problem but my main concern is i want the option in the script that IF a directory already exist IT won't be overwriten, and that i don't know how to do. PS all those directories are in userspace.
Thxs :)
happybunny
04-08-2007, 04:50 PM
you could make a check first (forgive my syntax)
find /path/to/dir -f
if $? = 0; file exists, do nothing
else mkdir /path/to/dir
fi
that is horrible and wont work, but you should get the idea
bwkaz
04-08-2007, 07:55 PM
I don't think that "mkdir -p" will overwrite a directory if it exists, but I'm not sure.
You could also use test:
[ -d /home/you/whatever ] || mkdir /home/you/whatever ("test if /home/you/whatever is a directory, and if not, make it")
You can also use:
[ -L /home/you/whatever ] || ln -s /wherever /home/you/whatever to check for symlinks. (I believe "-h" works instead of "-L" in the test, also.) Also, I know that "ln -s" will fail if the symlink name already exists; you need -f to overwrite an existing link.
Davno
04-09-2007, 10:34 AM
I don't think that "mkdir -p" will overwrite a directory if it exists, but I'm not sure.
You could also use test:
[ -d /home/you/whatever ] || mkdir /home/you/whatever ("test if /home/you/whatever is a directory, and if not, make it")
You can also use:
[ -L /home/you/whatever ] || ln -s /wherever /home/you/whatever to check for symlinks. (I believe "-h" works instead of "-L" in the test, also.) Also, I know that "ln -s" will fail if the symlink name already exists; you need -f to overwrite an existing link.
Seems to work nice with mkdir, it does not overwrite the directory. :)
But the link part is different. If the link does not exist, it will create it properly but if it exist it will create another link inside the directory it was suppose to link and end up creating multitude link in a loop.
Exemple: instead of creating a link of /home/user/test to /home/user/Desktop/test It will make /home/user/test/test/test/test/.....
Hummm...
Also another question> When using kwrite to write the script the (ln) is colored in red but the option (-s) still is black (spacing is right). Is this normal or is it what could cause the above link problem ?
Ill have to read more about (find and if) and try HappyBunny suggestion for the link part later. Thxs guys
bwkaz
04-09-2007, 07:19 PM
Works for me:
$ tree Desktop test
Desktop [error opening dir]
test [error opening dir]
0 directories, 0 files
$ mkdir /home/user/Desktop
$ mkdir /home/user/Desktop/test
$ [ -L /home/user/test ] || ln -s /home/user/Desktop/test /home/user/test
$ tree Desktop test
Desktop
`-- test
test
1 directory, 0 files
$ [ -L /home/user/test ] || ln -s /home/user/Desktop/test /home/user/test
$ tree Desktop test
Desktop
`-- test
test
1 directory, 0 files In other words, after running the "test || ln" once, I have both a "test" symlink (which tree doesn't show very well) and a "Desktop/test" directory. After running the "test || ln" again, I have exactly the same setup; the ln did not run. It also did not create an infinite symlink "loop" like you described.
Make sure you got the arguments to ln and the filename argument to the -L test correct, though. The argument to the -L test should be the symlink's name (not its target), and the arguments to ln are the target first, then the link name second.
Regarding kwrite -- so what's wrong with that, anything? Just because kwrite's syntax highlighting doesn't recognize the arguments (and I'm not even sure if that's what you think is wrong, but I'm going to guess that it is) doesn't mean anything's broken. The arguments are valid.
Davno
04-09-2007, 08:13 PM
Got it now.
[ -d /home/normand/test123 ] || mkdir /home/normand/test123
[ -L /home/normand/test123 ] || ln -s /home/normand/test123 /home/normand/Desktop
The error was that i wrote (test123) at the end of the line. It would work once but the next time it would make another link in the main dir.
Thank you
bwkaz
04-10-2007, 06:39 PM
The error was that i wrote (test123) at the end of the line. It would work once but the next time it would make another link in the main dir. No, that wasn't the error. :p
If your test was working correctly, then the ln would have never run on the second try. So any changes that you made to the ln command to get it to (apparently) work can't possibly have actually fixed the issue.
However, your test is incorrect. You have to test whether the symlink name (/home/normand/Desktop/test123) is a symlink, not whether the target name (/home/normand/test123) is a symlink. The second test (which is what you have) will always fail, which means the ln will always run. If you add the Desktop in there, then you should be able to use the other version of ln. (Refer to what I did in my test. But also note that I was making a symlink from my home directory into Desktop, and you're doing it the other way around. :))
Davno
04-10-2007, 09:35 PM
Here the script and the output. It create a link icon on the desktop. and just give me a (file exist notification) when i run it a second time. Before that, it was creating an other link inside the real folder when the link already existed on the desktop and i was running the script another time.
Working script :
#!/bin/bash
#!/usr/local/bin/bash
################################################## ##########################
#
#Script to create my favorite directory and link on a new installation.
#usage
#test if /home/you/whatever is a directory, and if not, make it
#then if /home/you/whatever is a directory,make link to it on desktop
#
################################################## ##########################
[ -d /home/normand/ape ] || mkdir /home/normand/ape
[ -L /home/normand/ape ] || ln -s /home/normand/ape /home/normand/Desktop
[ -d /home/normand/mp3 ] || mkdir /home/normand/mp3
[ -L /home/normand/mp3 ] || ln -s /home/normand/mp3 /home/normand/Desktop
[ -d /home/normand/flac ] || mkdir /home/normand/flac
[ -L /home/normand/flac ] || ln -s /home/normand/flac /home/normand/Desktop
[ -d /home/normand/cd_cover ] || mkdir /home/normand/cd_cover
[ -L /home/normand/cd_cover ] || ln -s /home/normand/cd_cover /home/normand/Desktop
[ -d /home/normand/ogg ] || mkdir /home/normand/ogg
[ -L /home/normand/ogg ] || ln -s /home/normand/ogg /home/normand/Desktop
[ -d /home/normand/wav ] || mkdir /home/normand/wav
[ -L /home/normand/wav ] || ln -s /home/normand/wav /home/normand/Desktop
Result when running the script more then once when the link is on the destop already:
[normand@localhost ~]$ ndmkdir
ln: creating symbolic link `/home/normand/Desktop/ape' to `/home/normand/ape': File exists
ln: creating symbolic link `/home/normand/Desktop/mp3' to `/home/normand/mp3': File exists
ln: creating symbolic link `/home/normand/Desktop/flac' to `/home/normand/flac': File exists
ln: creating symbolic link `/home/normand/Desktop/cd_cover' to `/home/normand/cd_cover': File exists
ln: creating symbolic link `/home/normand/Desktop/ogg' to `/home/normand/ogg': File exists
ln: creating symbolic link `/home/normand/Desktop/wav' to `/home/normand/wav': File exists
I just realise that the check for a existing link in the code is not needed:
#!/bin/bash
[ -d /home/normand/test999 ] || mkdir /home/normand/test999
ln -s /home/normand/test999 /home/normand/Desktop
I end up with the same result, the first time it create it and then it just tell me its already there:
[normand@localhost ~]$ testscript
[normand@localhost ~]$ testscript
ln: creating symbolic link `/home/normand/Desktop/test999' to `/home/normand/test999': File exists
bwkaz
04-11-2007, 06:08 PM
Please just fix your "test -L" tests. Then the ln commands won't even run, and you won't get that "file exists" message either. As it is, your tests will always fail, so they're rather useless. ;)
Davno
04-11-2007, 11:39 PM
I fixed it finally. I was asking it to look for the main directory instead of /home/user/Desktop/test.
Thxs for your patience, I just started to get into scripting :) , as you can see that script is pretty basic compared to some of the stuff i see here.