1

I want to open a terminal and automatically open a folder and run a command within it

For this, I have the following:

:~$ gnome-terminal --working-directory=/home/project -- code .

This runs code . (opens VSCode in the current folder) after it opens the terminal in /home/project.

  1. Is this behaviour guaranteed that it will navigate to the working directory and then run code .

    I ask because I do not want code . to run in the current directory from where the command is issued and then the terminal opens in the working directory.

  2. This seems to work fine if the working directory exists. But if /home/project does not exist, I want the entirety of the above command to fail. At present, it still runs code . if the working directory does not exist in the current folder from the above command is issued.

New contributor
One_Cable5781 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

2 Answers 2

8

What you have might work, I don't know how --working-directory is implemented, but it doesn't seem worth it. Just use this instead:

gnome-terminal -- code /home/project

Or, if you really need to change directory, use

 gnome-terminal -- sh -c "cd /home/project && code ."

The && ensures that the command will fail if the directory doesn't exist since if it doesn't exist, the cd will fail.

You could even script it, and have something like

#!/bin/bash

if [[ -d /home/project ]]; then
   cd /home/project && code .
fi

Save that as ~/bin/project.sh, make it executable with chmod a+x ~/bin/project.sh and then run gnome-terminal -- project.sh.

2
  • Thank you. In both these commands, once I close VSCode (the output of code .), the terminal closes too. When I explicitly (1) cd /home/project, then, (2) manually type code . and then close VSCode, the terminal from (1) with pwd of /home/project continues to persist. Is there a way to issue a "pause" as it were between cd /home/project and code . ? I am unsure if the lack of a "pause" is somehow the cause of this behaviour though. Commented yesterday
  • No, @One_Cable5781, the pause is irrelevant. I can't reproduce what you say on my system, but I'm using a different command since I don't have VSCode installed. I tested this using gnome-terminal -- nautilus /home/terdon/foo and the terminal always stays open. Perhaps VScode kills its parent process for some reason? In any case, you could try gnome-terminal -- code /home/project &, or if that doesn't work gnome-terminal -- sh -c "code /home/project & disown but since I can't reproduce it, I don't really know what approach is most likely to work. Might be worth posting a new question. Commented yesterday
1

There are two methods to run command2 only if command1 succeeds:

  1. if { command1 ; } then command2 ; fi
  2. command1 && command2

But this seems to be not what you're actually asking for, because in your case there aren't actually two commands that you want to run in order; there is only a single command that you run within the terminal: code .

It seems that you want to run this command (code .) only if current working directory is /home/project. So you should simply check that condition.

Replace code . with:

sh -c 'if [ `pwd` = /home/project ]; then code . ; fi'

(gnome-terminal can't execute if directly as it's a builtin shell command, and it expects only a program name after --, so shell must be invoked explicitly)

On my system, this behaves as expected, ie. changes working directory to /home/project before running code (only I tried with nautilus instead of code because I don't have VSCode installed - it properly opened Nautilus window in /home/project folder). However, I see that you note that your initial attempt ran code in your current directory. So maybe you have better results with just explicitly changing the directory and running code only if cd command succeeds:

gnome-terminal -- sh -c 'cd /home/project && code .'
10
  • 1
    I tried gnome-terminal --working-directory=/home/project && code . This opens code . in the current folder and not in the working directory specified Commented yesterday
  • 1
    Because in your example you aren't actually trying to run two commands in order. See my edit to the answer. Commented yesterday
  • I gave the following command: gnome-terminal --working-directory=/home/project -- if[pwd=/home/project]; then code . ' fi There seems to be a syntax error. bash: syntax error near unexpected token `then' Note that this comment section does not allow easy formatting, there are backticks as you have specified surrounding pwd in my actual command but not rendered in these comments Commented yesterday
  • Yes, it's because of very poor parameter interpretation by gnome-terminal. I have posted a fix. Commented yesterday
  • 1
    [ `pwd` = /home/project ] seems overcomplicated and doesn't cover other path representations that reach the same working directory. [ . -ef /home/project ] is simpler and covers alternative path representations. Otherwise good answer. +1 Commented 2 hours ago

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.