Say that I'm doing this:

cd subdir
git init
cd ../

Is there a way to do this with a single command, or perhaps two, rather than having to move in and out of a directory in order to run a command there?

(Not looking for a git-specific solution; that's just an example.)

link|improve this question

67% accept rate
feedback

6 Answers

up vote 9 down vote accepted

I'm surprised no-one mentioned this:

( cd dir ; git init )

or

( cd dir && git init )

It's pretty short and easy to type. It does start a sub-shell, so you can't modify your environment from that, but that doesn't seem to be an issue here.

link|improve this answer
Perfect! Exactly what I was looking for. – Trevor Burnham Apr 17 '11 at 16:34
very helpful answer. – Anisha Kaul Apr 18 at 7:49
feedback

Not exactly what you're asking (you have real answers above with the subshell) but look at pushd and popd

link|improve this answer
feedback

You have a few options. You can either group the commands with && or ;. Like this:

cd subdir && git init && cd ..

or

cd subdir; git init; cd ..

The difference between these is that in the first example, if one of the commands fails, it will not execute the rest of them. In the second example, all of the commands will run no matter what.

Another option would be to define a function and use it, for instance:

function cdinit() {
    cd $1
    git init
    cd ..
}

Then you can run the command:

cdinit subdir

And it will automatically git init in that directory and move out of it.

You could also do a more complex solution using a function if you have a bunch of directories and want to git init them with one command.

function cdinit() {
    for arg in $@
    do
        cd $arg
        git init
        cd ..
    done
}

You can then run this with:

cdinit subdir1 subdir2 subdir3

And it will do git init in subdir1, subdir2, and subdir3.

link|improve this answer
Thanks. I was aware of && and ;, but hoped for something more elegant. Sounds like writing a script is my best option. – Trevor Burnham Apr 17 '11 at 16:21
Correction: This answer is fine, but Mat's answer is better for my particular needs. – Trevor Burnham Apr 17 '11 at 16:36
feedback

You can group the commands with &&, i.e.

cd subdir && git init && cd ../

If you don't want any dependency on the exit code of each command, you can use ; instead, i.e.:

cd subdir ; git init ; cd ../
link|improve this answer
Or with ; so that they don't depend on the return code of the previous one. – slhck Apr 17 '11 at 16:14
Thanks @slhck, I added it to the answer. – Gareth Apr 18 '11 at 4:13
feedback

You have to hop into your target-directory if the command doesn't have a filename or directory name parameter.

But you can write a bash script that takes the target directory and the command as parameters. For this you could take a look at pushd and popd: http://ss64.com/bash/pushd.html

I would write that little script for you, but I haven't a Linux box here :)

link|improve this answer
Just saw the answer from Mark Szymanski. You could just implement a second parameter for a command (and rename the command) and you have what you want. – wullxz Apr 17 '11 at 16:25
feedback

Programs have different ways of handling arguments, so a few will have some equivalent of -folder=name option. Beyond that exception, the standard, even on MS DOS, is simply

$ program subdir

Sometimes you need

$ program subdir/

The program will open the folder, work with it the same way you work with a file, and once finished, return control to your shell, which is pointed at your original standard directory. Programs handled this way DO have the issue that error outputs (like core dumps) go to a file in your shell's current directory (rather than subdir.)

There is no workaround unless the program has command switches available to specify a different place. Some programmers take artistic license between "directory program was called from" and "directory program was told to work in ."

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.