bash

A non-exhaustic list of bash commands for everyday work

Below is a list of useful bash commands for everyday work. I have just created the repository, more contents shall be added soon.

  1. Audio and Video
  2. Dates and Times
  3. Download
  4. Images
  5. PDFs

Audio and Video

Get bash script here.

Trim audio

ffmpeg -ss 20 -i input.mp3 -t 10 -c copy output.mp3

The option -ss 20 starts trimming from 20 sec of the input audio and -t 10 signifies that output audio length is 10 sec.

Concatenate audio

mp3wrap output.mp3 input1.mp3 input2.mp3

Merge video and audio

With audio re-encoding

ffmpeg -i video.mp4 -i audio.wav -c:v copy -c:a aac output.mp4

Without audio re-encoding

ffmpeg -i video.mp4 -i audio.wav -c copy output.mkv

Replacing audio stream

ffmpeg -i video.mp4 -i audio.wav -c:v copy -c:a aac -map 0:v:0 -map 1:a:0 output.mp4

Merge video and subtitle (.srt) file

ffmpeg -i video.mp4 -i subtitle.srt -c copy -c:s mov_text output.mp4

Dates and Times

Get bash script here.

View Clock with xclock

xclock -update 1 -geom 900x900

The -update option signifies how often the clock is updated in seconds. The -geom or -geometry is the resolution (size) of the clock.

View calender with cal

cal

To print the current year’s calendar, use

cal -y

Putting a year after -y prints calender of that year.

View dates and times with date

date

Download

Download with wget

Simply put the URL of the file you want to download as an argument to wget.

wget [URL]

To download file and save under specific name, run

wget -0 [name] [URL]

To download in background, run

wget -b [URL]

Resume download with wget

To resume/continue downloading a file, run

wget -c [URL]

Download multiple files with wget

Make a list of all the URLs (one per line) in a text file downloadlist.txt and then run

wget -i downloadlist.txt

Images

Resize images

To reduce the size of the image input.jpg to 40%, run

convert -resize 40% input.jpg output.jpg

The target size may also be specified:

convert -resize 720x480 input.jpg output.jpg

Convert transparent PNG to JPG by adding background

The following example converts image.png to image.jpg by adding a white background

convert image.png -background white -flatten -alpha off image.jpg

Metadata

We are using ImageMagick here. To read metadata, use

identify -verbose /path/image.jpg | grep exif

To remove all metadata, use

mogrify -strip /path/image.jpg

PDFs

Get bash script here.

Turn images into PDFs

convert 1.jpg 2.jpg output.pdf

To convert all JPG images in the directory, use

convert *.jpg output.pdf

Extract pages from PDFs

To extract pages 10-12, 15 and 21-22 from input.pdf and compile them into out.pdf, use

pdftk input.pdf cat 10-12 15 21-22 output out.pdf

Extract imbedded images from PDFs

To extract images in their original formats:

pdfimages -all input.pdf /tmp/out

To extract images in JPEG:

pdfimages -j input.pdf /tmp/out

Merge several pdf files into one pdf file

pdftk infile1.pdf infile2.pdf infile3.pdf cat output outfile.pdf