Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Suppose for example I have 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?

Because If scripted file run directly (by double-click and not run in terminal) then I want to provide GUI selection window.

share|improve this question
2  
You can use Zenity: help.gnome.org/users/zenity/stable/file-selection.html.en –  TuKsn Jun 26 '14 at 8:57
3  
Please remember to add warning about usage of GUI windows. Unnecessary windows popping up can induce rage on advanced users. –  progo Jun 26 '14 at 9:54
4  
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 '14 at 12:52
1  
Since we are mixing paradigms, why not get the user input the paths via a web app ? –  user1598390 Jun 26 '14 at 17:36
3  
@terdon Because if we run script directly & not Run in terminal then I want to provide GUI window. –  Pandya Jun 27 '14 at 16:26

3 Answers 3

up vote 18 down vote accepted

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

Source

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 :)

share|improve this answer
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. –  DocSalvager Jul 3 '14 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 '14 at 3:48
    
Yad looks quite good - it has more options than zenity :) –  Wilf Jul 3 '14 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 at 20:09
    
@Scooby-2 cool added install instructions to answer (so future users don't have to do comments) –  Wilf Mar 17 at 23:31

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))
share|improve this answer
1  
Though it is right alternative solution but it doesn't provide GUI window as mentioned in question! –  Pandya Jun 26 '14 at 9:16
2  
I know, but someone else might find it useful. I posted the screenshot to avoid any confusion. –  kraxor Jun 26 '14 at 9:24
4  
And, to be fair, it qualifies as a GUI IMHO. –  kraxor Jun 26 '14 at 9:26
1  
The commonly used line between GUIs and TUIs (textual UI) is the size of the "atom": is it a pixel or a character? –  progo Jun 26 '14 at 9:52
1  
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 '14 at 14:58

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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