How do I prompt users with a GUI dialog box to choose file/directory path, via the command-line?
Solution 1
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
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
Solution 2
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:
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))
Solution 3
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
.
Solution 4
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.
Related videos on Youtube
Pandya
Started using Linux and StackExchange since Ubuntu 12.04 LTS. Then Upgraded to 14.04 LTS. Now I am using Debian GNU/Linux on my Laptop and PureOS on old Desktop computer. I recommend visiting the Philosophy of GNU Project As I've replaced Ubuntu with Debian GNU/Linux, Now my question(s) are became off-topic on AskUbuntu. So, I continue to Unix & Linux. The second reason for my shifting to U & L is I found U & L more interesting than AU since AU is only Ubuntu specific whereas U & L is a broad concept and in my opinion U & L deserves generic questions. (I know why SE has AU & U & L both).
Updated on September 18, 2022Comments
-
Pandya over 1 year
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:
What commands can I use to prompt the user with a GUI selection window that returns the file path to the command-line?
-
TuKsn almost 10 yearsYou can use Zenity: help.gnome.org/users/zenity/stable/file-selection.html.en
-
terdon almost 10 yearsWhy, 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.
-
Tulains Córdova almost 10 yearsSince we are mixing paradigms, why not get the user input the paths via a web app ?
-
Pandya almost 10 years@terdon Because if we run script directly & not Run in terminal then I want to provide GUI window.
-
Admin almost 8 yearsIs there a way to see if the script is run in Terminal or directly? Maybe something like
isatty()
?
-
-
Pandya almost 10 yearsThough it is right alternative solution but it doesn't provide GUI window as mentioned in question!
-
kraxor almost 10 yearsI know, but someone else might find it useful. I posted the screenshot to avoid any confusion.
-
mike3996 almost 10 yearsThe commonly used line between GUIs and TUIs (textual UI) is the size of the "atom": is it a pixel or a character?
-
kraxor almost 10 years@progo Thank you for your explanation, I've edited my answer accordingly.
-
Wilf almost 10 yearsNice 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. -
kraxor almost 10 years@Wilf thanks, I've added your info to the answer.
-
DocSalvager almost 10 yearsYad 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.
-
Wilf almost 10 yearsYad looks quite good - it has more options than zenity :)
-
Scooby-2 about 9 years@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.
-
Wilf about 9 years@Scooby-2 cool added install instructions to answer (so future users don't have to do comments)
-
Scooby-2 about 9 years@Wilf Good thinking there. By the way, I'm running Mint 17 and yad can be installed from the Software Centre, too. I don't know if it applies to other releases, though.
-
Wilf about 9 years@Scooby-2 doesn't seem to be in the ubuntu repos, same looking at the default ones for mint qiana. You might have added the PPA in the past cos nearly everything on WebUp8 uses it (or I am just a moron :)
-
Scooby-2 about 9 years@Wilf The only entry in my /etc/apt/sources.list is virtualbox. I don't know how to check where it was installed from but I think I just type sudo apt-get install yad. BTW it's also shown in the Software Manager and it lists Version: 0.27.0-1~webupd8~utopic0. Also, sorry for the delay replying.
-
tatsu about 5 yearswow real neat! how do you use this to choose just directory though?