Power Up Linux GUI Apps

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:

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:

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:

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.