1

I want to open all sub-directories of the current folder in different tabs of the terminal.

I tried a solution like proposed in this thread, but it opens the directories in new terminals and not new tabs of the current terminal.

https://stackoverflow.com/questions/21591757/open-a-tab-and-running-a-script-using-gnome-terminal-tab-option-on-ubuntu

According to this thread:

Command to open new tab in the current terminal

I tried this:

#!/bin/sh

for d in ./*/ ; do (xdotool key ctrl+shift+t && cd "$d"); done 

However, this opens the current directory multiple times, but not the sub-directories.

  • Would it also be okay to open a single new terminal window with one tab per folder? This could be done rather easily and without resorting to emulating keystrokes, which is always tricky. Btw, your approach doesn't work because the cd happens in your current tab/shell, not in the new tab's shell. You'd have to emulate typing that command in there after a specific waiting time too, which gets really uncomfortable, as you not only have to wait for it to finish typing, if you press any key during that or focus another window, everything will break. – Byte Commander Jun 13 '18 at 9:50
  • Thanks for the explanation!. It would be fine to open a single new terminal with one tab per folder. – YUK Jun 13 '18 at 10:57
1

If it is okay that all the tabs will open in one new terminal window instead of the current one, you can use something like this below instead of simulating key presses with xdotool:

find "$(realpath .)" -mindepth 1 -maxdepth 1 -type d -printf "--tab --working-directory '%p'\n" | xargs gnome-terminal

Explanation:

The same formatted in a more readable way:

find "$(realpath .)" \
     -mindepth 1 -maxdepth 1 -type d \
     -printf "--tab --working-directory '%p'\n" | 
xargs gnome-terminal

What it does is:

  • realpath . gets the absolute path of the current directory
  • find [...] lists all files and folders inside the directory given as first argument which match the following criteria, in the specified way.

    • -mindepth 1 and -maxdepth 1 disables recursion, so that you only get results from directly inside your search folder, without that folder itself and without sub-subdirectores.
    • -type d limits the results to only directories, no files etc.
    • -printf "[...]" tells find to print the results it finds to its standard output, using the following format string. %p will be replaced by the result's path, starting with the search directory (which is why we used realpath earlier to make it absolute instead of just using .)

    So far, the output of just running the part before the | pipe would look like:

    --tab --working-dir '/tmp/test/folder1'
    --tab --working-dir '/tmp/test/folder2'
    --tab --working-dir '/tmp/test/folder3'
    
  • xargs gets the output from above piped into its standard input. It will read all input lines and use them to construct and run a command line, by appending them to its own arguments. The resulting command it would run with the example above would be:

    gnome-terminal --tab --working-dir '/tmp/test/folder1' --tab --working-dir '/tmp/test/folder2' --tab --working-dir '/tmp/test/folder3'
    

Now that constructed command will open a new gnome-terminal window, with one tab per --tab argument, and applying all arguments after each --tab (and before the next one) to that specific tab. --working-dircetory /PATH/TO/SOMEWHERE sets the initial working directory to the given folder.

  • Nice. If this answer solved your problem, please consider accepting it by clicking the check button its left then. – Byte Commander Jun 13 '18 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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