Emacs Org-mode - Export to another directory?

You have to put the following line at the beginning of your org file :

#+EXPORT_FILE_NAME: PATH/filename

Where PATH is the path to the folder where you want your file to be exported (e.g. ~/exports) and filename the name you want to give to your exported file (e.g. tutorial.html).


This probably wasn't possible when the question was first asked, but the simplest solution would be to add the directory to the :EXPORT_FILE_NAME: property:

:PROPERTIES:
:EXPORT_FILE_NAME: exports/<filename>
:END:

Just as in the accepted answer, the directory must exist in order for this to work.


In addition to the use of publishing by modifying your org-publish-project-alist variable as @user1248256 suggested, you can directly specify the org-export-publishing-directory variable within your file:

#+bind: org-export-publishing-directory "./exports"

* This is a test headline
Some text here.  This should be exported to the "./exports" directory.

Upon export it will be placed in the "exports" directory, but only if that directory exists. If it does not exist, you will get an error message in the console.


The original question referred to exporting of org-files, while most answers above actually have to do with publishing, which is a different concept.

I believe the best way to solve the problem posed by the OP is to add the following to your emacs initialization file (.emacs):

(defadvice org-export-output-file-name (before org-add-export-dir activate)
  "Modifies org-export to place exported files in a different directory"
  (when (not pub-dir)
      (setq pub-dir "exported-org-files")
      (when (not (file-directory-p pub-dir))
       (make-directory pub-dir))))

PS:

  1. I realize a 5 year old question might no longer be relevant to the OP, but hopefully people searching for similar stuff will benefit from this answer.

  2. This is a slight modification of a code snippet found in http://rwx.io/posts/org-export-configurations/

  3. The original solution found in the above blog allows for setting up different directories for each exported format. However, if the goal is to avoid having one's directory "littered with PDF, Tex, and HTML files", I think it is best to have only one directory containing exported files of all formats, which is the essence of the modification I offered above.


Edit: The emacs manual (https://www.gnu.org/software/emacs/manual/html_node/elisp/Porting-old-advice.html#Porting-old-advice) states that the defadvice mechanism was made obsolete by the new advice-add. So here is a code snipet with the same effect, using the recommended advice-add:

(defun org-export-output-file-name-modified (orig-fun extension &optional subtreep pub-dir)
  (unless pub-dir
    (setq pub-dir "exported-org-files")
    (unless (file-directory-p pub-dir)
      (make-directory pub-dir)))
  (apply orig-fun extension subtreep pub-dir nil))
(advice-add 'org-export-output-file-name :around #'org-export-output-file-name-modified)

As before, this should be placed in your .emacs file.

Tags:

Org Mode