How to [tar and] compress marked files in Emacs
Asked Answered
B

4

11

In dired+ in Emacs 23.2.1 on a Debian Squeeze variant I selected four files with * and then pressed Z to compress them. I answered y to the prompt and saw some status updates in the mini-buffer. Where do I find the compressed file? I tested on one file (C-u Z) and Emacs ran gzip on the one file and made it a .gz file. How do I [tar and] compress marked files in Emacs?

(To preempt any philosophical or methodological discussions about tar, gzip, other formats and archives in general, all I want is the four files to be stored in one file as compressed data. If that can be achieved via tar and gzip or compressing each directly into an archive doesn't matter.)

Bactericide answered 19/4, 2012 at 11:14 Comment(0)
B
23

If dired+ is anything like dired, you can mark the files with m and then hit ! (to run a shell command on the marked files) and specify the command as tar -czf foo.tar.gz * (the * is a special marker that is replaced by the names of the marked files).

Brainstorming answered 19/4, 2012 at 11:27 Comment(2)
Thanks, this works in dired+. I'd seen an example similar to this, but didn't realize the * would be replaced with the names of the /marked/ files. I'd have thought a commonly-used command like this would be "built-in" to dired[+].Bactericide
dired+ is just a set of extensions to dired.Wilfredwilfreda
B
6

You can also archive files just by marking and copying them to an archive file.

For example, mark several files in dired, and select m-x dired-do-copy.

When prompted for destination, type test.zip. The files will be added to the zip archive automatically.

You can also uncompress files by selecting them in dired and running the command dired-do-extract

To set this up, look at the following variables: dired-to-archive-copy-alist dired-extract-alist

Here's my setup, which has served me for many years...

;; dired-a provides support functions, including archiving, for dired
(load "dired-a")

;; Alist with information how to add files to an archive (from dired-a)
;; Each element has the form (REGEXP ADD-CMD NEW-CMD). If REGEXP matches
;; the file name of a target, that target is an archive and ADD-CMD is a command
;; that adds to an existing archive and NEW-CMD is a command that makes a new
;; archive (overwriting an old one if it exists). ADD-CMD and NEW-CMD are:
;; 1. Nil (meaning we cannot do this for this type of archive) (one of
;;    ADD-CMD and NEW-CMD must be non-nil).
;; 2. A symbol that must be a function e.g. dired-do-archive-op.
;; 3. A format string with two arguments, the source files concatenated into
;;    a space separated string and the target archive.
;; 4. A list of strings, the command and its flags, to which the target and
;;    the source-files are concatenated."
(setq dired-to-archive-copy-alist
      '(("\\.sh\\(ar\\|[0-9]\\)*$" nil "shar %s > %s")
    ("\\.jar$" ("jar" "uvf") ("jar" "cvf"))
    ("\\.tar$" ("tar" "-uf") ("tar" "-cf"))
    ("\\.tgz$\\|\\.tar\\.g?[zZ]$" ("tar" "-uf %s" "|" "gzip > %s") ("tar" "-czvf"))
    ("\\.ear$" ("zip" "-qr") ("zip" "-qr"))
;   ("\\.rar$" ("rar" "a")   ("rar" "a"))
    ("\\.war$" ("zip" "-qr") ("zip" "-qr"))
    ("\\.zip$" ("zip" "-qr") ("zip" "-qr"))
    ("\\.wmz$" ("zip" "-qr") ("zip" "-qr")) ;; for media player skins
    ("\\.arc$" ("arc" "a") nil)
    ("\\.zoo$" ("zoo" "aP") nil)
    ))

;; use pkzip with manipulating zip files (t) from within dired (use zip
;; and unzip otherwise)
(setq archive-zip-use-pkzip nil)

;; add these file types to archive mode to allow viewing and changing
;; their contents
(add-to-list 'auto-mode-alist '("\\.[ejrw]ar$\\'" . archive-mode))

;; modify the dired-extract switches to use the directory
;; ~/download/tryout as the default extract directory for zip files
(defconst MY_TRYOUT_DIR "~/downloads/tryout"
  "Directory for extracting files")

(setq dired-extract-alist
      `(
    ("\\.u\\(ue\\|aa\\)$" . dired-uud)
    ("\\.jar$" . "jar -xvf %s")
    ("\\.tar$" . ,(concat "tar -xf %s -C " MY_TRYOUT_DIR))
    ("\\.tgz$\\|\\.tar\\.g?[zZ]$" . ,(concat "tar -xzf %s -C " MY_TRYOUT_DIR))
    ("\\.arc$" . "arc x %s ")
    ("\\.bz2$" . ,(concat "bunzip2 -q %s"))
    ("\\.rar$" . ,(concat "unrar x %s " MY_TRYOUT_DIR "\\"))
    ("\\.zip$" . ,(concat "unzip -qq -Ux %s -d " MY_TRYOUT_DIR))
    ("\\.ear$" . ,(concat "unzip -qq -Ux %s -d " MY_TRYOUT_DIR))
    ("\\.war$" . ,(concat "unzip -qq -Ux %s -d " MY_TRYOUT_DIR))
    ("\\.zoo$" . "zoo x. %s ")
    ("\\.lzh$" . "lha x %s ")
    ("\\.7z$"  . "7z e %s ")
    ("\\.g?[zZ]$" . "gzip -d %s")   ; There is only one file
    ))
Badge answered 20/4, 2012 at 12:15 Comment(3)
loading dired-a fails, and it doesn't seem to be a package I can find anywhereSchuss
also doing a dired copy does not appear to work as you describe, I think you must have an extension to enable this, I just get an error that the target must be a directorySchuss
I found dired-a here: github.com/NikolausDemmel/emacs-pkgs/blob/master/misc/… and also here: groups.google.com/…Brainstorming
A
1

Platform: Ubuntu

Requirements

System:
sudo apt-get install atool

Emacs:
M-x package-list-packages
C-s dired-atool RET
i
x

Solution

1) Mark the files in dired buffer.
2) M-x dired-atool-do-pack NOTE: Make sure that shell-file-name is set to "/bin/bash".

Angularity answered 28/8, 2017 at 20:10 Comment(0)
A
0

In answer to the latter question, another tool which can be used to generate a compressed tar file from dired is pack. Mark the desired files and execute pack-dired-do-pack, specifying whatever.tar.gz as the output file name.

Aspectual answered 7/10, 2019 at 21:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.