Archive

Archive for the ‘bash’ Category

get the UUID of a partition

July 30, 2015 2 comments

Problem
You want to figure out the UUID of a partition. For instance, you want to mount a partition upon boot and thus you want to add it to your /etc/fstab.

Solution

$ sudo blkid

The blkid program … can determine the type of content (e.g. filesystem or swap) that a block device holds, and also the attributes (tokens, NAME=value pairs) from the content metadata (e.g. LABEL or UUID fields).” (source: man)

Update (20150803)
As msx pointed out in the comments, you can also write “lsblk -f“, which doesn’t require sudo rights at all.

U

Categories: bash Tags: , , ,

taking a screenshot about a webpage

July 11, 2015 Leave a comment

Problem

You know the URL of a webpage and you want to take a screenshot of it. For instance you want a thumbnail about the webpage.

Solution

It can be done very nicely with PhantomJS.

What is PhantomJS?
PhantomJS is a headless WebKit scriptable with a JavaScript API. It has fast and native support for various web standards: DOM handling, CSS selector, JSON, Canvas, and SVG.” (source)

How to install PhantomJS?
Follow the instructions here. Under Ubuntu I compiled it from source to get the latest version. Note that it takes a lot of time (about 30 minutes). Under Manjaro I could install it via yaourt and it took 1 minute (and got the newest version). The good news is that installation is not a problem.

How to take a screenshot?
If you download the source, you get a lot of example scripts. One of them is called rasterize.js, and this is exactly what we need.

$ phantomjs rasterize.js 
Usage: rasterize.js URL filename [paperwidth*paperheight|paperformat] [zoom]
  paper (pdf output) examples: "5in*7.5in", "10cm*20cm", "A4", "Letter"
  image (png/jpg output) examples: "1920px" entire page, window width 1920px
                                   "800px*600px" window, clipped to 800x600

Example #1:

phantomjs rasterize.js http://raphaeljs.com/polar-clock.html clock.png


Example #2:

phantomjs rasterize.js https://www.reddit.com/ red.png

It produced an image with dimension 600×3304. It’s too narrow, let’s fix that.

Example #3:

phantomjs rasterize.js https://www.reddit.com/ red.png 1024px

Its dimension is 1024×2432. Looks much better.

Example #4:
The previous image was too high. Let’s take a photo of that part that would be visible on our screen. For this we need to clip a window.

phantomjs rasterize.js https://www.reddit.com/ red.png "1024px*768px"

Great. Now scale it down to get a thumbnail.

Scaling down an image to thumbnail size

$ phantomjs rasterize.js https://www.reddit.com/ screenshot.png "1024px*768px"
$ convert -resize 250 screenshot.png thumb.jpg

The command convert comes from the ImageMagick package. Here we resize the image to width 250px. Convert will keep the image ratio, i.e. it figures out the height value.

Links

convert .wmv to .mp4

ffmpeg -i input.wmv -c:v libx264 -crf 23 -profile:v high -r 30 -c:a libfaac -q:a 100 -ar 48000 output.mp4

More info here.

Categories: bash Tags: , , , ,

open a terminal in a specified folder

Problem
From a script I want to launch a terminal that opens in a specified folder.

Solution
Thanks to #linux, I got two solutions:

1) I use konsole, which has an option for this:

konsole --workdir /tmp

It will open a terminal in the /tmp directory.

2) Change directory to the specified folder and launch the terminal:

cd /tmp; konsole

If you want to do it from a script, the idea remains the same.


If you want to execute a command automatically in the opening terminal, use this:

konsole -e mc

Find more info here.

Categories: bash, linux Tags: , ,

resize an image to a given width by keeping the aspect ratio

Problem
You want to resize an image to a given width but you want to keep the width/height ratio of the image.

Solution

convert -resize 200 big.jpg small.jpg

Here the new width will be 200 pixels.

Categories: bash Tags: , ,

edit crontab with emacs

March 8, 2015 Leave a comment

Problem
You can edit your crontab file with “crontab -e“. It worked fine as long as I used vim. However, when I switched to emacs and set the EDITOR environment variable to point to emacs, I couldn’t edit my crontab file normally. Emacs is started in the background and somehow it caused problems.

Solution
I made a script for editing my crontab file. This script sets EDITOR back to vim, thus “crontab -e” opens vim. Here is the script:

#!/usr/bin/env bash

# crontab_e

EDITOR=/usr/bin/vim
crontab -e

I call this script “crontab_e”, so it’s easy to remember its name.

Categories: bash Tags: , ,

Which process opened a file?

March 1, 2015 Leave a comment

Ever wondered who (which process) opened a given file? There is a Unix command to answer this question called lsof (“list open files”). Read more about it at wikipedia.

Demo
Execute the following script:

#!/usr/bin/env python3

# who.py

def main():
    f = open("/tmp/who.txt", "w")
    input("Press ENTER to continue...")
    f.close()

#####

if __name__ == "__main__":
    main()

Make it executable and run it. In another terminal issue the command “lsof /tmp/who.txt“. You’ll get a similar output:

$ cd /tmp

$ lsof who.txt 
COMMAND  PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
python3 1288  jabba    3w   REG   0,32        0   27 who.txt

$ ps 1288
  PID TTY      STAT   TIME COMMAND
 1288 pts/0    S+     0:00 python3 ./who.py

The file who.txt was opened by the process with PID 1288, which is the who.py script.

Categories: bash Tags: , , ,
Follow

Get every new post delivered to your Inbox.

Join 80 other followers