Skip to content

Small Tools



# 水平方向
convert +append *.png out.png
# 垂直方向
convert -append *.png out.png

参考 How do I join two images in Ubuntu?


# only specify the wide as 1024 pixel to keep the aspect ratio
convert input.png -resize 1024x out.png
convert input.png -quality 50% out.png

参考How can I compress images?


参考convert images to pdf: How to make PDF Pages same size


pdftk A.pdf B.pdf cat output merge.pdf


convert a.png b.png -compress jpeg -resize 1240x1753 \
                      -extent 1240x1753 -gravity center \
                      -units PixelsPerInch -density 150x150 multipage.pdf

注意重点是 -density 150x150,若去掉这个选项,则还是得不到相同页面大小的文件。



pdf 转为 jpg

-quality 100 控制质量 -density 600x600 控制分辨率


pdf 转 png 更好的命令是 pdftoppm,参考 How to convert PDF to Image?

pdftoppm alg.pdf alg -png -singlefile

图片质量比 convert 好很多!!

convert imgs to pdf

ls -1 ./*jpg | xargs -L1 -I {} img2pdf {} -o {}.pdf
pdftk likelihoodfree-design-a-discussion-{1..13}-1024.jpg.pdf cat output likelihoodfree-design-a-discussion.pdf

注意这里需要用 ls -1,如果 ll 则第一行会有 total xxx 的信息,即 ll | wc -l 等于 ls -1 | wc -l + 1,而且在我的 Ubuntu 18.04 中,ll 甚至还会列出



ffmpeg: 视频处理


参考 如何使用ffmpeg去除视频声音?

ffmpeg -i .\input.mp4 -map 0:0 -vcodec copy out.mp4


参考 ffmpeg 视频倍速播放 和 慢速播放


参考How can I rotate a video?


ffmpeg -i -vf "transpose=1"

然后报错 “The encoder ‘aac’ is experimental but experimental codecs are not enabled”

注意添加 -strict -2 要注意放置位置,一开始直接在上述命令后面加入,但失败,应该写成

ffmpeg -i -vf "transpose=1" -strict -2


ffmpeg -ss 00:00:30.0 -i input.wmv -c copy -t 00:00:10.0 output.wmv


  • (optional) -ss specifies the start timestamp, the format is
  • (optional) -t specifies the duration, or use -to to specifies the end timestamp

refer to Using ffmpeg to cut up video


A much more powerful command than top, refer to Find out what processes are running in the background on Linux


在找学习资料时,突然不是很确定当初是否已经在用这台笔记本了,所以想确定一下本机的装机时间,参考 How can I tell what date Ubuntu was installed?,主要时通过查看文件的上次修改时间,比如

$ ls -lt /var/log/installer/
total 1200
-rw-rw-r-- 1 root   root 464905 Dec  2  2016 initial-status.gz
-rw-r--r-- 1 root   root     60 Dec  2  2016 media-info
-rw------- 1 syslog adm  334743 Dec  2  2016 syslog
-rw------- 1 root   root   2467 Dec  2  2016 debug
-rw------- 1 root   root 407422 Dec  2  2016 partman
-rw------- 1 root   root     17 Dec  2  2016 version
-rw------- 1 root   root    956 Dec  2  2016 casper.log


$ ls -lt /
drwxrwxr-x   2 root root       4096 Dec  2  2016 cdrom
drwx------   2 root root      16384 Dec  2  2016 lost+found
drwxr-xr-x   2 root root       4096 Apr 21  2016 srv

出现了 2016.04.21 的一条记录。但如果我加上 -c,结果竟然不一样

$ ls -clt /
drwxrwxr-x   2 root root       4096 Dec  2  2016 cdrom
drwxr-xr-x   2 root root       4096 Dec  2  2016 srv
drwx------   2 root root      16384 Dec  2  2016 lost+found

难道 ls 默认显示的时间不是上次修改时间吗??另外注意到 srv 其实是一个空文件夹。

这时我用 stat 进一步查看,

$ stat /srv
  File: /srv
  Size: 4096        Blocks: 8          IO Block: 4096   directory
Device: 825h/2085d  Inode: 1179649     Links: 2
Access: (0755/drwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2021-05-05 08:43:20.955106697 +0800
Modify: 2016-04-21 06:07:49.000000000 +0800
Change: 2016-12-02 02:46:47.363728274 +0800
 Birth: -


  • Modify: the last time the file was modified (content has been modified)
  • Change: the last time meta data of the file was changed (e.g. permissions)

然后进一步查看 Windows 系统的时间,

$ ll -clt
drwxrwxrwx  1 weiya weiya       4096 Oct  1  2016 '$Recycle.Bin'/
drwxrwxrwx  1 weiya weiya          0 Sep 29  2016  FFOutput/
-rwxrwxrwx  2 weiya weiya   15151172 Jul  2  2016  WindowsDENGL.tt2*
-rwxrwxrwx  2 weiya weiya   16092228 Jul  2  2016  WindowsDENG.tt2*
-rwxrwxrwx  2 weiya weiya   16217976 Jul  2  2016  WindowsDENGB.tt2*
-rwxrwxrwx  1 weiya weiya     400228 Mar 19  2016  bootmgr*
-rwxrwxrwx  1 weiya weiya          1 Mar 19  2016  BOOTNXT*
drwxrwxrwx  1 weiya weiya       8192 Mar 18  2016  Boot/

最早可以追溯到 2016.03.18.

paste, cat: 文本文件拼接

### 按列
paste file1 file2 > outputfile
### 按行
cat file1 file2 > outputfile

ps2pdf, pdf2ps

reduce pdf file size

It can be used to reduce the pdf size.

Generally, there are two major reasons why PDF file size can be unexpectedly large (refer to Understanding PDF File Size).

  • one or more fonts are stored inside PDF document.
  • using images for creating PDF file.

I just got a large non-scanned pdf with size 136M, and it probably is due to many embedded fonts which can be checked in the properties.

Then I tried the command ps2pdf mentioned in Reduce PDF File Size in Linux, the file size is significantly reduced, only 5.5M!

$ ps2pdf -dPDFSETTINGS=/ebook Puntanen2011_Book_MatrixTricksForLinearStatistic.pdf Puntanen2011_Book_MatrixTricksForLinearStatistic_reduced.pdf
$ pdfinfo Puntanen2011_Book_MatrixTricksForLinearStatistic.pdf 
Producer:       Acrobat Distiller 8.0.0(Windows)
CreationDate:   Tue Jul 26 20:43:43 2011 CST
ModDate:        Fri Aug 19 19:57:50 2011 CST
Tagged:         no
UserProperties: no
Suspects:       no
Form:           AcroForm
JavaScript:     no
Pages:          504
Encrypted:      no
Page size:      439.37 x 666.142 pts
Page rot:       0
File size:      142394146 bytes
Optimized:      no
PDF version:    1.3
$ pdfinfo Puntanen2011_Book_MatrixTricksForLinearStatistic_reduced.pdf 
Producer:       GPL Ghostscript 9.26
CreationDate:   Tue Apr 13 18:04:57 2021 CST
ModDate:        Tue Apr 13 18:04:57 2021 CST
Tagged:         no
UserProperties: no
Suspects:       no
Form:           none
JavaScript:     no
Pages:          504
Encrypted:      no
Page size:      439.37 x 666.14 pts
Page rot:       0
File size:      5766050 bytes
Optimized:      no
PDF version:    1.4

flatten pdf file

Flattening a PDF means to merge separated contents of the document into one so that,

  • Interactive elements in PDF forms such as checkboxes, tex boxes, radio buttons, drop-down lists are no longer fillable
  • Annotations become “native text”
  • Multiple layers of text, images, page numbers, and header styles turn into one single layer.

An easy way is

pdf2ps orig.pdf - | ps2pdf - flattened.pdf

some alternatives can be found in is-there-a-way-to-flatten-a-pdf-image-from-the-command-line.


Ubuntu 18.04 和 CentOS 7 中的 rename 不一样,

# Ubuntu 18.04
$ rename -V
/usr/bin/rename using File::Rename version 0.20
# CentOS 7
$ rename -V
rename from util-linux 2.23.2

用法也有差异,前者采用类似 sed 格式语句进行替换

rename -n 's/Sam3/Stm32/' *.nc  /*确认需要重命名的文件*/
rename -v 's/Sam3/Stm32/' *.nc  /*执行修改,并列出已重命名的文件*/


