19

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?

6
  • 2
    You can use Zenity: help.gnome.org/users/zenity/stable/file-selection.html.en
    – TuKsn
    Jun 26, 2014 at 8:57
  • 4
    Please remember to add warning about usage of GUI windows. Unnecessary windows popping up can induce rage on advanced users.
    – mike3996
    Jun 26, 2014 at 9:54
  • 5
    Why, oh why would you ever want to implement such an annoying "feature"? Remember that if we enter the directories at the command line we can use tab completion, and don't need to wait for some gui to load. Why anyone would want to add a GUI to a perfectly good shell script is beyond me.
    – terdon
    Jun 26, 2014 at 12:52
  • 1
    Since we are mixing paradigms, why not get the user input the paths via a web app ? Jun 26, 2014 at 17:36
  • 4
    @terdon Because if we run script directly & not Run in terminal then I want to provide GUI window.
    – Pandya
    Jun 27, 2014 at 16:26

4 Answers 4

39

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 (for GTK window managers anyway), 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

Source

For the most part you can use it the same way - for the file browser:

yad --file-selection

and for the help page:

yad --help-all

Though at the time (around version 26?), it had not been updated to match the new GTK 3.14+ interface (zenity had) - it has more features, but check compatibility (based on documentation it should work on GTK+ >= 2.16.0

8
  • 2
    Yad is a dramatically enhanced fork of Zenity and has largely replaced it since the Zenity project went dormant. I see that Zenity is now back in development at Gnome.org (Gnome3 only?) but I see no way download it. Jul 3, 2014 at 3:16
  • @DocSalvage - that page is fairly ancient - it says next release '3.2' - I have version 3.8 on a now-fairly old Fedora 19 system (with Gnome 3.8 - quite a few bits of gnome are updated for each version of Gnome shell (so its probably been updated for 3.10 + 3.12)). It last deals with bug reports from 2005-2009 as well. You can get the latest stable version (3.8 again) from the Ubuntu repos - packages.ubuntu.com/trusty/zenity. You should also be able to find compiled versions of Yad here.
    – Wilf
    Jul 3, 2014 at 3:48
  • Yad looks quite good - it has more options than zenity :)
    – Wilf
    Jul 3, 2014 at 3:54
  • 1
    @Wilf Yes, yad is Zenity on steroids. Once you get to grips with it, it rocks as it's so flexible. I am a convert. I yadded (couldn't resist that, sorry) a response to the OPs question below, btw.
    – Scooby-2
    Mar 17, 2015 at 20:09
  • @Scooby-2 cool added install instructions to answer (so future users don't have to do comments)
    – Wilf
    Mar 17, 2015 at 23:31
21

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

Syntax:

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

Example:

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

The output will be something like this:

Example

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))
8
  • 1
    Though it is right alternative solution but it doesn't provide GUI window as mentioned in question!
    – Pandya
    Jun 26, 2014 at 9:16
  • 2
    I know, but someone else might find it useful. I posted the screenshot to avoid any confusion.
    – kraxor
    Jun 26, 2014 at 9:24
  • 10
    And, to be fair, it qualifies as a GUI IMHO.
    – kraxor
    Jun 26, 2014 at 9:26
  • 3
    The commonly used line between GUIs and TUIs (textual UI) is the size of the "atom": is it a pixel or a character?
    – mike3996
    Jun 26, 2014 at 9:52
  • 3
    Nice answer - the width and height of some terminals is defined by varibles such as $LINES and $COLUMNS - so you run $(dialog --stdout --title "Please choose a file" --fselect $HOME/ $(expr $LINES - 15) $(expr $COLUMNS - 10)) to make it fill the terminal/screen window.
    – Wilf
    Jun 26, 2014 at 14:58
6

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

0
3

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.

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.