commands

editing commands


The diff command reveals differences between two files, if any. Sometimes commands need LC_ALL=C prepending them on the command line.

# file transfer commands

Give the sftp command the login/server info. Use -p for preserving the dates and permissions for any files transferred, and -r for recursively copying directories. Use a colon to delimit the login/server from the optional path, which defaults to the home directory of the account.

> sftp -pr account@domain.tld:/some/path

Similarly, in emacs the dired command can be evoked with a colon-delimited remote path, t.i. a protocol, a login/server, and a path.

/scp:account@domain.tld:/some/path

...or even with ssh:

/ssh:account@domain.tld:path

The default protocol is evoked with "-", which usually resolves to scp. (Actually, it references the variable tramp-default-method, and that method is usually named "scp", which itself uses scp.) The default login/server is the current login/server, and the default path is the home directory for the given login/server. Therefore, "/-::" implies these defaults of "/scp:account@computer.local:~", for whatever the current account and computer name happens to be.

For example, Controlx d will evoke dired (directory edit) by prompting in the minibuffer for the path of a directory. Or, within dired use Shiftc for copying a file, and it asks for the destination path. Simply type a remote path and the marked files will be copied to the remote destination, even with ssh.

# disks

List disks and partitions with lsblk -p, mount a disk with mount, unmount a disk with umount, and always sync before disconnecting a disk.

# searching with find and grep

Find all html files then grep for "Text". The "{}" is replaced by find with each filename; or "{} +" to use as many filenames as possible for the command executed (for possibly greater efficiency).

> find . -name "*.html" -exec grep -l "Text" {} +

For file name searches only (f.e. for all HTML files, including ".htm": -name '*.htm*'), the emacs command ESC x find-name-dired prompts for a directory for recursively searching and then a shell wildcard pattern (case-insensitive, without quotes), which returns a dired listing of results. Then use % g to mark all files whose contents contain a match for a regular expression.

# searching files within dired

Within dired the command ESC x dired-do-find-regexp (shortcut: Shifta) will search marked files or recursively search a marked directory for a regexp matching file contents.

Within dired the command ESC x dired-do-isearch-regexp (shortcut: ESC s a ESC Controls) will interactively search file contents of marked files.

# ImageMagick

The ‑crop option will crop multiple times within an image when the geometry is only the size without a position ("#x#", rather than "#x#+#+#"). Saving to a TIFF results in multiple images within it.

Exporting all images from a TIFF can be done with the percent format "%d" in the output name, along with the -scene option for the starting number.

Using ‑repage is generally a good idea when cropping or rotating.

The ‑virtual‑pixel option is needed for affecting the color of pixels for rotations other than 90° increments, and its default method value is edge. The ‑virtual‑pixel method black, as in:

-virtual-pixel black
is similar to:
-background "#000" -virtual-pixel background

# compression

Compression is specified with -compress followed by type: None, BZip, Fax, Group4, JPEG, JPEG2000, Lossless, LZW, RLE or Zip. List all types with:

> magick -list compress

The "Lossless" type is for JPEG, when available. The ICO format uses PNG compression by specifying "Zip", t.i. -compress ZIP.

Compression for JPG for line drawings has seemed pretty much unnoticeable at 50% (‑quality 50), and minor blurring noticeable at 25% when zoomed. However, 20% compression seems okay and also the lowest acceptable, which has resulted in approximately 1% of the TIFF size. At 15% compression there has been large swaths of color noticeable.

# image info

Identify the current dimensions, the resolution depth and units, and the compression type and quality.

> identify -format "%wx%h, %xx%y %U, %C %Q" image.tif

Identify the original dimensions (before resizing), image depth, and image colorspace.

> identify -format "%G, %[bit-depth], %[colorspace]" image.tif

Identify the list of any embedded profiles (or a warning).

> identify -format "%[profiles]" image.tif

# IM with dired's ! shortcut in emacs

Mark some files within dired and then type ! to evoke a shell command on them. Within the shell command use the "?" character for one file at a time, and the "*" character for all files at once.

Rotate clockwise:
mogrify +distort SRT 90 +repage ?
Crop based on gravity and percentages of size:
convert ? -set filename:f '%t' -gravity South -crop "100%x54%+0+0" +repage '%[filename:f]-2.jpg'
Create thumbnail:
convert ? -set filename:f '%t' -resize "240x240>" -density 72 '%[filename:f]_240px.jpg'
Convert to a JPG file and then as an additional thumbnail, too, with relative filename:
convert ? -set filename:f '%t' +write '%[filename:f].jpg' -resize "240x240>" -density 72 '%[filename:f]_240px.jpg'
or:
convert ? -set filename:f '%t' -resize "2560x2560>" -density 72 +write '%[filename:f]_2560px.jpg' -resize "240x240>" -density 72 '%[filename:f]_240px.jpg'
Convert to JPG at 20% quality, with same filename:
convert ? -set filename:f '%t' -quality 20 '%[filename:f].jpg'
Create PDF from a list of selected image files (but recompresses them):
convert * name-of-document.pdf

