Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

may be it seems strange for you, but I want to run command in a specific folder without changing the current folder in the shell. Example - this is what I usually do:

~$ cd .folder
~/.folder$ command --key
~/.folder$ cd ..
~$ another_command --key

Though I want something like this:

~$ .folder command --key
~$ another_command --key

Is it possible?

share|improve this question
    
Can't you do ~/.folder/command --key ? Does the command require your current directory to be ~/.folder ? –  glenn jackman Jan 22 at 16:58

2 Answers 2

up vote 5 down vote accepted

If you want to avoid the second cd you can use

(cd .folder; command --key)
another_command --key
share|improve this answer
    
Very quick answer! I even can't to accept it because system doesn't allow me)) –  Timur Fayzrakhmanov Jan 22 at 16:12
    
magic parenthesis! how does that work? +1 –  precise Jan 22 at 18:06
    
The commands within the parenthesis are run in a new shell process so changing the directory, setting environment variables etc. inside the parenthesis do not affect the parent shell that runs the other commands. –  Florian Diesch Jan 22 at 18:25
    
I'd change the ; to && for good measure. If the cd fails (e.g. because you typoed the directory name), you probably don't want to run the command. –  geirha Jan 24 at 9:35

Without cd... Not even once. I found two ways:

# Save where you are and cd to other dir
pushd .folder
command --key
# Get back where you were at the beginning.
popd
another_command --key

and second:

find . -maxdepth 1 -type d -name ".folder" -execdir command --key \;
another_command --key
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.