Super User is a question and answer site for computer enthusiasts and power users. 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

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
up vote 3 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
@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:

cd /dir/

Then save it somewhere and add that script to crontab

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .