Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

How can I run a command that is in a different directory so that the command thinks I've ran it in the current directory?

If a command is using basename() or argv[0] (or whatever it's called) to get where it was called from, and it requires files in its directory, it may look in the directory that you called the application from; i.e. the wrong one.

Of course, you can cd to the directory first and invoke it there, but is there a way to invoke the application so that it thinks you've ran it from its directory (without cding first)?

If it is possible, I will use this in crontab, as the thought of using cd in there scares me.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

Why does using cd in crontab scare you?

Note that if you want to just temporarily change directory and then come back, you can use pushd and popd

share|improve this answer
    
I get the impression it could affect the workings of other commands that may be in there, or the cron daemon? Maybe I'm being a bit daft, like. –  Doddy Aug 18 '11 at 13:28
    
And I suppose I'm not a fan of the strings of cd dir && ./cmd entries in the crontab. I just figured there would be a way to do it easily. –  Doddy Aug 18 '11 at 13:32
2  
@panic This is actually the common way to do it. cd blah && command. –  slhck Aug 18 '11 at 13:38
    
@panic Well, then use pushd and popd. pushd dir && ./cmd && popd –  Let_Me_Be Aug 18 '11 at 14:01

You can make a script like this:

#!/bin/sh
cd /dir/
yourcommand

Then save it somewhere and add that script to crontab

share|improve this answer

Your Answer

 
discard

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.