5

I am trying to open all PDFs in a folder from the Terminal.

When I do

open *.pdf

Preview (my default PDF application), opens only one of them.

  • Is there a way to get Preview to open all of them?

  • If not, what other PDF viewer works well for this?

Thanks!

Running Sierra 10.12.6 on a MB Pro.

3
  • 1
    Preview is probably opening all files in the same window. See the sidebar.
    – lhf
    Feb 20, 2018 at 20:43
  • @lhf That's what I thought first as well but it did not seem to do so! (In any case, several windows work better for me)
    – patrick
    Feb 21, 2018 at 1:02
  • Preview used to open multiple PDFs launched like this in a single window. But the changed on High Sierra at least, as now it opens each in it's own window. It still opens multiple pictures in a single window, aka open *.jpg
    – cde
    Nov 8, 2018 at 5:20

2 Answers 2

2

If you want to just open a list of PDFs all at once, you just need to separate the file names (enclosed with quotes) by a space as follows:

open "file1.pdf" "file2.pdf" ... "fileN.pdf"

This will open every PDF specified on one line.

If you want to open every PDF in a particular directory, use the command (simple for/do loop):

for file in /Path_to_Directory/*.pdf; do open "${file}"; done

Both of the examples above use the default app associated with the file (Preview). If you want to specify a particular app (maybe you have a different PDF viewer but want to use Preview) use the -a flag and specify the app:

for file in /Path_to_Directory/*.pdf; do open -a Preview.app "${file}";  done

Note: Be sure to enclose the variable name in quotes (") to account for spaces and non-printing characters. For example, if you have a file named "My PDF File.pdf", not including the quotes will cause the command to try and open each string ("My", "PDF", and "File") as separate files.

1
  • The first command, due to shell globbing, open "file1.pdf" "file2.pdf" is exactly the same as open file*.pdf`
    – cde
    Nov 8, 2018 at 5:24
0

The manpage for open in Sierra still says:

 You can specify one or more file names (or pathnames), which are inter-
 preted relative to the shell or Terminal window's current working direc-
 tory. For example, the following command would open all Word files in the
 current working directory:

 open *.doc

 Opened applications inherit environment variables just as if you had
 launched the application directly through its full path.  This behavior
 was also present in Tiger.

That means your initial approach is usually also the correct one. That is "usually" as in: this open command is buggy (and Preview is buggy, too).

If your PDFs have 'proper' filenames, then all is well and in a directory with 20 PDFs all PDFs will be opened in the manner the manpage promises.

However, if there are 'funny' filenames, legally allowed (or represented differently on the filesystem) on HFS+ but known for being 'difficult' in a shell environment, problems arise.

The globbing function for open is broken. If for example a filename starts with - (minus sign) the next character gets interpreted as another option for the open command. Since there are not many options available for the open command this results often in an error. Spaces in filenames, everything that would need escaping in a normal shell command, seems to be troubling for this command.

Other bugs to observe are that using the option open -a Preview.app does not work as might be expected. Further, it seems that Preview does not respect your preferences whether newly opened docs should all get their own window. Preview opens multiple PDFs with a simple open *.pdf but all those docs are then 'hidden' in the sidebar of a single shared window. You then have to click the triangles to reveal that there are multiple files open now.

A number of files with clean names in a single working directory accessed via open *.pdf with Preview.app set as your default PDF-viewer will open them all (well, I didn't check a probable upper limit on this…)

That leaves now the following options:

  • either ensure you have clean filenames, then use your initial command as it confirms to Apple's documentation and is easier to remember, type and use
  • avoid both Preview.app and the option open -a Preview.app
  • if both options above fail, only then use the solution from Allan's answer
7
  • Eh. Too bad that no-one here around can downvote Apple's bugs or the company's manpages, isn't it? Feb 28, 2018 at 16:04
  • 1
    if there are 'funny' filenames, --> that was def the case here, good to know -- thanks for insight!
    – patrick
    Mar 1, 2018 at 2:49
  • @patrick Well, if I name any files I create, I name them with the severe limitations of most filesystems in mind, and my inclination of using a CLI. Downloaded files are often a box full of chocolates… Mar 1, 2018 at 2:52
  • @patrick btw: are you seeing this on HFS+ or APFS (also)? Mar 1, 2018 at 3:10
  • I am not quite sure -- my system is Sierra 10.12.6 on a MB Pro, not quite sure what applies. ( I am really no Mac expert, sorry...)
    – patrick
    Mar 1, 2018 at 20:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .