Hoping for a suggestion. Using a shell script in Mac OS X Catalina which broke the script. I know there were many security changes but haven't so far figured out modification to access downloads folder. Script errors because it can't get to the file to print. Script is:

do shell script "cat `ls -t /UserName/Downloads/Barcodes*.zpl | head -1` | lpr -P BarcodePrinter -o raw " with administrator privileges

Tried several modifications for the location of the file. Downloads folder is still getting file with long name where * would be variable for 20+ characters generated from the barcode file. Printer name still matches but error is:

No such file or directory
lpr:  No file in print request

ANY suggestions appreciated. Been searching forums but very much a novice and can't seem to pinpoint the right solution.

  • A few questions and suggestions: Mar 21, 2020 at 0:21
  • Specifically, I highly suggest using find to list your files instead of ls. ls is PWD dependent and may be part of the reason the script isn't working for you. It's a little difficult, so instead for now I suggest using dir -w 1, which is essentially a safe output version of ls and takes the -t argument too, and keeps one filename per line. Mar 21, 2020 at 0:46
  • You also shouldn't need to use cat. Try doing dir -w 1 -t /UserName/Downloads/Barcodes*.zpl | head -1 | lpr -P BarecodePrinter -o raw Mar 21, 2020 at 0:51
  • The usual location for home folders on macOS is under /Users (i.e. /Users/UserName/Downloads/...). Do you have the correct path in your script? Mar 21, 2020 at 9:03
  • That second reference to barcode should have been barecode not barecode. Must have inadvertently stuck an e in there somehow. Regardless, no luck with this but different behavior.
    – beginner
    Mar 22, 2020 at 20:42

2 Answers 2


Using the zsh shell on macOS, assuming that the lpr command itself is correct:

lpr -P BarcodePrinter -o raw < ~/Downloads/Barcodes*.zpl(.Nom[1])

The (.Nom[1]) glob qualifier changes the behavior of the preceding globbing pattern so that it only matches regular files (that's what . does), and so that it expands to nothing at all if there is no match (the N, corresponds to nullglob in the bash shell).

The om[1] orders the results of the match in order of modification time stamp and picks out only the first entry from the list, i.e. the most recently modified of the matching files.

If the pattern does not match anything, which it wouldn't do if you are specifying the wrong directory or have mistyped a filename, the shell would complain with

zsh: no such file or directory: ~/Downloads/Barcodes*.zpl(.Nom[1])

The shell will always write out the pathname of the file that can't be found when it reports this type of error (this is missing from the error in your question and would have made it easier to diagnose the issue).

With the extra information posted in comments below, it is clear that the actual name of the files matches ~/Downloads/zebraBarcodes*.zpl or ~/Downloads/*Barcodes*.zpl rather than ~/Downloads/Barcodes*.zpl. The same approach as above with an updated pattern would work.

  • Still don't seem to be getting where I need. This version is resolving the username and downloads folder, but still resulting in 'no such file or directory' number 1
    – beginner
    Mar 23, 2020 at 15:04
  • @beginner Then you should probably double check that you actually have at least one file matching the pattern that you're using. You have not shown us any listing of actual filenames, and we can't make it work by magic.
    – Kusalananda
    Mar 23, 2020 at 16:29
  • @beginner To put it another way, if I asked you why I get an error when trying to match names with a certain filename globbing pattern, what would be your response? It would be "Is the pattern correct?" and "Are your files name the way you think they are?".
    – Kusalananda
    Mar 23, 2020 at 17:47
  • So sorry...duh. My bad. File names are things like: zebraBarcodes3586ea47e32a6d687c2a93cc6ad158f9b.zpl Each time I use the company's website to print new barcodes, the file name is zebraBarcodes....zpl where the long string of characters/numbers changes for each file name. Even when there are multiple versions in the downloads folder I'm typically printing just the newest version (or I can delete older). Does that help? Sorry about that but that's where their '*' I assume was the variable for the numbers/characters different in each file.
    – beginner
    Mar 24, 2020 at 17:12
  • The Zebra printer I'm using is defined in the Mac using CUPScntl. But since it's defined I can see it within the Mac with my other printers. That's my BarcodePrinter. Then the files I'm trying to once again get to print are the random name sample given above.
    – beginner
    Mar 24, 2020 at 17:18

What might be happening here is that the default behavior of ls is to print files to stdout by column, which can cause multiple filenames to be in one line. ls also doesn't print whole filenames (i.e., including the directory prefix), so use cd first to get yourself into the directory for your succeeding commands to properly target the selected files. I also wouldn't use a glob in ls to filter files like that. Use grep, which uses regex's.


cd /Username/Downloads | dir -w 1 -t /UserName/Downloads/ | grep "^Barcodes.*\.zpl$" | head -1 | lpr -P BarecodePrinter -o raw

Also, check if your printer "BarecodePrinter" is spelled correctly.

I do not reccomend using ls or dir to list your files for parsing, find would be better, but I am unaware on how to properly set it up to sort by time like ls without awk magic.

With find, sort and awk:

find /UserName/Downloads -maxdepth 1 -printf "\n%AD %AT %p\n" | grep ".*Barcodes.*\.zpl$" | sort -r | awk '{ print $3 }' | head -1 | lpr -P BarecodePrinter -o raw

Even this can break because of improperly escaped filenames, which find is seemingly notorius for. It's definitely possible to make this even safer, but it's beyond my reach. I'll leave that to other users.

  • ls will output a single column of newline-delimited names if its standard output is not connected directly to a terminal.
    – Kusalananda
    Mar 21, 2020 at 8:54
  • I didn't know that. I've been yelled at before for using ls in scripts, thats all I know. Mar 21, 2020 at 9:24
  • Also, to anyone reading this answer, I wrote it when I was nearly delierous with a lack of sleep, so most of it probably seems insane. I also can't test this on MacOS. Mar 21, 2020 at 9:33

You must log in to answer this question.

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