Question

Hello Bash Superheros,

I am trying to create a simple bash function that pipes the output from find to a a pdf viewer, similar but limited to evince.

function findpapers {
    find ~/papers/ -name *$1* | evince 
}

The above function opens the viewer but does not display any of the desired files/paths. Ultimately, I would like to display all the pdf's outputted from find. A simple find command such as:

$ find /home/alex/papers/ -name *erebus*

creates an output like:

/home/alex/papers/2000/rowe/seismic_and_acoustic_observations_at_mount_erebus_volcano_ross_island_antarctica.pdf

/home/alex/papers/2008/Jones/infrasonic_tracking_of_large_bubble_bursts_and_ash_venting_at_erebus_volcano_antarctica.pdf

Then the idea is to display those two pdf files in a viewer.

Any ideas or suggestions? I am using Linux Mint if that helps. Thanks heaps in advance!

Was it helpful?

Solution

You want the list of file names on the command line of evince, not in its standard input.

Use

evince $(find /home/alex/papers/ -name *erebus*)

or

find /home/alex/papers/ -name *erebus* | xargs evince

On linux (or any OS that uses the gnu versions of find and xargs) , if there is any chance the file names might contain spaces, better use

find /home/alex/papers/ -name *erebus* -print0 | xargs -0 evince

OTHER TIPS

You should also be able to use

find /home/alex/papers/ -iname '*erebus*' -exec evince '{}' +
  • iname tells find that the search pattern given is case insensitive.
  • -exec executes the given command evince with the found filenames inserted for {}. The plus sign tells find to append all found filenames to a single evince command, instead of starting a new evince for each single found file. If you replace + with an escaped semicolon (\;), you get the latter behaviour.

If you are using bash 4 or later, you don't need find:

shopt -s globstar
evince /home/alex/papers/**/*erebus*
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top