3

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?

1
  • If you're coming from Linux or getting tips from a Linux source, it's always worth remembering that many commands on OSX are BSD versions, not the GNU ones you might be familiar with. They differ in many small, subtle ways and some big, not-so-subtle ways too.
    – Telemachus
    May 6, 2013 at 12:12

1 Answer 1

8

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 thefindcommand 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 run

`echo -n "+"` 

Back ticks are used to save the results of a command to a variable, so the above gives an error but this does not:

foo=`echo -n "+"`

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:

find ~/.mail/fastmail -maxdepth 1 -type d -name ".*"  -exec echo -n "+'{}' " \;

Sample output:

+'/home/terdon/.mail/fastmail/.bar' +'/home/terdon/.mail/fastmail/.foo' 

To pass the output of this command as input to another program (mailbox for example), do this:

mailbox `find ~/.mail/fastmail -maxdepth 1 -type d -name ".*"  -exec echo -n "+'{}' " \;`

or

mailbox $(find ~/.mail/fastmail -maxdepth 1 -type d -name ".*"  -exec echo -n "+'{}' " \;)

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:

find ~/.mail/fastmail -maxdepth 1 -mindepth 1 -type d -exec echo -n "+{} " \;

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

    mailbox `find ~/.mail/fastmail -maxdepth 1 -mindepth 1 -type d | 
     awk -F"/" '{printf "+%s ",$NF}'`
    

    -F"/" tells awk 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)

       mailbox `for dir in $(
         find ~/.mail/fastmail -maxdepth 1 -mindepth 1 -type d
        ); do echo -n "+$(basename $dir) "; done`
    

    If your folder names contain spaces or strange characters, use this instead:

    mailbox `find ~/.mail/fastmail -maxdepth 1 -mindepth 1 -type d | 
      while IFS= read -r dir; do echo -n "+$(basename $dir) "; done`
    
5
  • Thanks. I'm trying to use this to list all my mailboxes in my mail folder to be used in my .muttrc file. The original code I borrowed from used: mailboxes echo -n "+ "; find ~/Maildir -maxdepth 1 -type d -name ".*" -printf "+'%f' " How would I use your example above to give the output to 'mailboxes'?
    – user222332
    May 6, 2013 at 12:35
  • @user222332 same way really, see updated answer.
    – terdon
    May 6, 2013 at 12:40
  • @user222332 you are very welcome. If my suggestion answered your question please mark it as accepted so the thread can be marked as answered.
    – terdon
    May 6, 2013 at 13:52
  • will do. 1 quick follow-up. Using find ~/.mail/fastmail -maxdepth 1 -type d -name "*" -exec echo -n "+'{}' " \; gives me the full mailbox path +'/Users/trey/.mail/fastmail/INBOX.mutt' How can I change the output to be +INBOX.mutt (strip the path and take away the single tick marks)?
    – user222332
    May 6, 2013 at 23:00
  • 1
    @user222332 see updated answer. In the future please explain your desired output in your actual question so we can know how to answer.
    – terdon
    May 7, 2013 at 15:40

You must log in to answer this question.

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