Ubuntu: How do I prompt users with a GUI dialog box to choose file/directory path, via the command-line?


Suppose I have a script like this:

(The example depicts an rysnc use case)

#!/bin/bash  echo -n "Enter Source Directory:"  read srcdir  echo -n "Enter Destination Directory:"  read dstdir  rsync -av --delete "$srcdir" "$dstdir"  

The idea here is to prompt the user to enter the "Source" and "Destination" directories for rsync to work with. As is, the user will have to manually enter /path/to/directory/ via the command-line.

Instead, I want to prompt the user to enter the paths through a GUI interface.

Something like this: screem

What commands can I use to prompt the user with a GUI selection window that returns the file path to the command-line?


You can use this for files:

zenity --file-selection  

and this for folders:

zenity --file-selection --directory  

for usage, run:

zenity --help-general  zenity --help-file-selection  

Generally it matches the current theme, on my machine with a modded version of Zukitwo 3.8 it looks like this:

One way of using it is like this:

echo "you selected $(zenity --file-selection)"  

Which would result in you selected /path/to/file.

You can also use options to set an appropriate title, and the directory it starts in - With your rsync use case, for example:

zenity --file-selection --directory --title="Choose rsync source directory" --filename=$HOME/Desktop/  

For files, you can also specify a filetype to select - e.g:

zenity --file-selection --file-filter='PDF files (pdf) | *.pdf' --title="Select a PDF file"  

NOTE: You can also use YAD, a fork of Zenity that has loads more features.

sudo add-apt-repository ppa:webupd8team/y-ppa-manager  sudo apt-get update  sudo apt-get install yad  


For the most part you can use it the same way:

yad --file-selection  

for the file browser

yad --help-all  

for the help page

Though the version I am using has not been updated to match the new GTK 3.14+ interface (zenity has) - nevermind it has more features :)


Just for the record, you can use dialog for a Text-based User Interface (TUI) solution.


dialog --title "text" --fselect /path/to/dir height width  


FILE=$(dialog --stdout --title "Please choose a file" --fselect $HOME/ 14 48)  echo "${FILE} file chosen."  

The output will be something like this:


As pointed out by @Wilf, you can use the $LINES and $COLUMNS variables to make it fill the terminal:

$(dialog --stdout --title "Please choose a file" --fselect $HOME/ $(expr $LINES - 15) $(expr $COLUMNS - 10))  


I know this is 8 months old and also that the OP's question has been answered. However, yad has been mentioned but no example has been offered. Here's my solution using yad.

DIR="/home" \  i=0;for location in source destination  do  ((i++));selection[$i]=$(yad --center \  --width 350 \  --form \  --title="yad example" \  --text="Select $location directory" \  --field=:LBL "" \  --field=Path:DIR "$DIR" \  --separator='' )  done;\  echo "Command to run is \"rsync -av --delete ${selection[1]} ${selection[2]}\""  

The way it works is like this. We put yad in a for loop, setting the variable $location to source for the first pass and destination for the second. The output is placed in the array selection[] for which the variable i is used as the index. This is set to 0 at the start and incremented with each pass. Hence the source is saved as ${selection[1]} and the destination ${selection[2]}.

The DIR="/home" on the first line sets the dialog default. The yad command options can be found from the terminal by typing yad --help.

yad screenshot


Here is the shortest (and best) solution to the answer: Yad provides the exact option just like zenity does:

yad --file-selection --directory  

This opens a directory selection dialog. Without the additional argument --directory it will be a file selection dialog instead.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »