Thursday, July 7, 2011

ln -sf gives ?[01;32mFILENAME?[0m

So I was trying to link to a few files from a script and it kept getting me these strange links instead of the actual file name. This is what I had in my shell prompt:

for i in `ls /var/www | grep FILENAME`; do ln -sf $i .; done

[The grep is there because of multiple files which began by the same name]. But then what I got instead in the folder where I was running this script was this:

sudipta@Hogwarts:~/html$ ls -l
total 156
lrwxrwxrwx 1 sudipta sudipta     25 2011-07-06 23:08 ?[01;32mFILENAME?[0m -> ?[01;32mFILENAME?[0m

lrwxrwxrwx 1 sudipta sudipta     34 2011-07-06 23:08 FILENAME.txt -> FILENAME.txt

I had no idea where this strange file was from or why it was here. Also, only a few files were getting affected by this. A simple "ls -l" showed that this was happening only to those files which had the executable file set

After some time, I realized that this was a permissions problem. I gave the local directory write permission by www-data, and then sudo-ed myself into www-data and re-executed my script again.

sudipta@dev:~/html$ rm -f FILENAME*sudipta@dev:~/html$ chmod a+w .
sudipta@dev:~/html$ sudo su www-data
[sudo] password for sudipta:
$
for i in `ls /var/www | grep FILENAME`; do ln -sf $i .; done
$^D
And that did it! All I needed was to run the thing as www-data with write permission in my current directory.
sudipta@Hogwarts:~/html$ ls -l
total 156
lrwxrwxrwx 1 sudipta sudipta     25 2011-07-06 23:08 FILENAME -> FILENAME

lrwxrwxrwx 1 sudipta sudipta     34 2011-07-06 23:08 FILENAME.txt -> FILENAME.txt