[kde-linux] opening linked files

John O'Hagan johnohagan at iprimus.com.au
Sat Jul 28 09:00:48 UTC 2007


> What i need is changing to the directory the file is pointing to, then
> opening that file with the proper file association. Currently, file
> association is ok, but links are opened in the directory where they are.
> I would like to write a script that chances to the dir the actual file is,
> then open it.
> I could associate a script to that file type, that opens the file the usual
> way, if that is a real file or changes to the right directory before
> opening, How to pass the path to the directory to the script?

The "file" command could be what you want:

file $FILE | awk '/symbolic link/ {print $5}'

will return the path to the real file if $FILE is a symbolic link otherwise it 
returns nothing. So, for a particular PROGRAM, you could do:

REALFILE=$(file $FILE | awk '/symbolic link/ {print $5}' | sed s/\'// | sed 
s/\`// ) 

[[ $REALFILE ]] && $PROGRAM "$REALFILE" || $PROGRAM $FILE

#(The extra sed bits in first line remove annoying backticks and quotes from 
the output; theres probably a neater way, but you get the idea).

This checks if a file is a link and opens the target if it is, otherwise it 
just opens the file, using whatever program you have assigned to PROGRAM.

If you actually need to be in the real file's directory, you could make the 
second line:

[[ $REALFILE ]] && cd $(dirname "$REALFILE") && $PROGRAM "$REALFILE" || 
$PROGRAM $FILE


Regards,

John



More information about the kde-linux mailing list