prepare image for a PDF

Size a 600 DPI image into a multiple of 50 for width and height (for scaling simply to 72 DPI in PDFs later), by first making the dimensions an even number. Also annotate the four corners with label indicating the side of paper, f.e. Front, Inside, or Back.

magick ? -set filename:f '%t' -background "#000" -splice "%[fx:w%2]x%[fx:h%2]" +repage -gravity Center -crop "%[fx:50*(trunc(w/50)-2)]x%[fx:50*(trunc(h/50)-2)]+0+0" +repage -bordercolor "#000" -border 150 -gravity NorthWest -font "Verdana" -pointsize 100 -fill "#FFF" -annotate +25+20 'Front' -annotate "90x90+%[fx:w-40]+25" 'Front' -annotate "180x180+%[fx:w-25]+%[fx:h-20]" 'Front' -annotate "270x270+40+%[fx:h-25]" 'Front' -quality 20 '%[filename:f].jpg'
And then the thumbnail:
magick ? -set filename:f '%t' -background "#000" -splice "%[fx:w%2]x%[fx:h%2]" +repage -gravity Center -crop "%[fx:50*(trunc(w/50)-2)]x%[fx:50*(trunc(h/50)-2)]+0+0" +repage -resize "200x200>" -density 72 -bordercolor "#000" -border 20 -gravity NorthWest -font "Verdana" -pointsize 16 -fill "#FFF" -annotate +20+2 'Front' -annotate "90x90+%[fx:w-4]+20" 'Front' -annotate "180x180+%[fx:w-20]+%[fx:h-2]" 'Front' -annotate "270x270+4+%[fx:h-20]" 'Front' -quality 20 '%[filename:f]_240px.jpg'

# multi-image TIFF

Use a shell when a TIFF has multiple images inside it to reference its first image.

Convert to a JPG file and then as an additional thumbnail, too, with relative filename:
> convert image.tif[0] -set filename:f '%t' +write '%[filename:f].jpg' -resize "240x240>" -density 72 '%[filename:f]_240px.jpg'
Rotate clockwise, then output as a JPG file and also a thumbnail:
> convert *.tiff[0] +distort SRT 90 +repage -set filename:f '%t' +write '%[filename:f].jpg' -resize "240x240>" -density 72 '%[filename:f]_240px.jpg'

# errors

Errors along the lines of "cache resources exhausted" probably indicate the "/etc/ImageMagick-[version number]/policy.xml" has been changed, perhaps when upgraded. Check the resources with:

> magick -list resource

Typically, the policy has had nothing set at all, so simply comment out anything in that file in order to use the defaults (of nothing set at all).

# renumber file names

View a directory of files named with numbers within dired with Controlx d (command: ESC x dired), hide details with the shortcut ( (command: ESC x dired-hide-details-mode) to show only filenames, and then use Controlx Controlq (command: ESC x dired-toggle-read-only) to switch to write mode. A message appears saying how to save changes (Controlc Controlc), or cancel (Controlc ESC).

Search with a regular expression using ESC Controls for matching the numbers, for example this matches at least two digits:
[0-9]\{2,\}
Then replace matches using ESC % by adding a number to the matched number, and using format to convert the number back to regular text. For example, add 1 to matched numbers:
\,(format"%d"(+ \#& 1))

The \, is for using lisp for the replacement; see 15.10.2 Regexp Replacement. Within that, the \#& conveniently converts the whole match to a number (when possible). The same prefix "\#" works for parenthesized matches like \#1 and so on. Also, multi-digit parenthesized references are possible when using lisp with a replacement, t.i. beyond 1–9. For example, \27 for the 27th parenthesized match, or the numerical value of the match with \#27.

After all desired editing is done, use Controlc Controlc to save the changes to the filenames, and then read-only mode will be enabled again.

# inserting text results

Insert the results of a calculation using the common command "eval-expression" for evaluating LISP (shortcut ESC :).
ESC : (insert(format"%d"(*(/ 5400 50)6)))
Insert the number of characters or bytes between point and mark, such as of the current selection.
ESC : (insert(format"%d"(-(mark)(point))))
The emacs command ESC x shell-command (shortcut: ESC !) will insert the results of a shell command at point in the current buffer when used with the universal argument (Controlu). For example, to insert the width of an image into the buffer using identify from ImageMagick:
Controlu ESC ! identify -format "%w" image.jpg

More in:
installing emacs on macOS
begin