Elisp: File and Directory Functions

By Xah Lee. Date: . Last updated: .

Here's the most useful functions for file and directory.

{Rename, Copy, Delete} Files

(rename-file "/home/jane/test1.txt" "/home/jane/test2.txt")

(copy-file "/home/jane/test1.txt" "/home/jane/test2.txt")

(delete-file "/home/jane/test2.txt")

(copy-directory "/home/jane/stuff" "/home/jane/stuff-backup")

;; delete a whole dir. (new in emacs 23)
(delete-directory "/home/jane/stuff" t)

File Name Manipulation

;; get the dir path part
(file-name-directory "/home/jane/xyz.txt") ; "/home/jane/"

;; get filename part
(file-name-nondirectory "/home/jane/xyz.txt") ; "xyz.txt"

;; get filename's extension
(file-name-extension "/home/jane/cat.txt.jpg") ; "jpg"

;; get filename without extension
(file-name-sans-extension "/home/jane/cat.txt.jpg") ; "/home/jane/cat.txt"

;; get relative path
(file-relative-name "/home/jane/b/cat.jpg" "/home/jane/") ; "b/cat.jpg"

;; get full path
(expand-file-name "test.el")
;; sample output
;; "/home/jane/misc/emacs/test.el"

(info "(elisp) File Names")

Traverse Directory

Elisp: Traverse Directory

Elisp Basic Functions

  1. Elisp: Cursor Position Functions
  2. Elisp: Text Editing Functions
  3. Elisp: String Functions
  4. Elisp: Buffer and File Functions
  5. Elisp: File and Directory Functions
Patreon me $5 patreon

Or Buy Xah Emacs Tutorial

Or buy a nice keyboard: Best Keyboard for Emacs

If you have a question, put $5 at patreon and message me.