I'm trying use the following command on Mac OSX Mountain Lion to get a list of my mail folders as part of my .muttrc file:
`echo -n "+ "; find ~/.mail/fastmail -maxdepth 1 -type d -name ".*" -printf "+'%f' "`
find: -printf: unknown primary or operator
-bash: +: command not found
How can I rewrite this to get the desired result?
Best Answer
OSX
find
has no-printf
action. The+: command not found
error is because your command is enclosed in back ticks (``), so the shell is treating the results of the
findcommand as a command and attempting to execute them, specifically it is trying to execute
+` which is the first thing printed by the command you ran. You will get the same error if your runBack ticks are used to save the results of a command to a variable, so the above gives an error but this does not:
You do not say what your desired output is. Based on your question, I assume you want to get a list of all folders in a given directory that start with a
.
and print their names on the same line, quoted and preceded by a+
. If so, you can do something like this:Sample output:
To pass the output of this command as input to another program (
mailbox
for example), do this:or
In response to OP's comment:
If you just want all folders, you don't need
-name
, to remove the quotes, just don't quote{}
. I will also assume that you don't want the parent folder (fastmail
), hence-mindpeth 1
:Removing the path is slightly more complex because, contrary to what you might expect, you can't just use
basename
in the-exec
call. You need to get creative, here are a few choices:Parse with
awk
-F"/"
tellsawk
to use/
as the field delimiter and then print+
followed by the last field ($NF
) which will be the folder name.Use a
for
loop (assuming that your folder names have no strange characters or spaces)If your folder names contain spaces or strange characters, use this instead: