I'm trying to do that most basic of things: perform one or more commands on all files within a particular folder.
In this instance, it's converting eps files to PDF using macOS's pstopdf
command.
#!/bin/zsh
FILES=$(find "/Users/Ben/Pictures/Stock Illustrations" -type f)
for f in "$FILES"
do
echo "$f"
pstopdf "$f"
done
echo "$f"
produces a correct list of all the files; but I then get a second list of files -- seemingly from pstopdf
itself** -- starting with File name too long: /Users/Ben/Pictures/Stock Illustrations/Flock wallpaper.eps
, but the rest of the files are listed correctly.
However, the pstopdf
command doesn't create any PDF files.
** I've tried commenting out the echo
and the pstopdf
command, so I know that each produces a list of filenames.
If I run pstopdf <file.eps>
in the Terminal, I get no output to the CLI (e.g. no filename listed), but the file is processed and a PDF file created.
I dare say I could use xargs
in the find
command, though I prefer the more structured approach of a loop with arguments, not least because it gives the option of multiple commands and other logic, and it's easier to read.
There is a similar question here: I get message "File name too long" when running for..in and touch
But I don't understand how the answer applies. It says "or touch them one by one" (which is what I want), but if I do something like:
FILES=/Users/Ben/Pictures/Stock\ Illustrations/*
I just get "No such file or directory".
FILES
. depending on where and how you use it, you might get that error, possibly due to word splitting messing the result up when expanded. But you'd have to show what you're actually doing there. – ilkkachu Aug 30 '22 at 11:01for f in "$FILES"
looks exactly the same as in the question you linked,$FILES
will be a single string containing all the filenames, joined together. – ilkkachu Aug 30 '22 at 11:02for f in $files
doesn't split it up, like say in python? Can you give the correct wording? – benwiggy Aug 30 '22 at 11:04for f in $files
does wordsplit on whitespace by default (and expand globs), butfor f in "$files"
doesn't. But the splitting is rife with issues, and ignores the fact that filenames can themselves contain whitespace, so it's usually best avoided. I'm not sure what Python behaviour you refer to, since something likefor i in list
in Python would loop over each element of the list (without textual splitting), whilefor i in string
would loop over each character of the string (not splitting on whitespace) – ilkkachu Aug 30 '22 at 11:07