Power Up Linux GUI Apps

Save time and gain functionality by starting your GUI apps from the command line.

Windowing interfaces to Linux have lots of advantages. Once you find the programs you need, the menus and dialog boxes list your choices, and there’s often not much else to know about the interface.

One big disadvantage of GUI apps, though, is that you’re mostly limited to whatever the interface lets you do — unless you want to re-code the interface yourself, that is. For instance, how can you use a photo viewer to see all of the photos you put in various places on your disk this past Monday? If you know where the photos are stored and what their names are, you can navigate through your directories in a file browser like Nautilus, clicking on files to view them. But what a pain!

Linux users have more choices. You can start most GUI apps from the command line with a list of arguments (usually filenames) to work on. This lets you marry the handy features of shells (filename completion, wildcards, loops and more), and well as the power of utilities like find and grep, to the GUI apps you’re already using. The command line also often lets you specify options that aren’t available from menus.

Let’s start with a couple of examples. You’d like to use GIMP to edit the photo files with rome in their filenames — like rome12.jpg and elsa in rome.jpg. Start GIMP this way:

$ gimp *rome*.jpg

You have photos of Rome in some of your 100 subdirectories but you aren’t sure which. You’d like to view them all with Eye of GNOME. Let find or the shell find the files and give their pathnames to eog:

$ eog $(find . -name '*rome*.jpg' -print)
$ eog **/*rome*.jpg

The second example above works in Z Shell and in bash version 4 with the globstar shell option set. (** is a recursive wildcard operator. To learn more, read Even Wilder Wildcards.)

Building the Command Line

The first word on a command line is usually the name of a program you want to run, like konqueror. It may be a different name than you see on your desktop menus — such as eog.

Next you can add options. Each option starts with a space and one or two dashes. For a list, use the man command or try the program’s --help option:

$ man konqueror
No manual entry for konqueror
$ konqueror --help
Usage: konqueror ... [options] [URL]
...description of each option...

From those, you should get an idea of how to build the command line. If not, and the application doesn’t do anything dangerous (like removing files), you can also just experiment. For instance, although Konqueror says it wants a single URL, it accepts filenames (and wildcards); they’ll open in tabs, one per file. These usually come last on a command line. For example, to open all filenames ending in .html within an 800x600-pixel window at the bottom right corner of the display:

$ konqueror --geometry 800x600-0-0 *.html

When you start a GUI application from the command line, its window should open fairly soon. If you get another shell prompt (like $), the app is running independently of the shell. If you don’t get another prompt, the shell is waiting for the app to finish. In that case, you can add a final ampersand (&) to run the process in the background and get another prompt:

$ gimp [abc]*tif &
[1] 1234
$

Shell and utilities give lots of flexibility; we’ll have to settle for just a few examples. Other Power Tools columns have more ideas.

Argument Lists and Loops

Although many GUI apps can accept multiple filenames, there are limits. (If you pass 100 filenames to GIMP and it opens 100 windows, that’s a mess!) Or you may want to open just one file at a time.

If the shell waits for the GUI program to finish, a shell loop can run the app once for each file. For instance, to search the HTML files in the current directory for the word error (upper or lowercase) and open each one in Epiphany:

$ grep -il error *.html |
> while read file
> do epiphany "$file"
> done

There, grep -il outputs the names of files that contain the string, and the redirected-input while loop runs epiphany once for each file.

Some apps don’t behave that way, though. They spawn an instance of themselves
and the originally-called program exits right away, or the program becomes a child of init (the Linux “grandparent” process, PID 1). In those case, a loop like the one above will quickly open one window for every file. You’ll need to create a pause some other way.

For an example, you want to run the nautilus filesystem browser once, separately, on each subdirectory named lib. The handy find utility has -ok and -okdir (the latter is more secure) to pause and prompt you before each directory it finds:

$ find . -type d -name lib -okdir nautilus '{}' \;
< nautilus ./0209_exrc/lib > ? y
   ...nautilus opens on 0209_exrc/lib...
< nautilus ./0210_path/lib > ?
< nautilus ./0211_perm/lib > ? y
   ...nautilus opens on 0211_perm/lib...

Maybe your favorite filesystem browser has a feature like that. So, to up the ante, let’s use more of the power of shells and utilities. Browse each directory containing any files that haven’t been modified in the past year (365 days), running nautilus once per directory. Because some directories might have more than one matching file, we’ll use sed to strip the filename off the end of each pathname (removing the final slash and all non-slash characters after it) and sort -u to remove duplicate pathnames from sed‘s output. Finally, the argument-handling program xargs will read the output of sort, line by line (-L 1 means one line at a time, and -d '\n' sets the “line” delimiter to a newline), prompting (-p) each time. Here goes:

$ find . -type f -mtime +365 -print |
> sed 's@/[^/]*$@@' |
> sort -u |
> xargs -d '\n' -L 1 -p nautilus
nautilus . ? n
nautilus ./0209_exrc ? y
   ...nautilus opens on 0209_exrc...
nautilus ./0209_exrc/RCS ?

If you’re fairly new to the command line, that example may look like something you should write as a program instead. But once you’ve learned more about the Linux utilities and what they do, you can dream up that command line and have it running within a minute or so. From then on, the time savings (versus whatever you might do from a GUI-only interface) will be substantial.

Command-line Completion

Most shells have sophisticated command-line completion that lets you type parts of command and filenames, then press TAB to fill in the rest. Of course, many GUI file-browsing menus have similar features. but they only let you open one file at a time. On the command line, you can use completion to build a list of file or directory names for the GUI app — and the shells’ file-matching features are much more sophisticated than the GUIs I’ve seen so far.

Like it? Keep it!

If you concoct a handy command-line combination that you’d like to use again, remember that shells have aliases to give a name to a command line, and functions to name a series of commands. Some commands will also fit into a shell script that you can launch from your GNOME or KDE menus. The possibilities really are endless.

Jerry Peek is a freelance writer and instructor who has used Unix and Linux for more than 25 years. He's happy to hear from readers; see http://www.jpeek.com/contact.html.

[Read previous article] [Read next article]
[Read Jerry’s other Linux Magazine articles]