rename Sam3 Stm32 *.nc



send mail on the command line. On the stapc-WSL, install it via

$ sudo apt install sendmail

monitor the updates of /var/log/apache2/error.log. If the modified time is recent, then send email to alert.

while true; do
    last_date=$(date -r /var/log/apache2/error.log +%s)
#   curr_date=$(date -d "-1 mins" +%s)
    sleep 1m
    curr_date=$(date +%s)
    if [[ $last_date > $curr_date ]]; then
            cat <<-EOT
            TO: ${TO}
            From: ${FROM}
            Subject: ${SUBJ}

            There is some updates on the /var/log/apache2/error.log

        ) | sendmail -v ${TO}

其中 date -r file 返回文件的上次修改时间,而 +%s 将时间转换为 seconds,方便进行比较,另外 -d "-1 mins" 能对时间进行加减处理。



  • -f: output appended data as the file grows, powerful for checking the log file in real time.


wget a series of files in order



参考 Wget a series of files in order

wget vs curl

wget 不用添加 -O 就可以将下载的文件存储下来,但是 curl 并不默认将下载的文件存入本地文件,除非加上 -o 选项,而 wget-O 只是为了更改文件名。


curl -sL | apt-key add -

更多比较详见 What is the difference between curl and wget?


如果 zip 文件解压乱码,可以试试 unar,

采用 unar

参考 Linux文件乱码