Useful commandline tools in Linux/Mac OS X
Contents
This article keeps collecting useful command-line tools in Linux or Mac
OS X (via homebrew
). It will be continuously updated.
It includes the following functions:
-
tools for downloading videos or webpages;
-
useful tools on dealing with documents (
.txt
,.pdf
,.djvu
, etc.); -
tricks on image operations, like resize, format conversion, etc.; and
-
basic video operations.
Download
YouTube videos
To check the available formats:
|
|
After downloading, trim the suffixed random ID strings:
|
|
To download the best video with MP4 format:
|
|
To download the best MP4 with quality equal or less to 480p:
|
|
To download the best MP4 with quality <= 480p and the best audio in M4A:
|
|
To download items (e.g., 1, 2, …, 10, 12, 18):
|
|
When the default file name is too long (by default, it uses the video title in Youtube), you may specify a file name by a random ID string plus the extension:
|
|
If you encounter the following error due to Youtube age restriction,
ERROR: Content Warning This video may be inappropriate for some users. Sign in to confirm your age
you may handle by explicitly specifying cookies
-
use Firefox adds-on
Export cookies
to export the webpage (which video you want to download) cookies to a place, e.g.,~/Downloads/cookies.txt
; -
pass this cookies file to
youtube-dl
as follows:
|
|
Web pages
Download all children webpages: wget -r -np -R "index.html*" URL
Documents
Plain Text
Convert non-unicode Chinese texts to UTF-8:
|
|
The Chinese encoding majorly used include:
-
gbk
-
gb18030
-
gb2312
-
big5
for traditional Chinese
DjVu
Installation:
-
djvu encoding/decoding library support:
brew install djvulibre
-
djvu default viewer:
brew install djview
(orbrew install djview4
for its stable version 4) -
tools to convert djvu to pdf:
-
use
ddjvu
command fromdjvulibre
, or -
install the tool:
brew install djvu2pdf
-
Usages:
-
Break Djvu into separate pages:
1
$ djvmcvt -i input.djvu /path/to/out/dir output-index.djvu
-
Convert Djvu pages into images:
1
$ ddjvu --format=tiff page.djvu page.tiff
-
Convert Djvu pages into PDF:
1
$ ddjvu --format=pdf -quality=85 inputfile.djvu ouputfile.pdf
or, use
djvu2pdf
tool:1
$ djvu2pdf in.djvu
-
You can also use –page to export specific pages:
1
$ ddjvu --format=tiff --page=1-10 input.djvu output.tiff
This will convert pages from 1 to 10 into one tiff file.
Add margins to PDF files:
-
use
pdfcrop
provided byTexLive
1
pdfcrop --margin '29 0 29 0' input.pdf output.pdf
The four digits represent 'LEFT TOP RIGHT BOTTOM', respectively.
-
use
pdfjam
provided byTexLive
to do the offset trick for the right and left pages1
pdfjam --twoside --offset '1cm 0cm' file.pdf
Create a blank PDF file in A4:
|
|
or, with a specific size:
|
|
Dictionary
This article shows how to convert the BGL dictionary to the AppleDict format for Dictionary.app.
The key tool to convert formats is the Python package pyglossary
on
github. You may clone this project, or install from PyPI by $pip
install pyglossary
.
Moreover, we need additional tools to support AppleDict reading and writing:
-
Required Python libraries for AppleDict:
-
Reading from AppleDict Binary (.dictionary)
sudo pip3 install lxml
-
Writing to AppleDict
sudo pip3 install lxml beautifulsoup4 html5lib
-
-
Requirements for AppleDict on Mac OS X: if you want to convert glossaries into AppleDict format on Mac OS X, you also need:
-
GNU make as part of Command Line Tools for Xcode.
-
Dictionary Development Kit as part of Additional Tools for Xcode. Extract to
/Applications/Utilities/Dictionary Development Kit
(if using github clone), or~/Developer/Extras/Dictionary Development Kit
(if using pip installation).
-
Now it is ready to use pylossary
to convert a BGL dictionary to
AppleDict, e.g., "Merriam-Webster.Collegiate.Dictionary.BGL":
|
|
Images
Infomation
To display image meta information:
magick identify -verbose IN.png
Resize
Resize image with fixed ratios (the height and width may be exactly 64,
which will be modified by the ratio):
convert dragon.gif -resize 64x64 resize_dragon.gif
Resize image and ignore ratios:
convert dragon.gif -resize 64x64\! exact_dragon.gif
Only shrink larger images:
convert dragon.gif -resize 64x64\> shrink_dragon.gif
Resize by percentages:
convert dragon.gif -resize 50% half_dragon.gif
Conversion
color
Convert color images into grayscale:
convert --colorspace gray IN.jpg OUT.jpg
SVG
DEPENDENCY: require the package librsvg
; install by
brew install librsvg
Convert img.svg to img.jpg with height set to 1500px:
rsvg-convert -h 1500 img.svg img.jpg
Convert img.svg to img.pdf (vector image):
rsvg-convert -f pdf -o img.pdf img.svg
JPEG/PNG
Convert img.png to img.jpg with density 300, quality 100 and trimmed:
convert -density 300 -trim -quality 100
webp
DEPENDENCY: require the package webp
; install by brew install webp
Convert img.webp to img.jpg:
dwebp img.webp -o img.jpg
parallel conversion
Use 8-cores to convert images to pdf in parallel:
|
|
Combination
Concatenate all images vertically as a new one:
convert -append *.png out.png
Quality/Size reduction
Reduce file size of an image:
convert -resize 1000 -trim -quality 72 IN.jpg OUT.jpg
Videos
FFMPEG
Convert flv to mp4 without losing quality:
|
|
Alternatively, the following only changes container without reencoding:
|
|
AVCONV
Combine video file mp4
with sound track m4a
(useful when you
download video and audio files separately):
|
|
Author oracleyue
LastMod 2021-02-18