Open all sub-directories in individual tabs of terminal

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.


Solution 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.