Category Archives: Bash-Scripts

How to extract video URLs from YouTube

I know there are many tools out there that allow you to download videos from YouTube, like the Video Download Helper for Firefox, even online tools that convert the audio to mp3. The reasons I’m writing this article are a) because sometimes you just need the URL, for example to watch a video with a much less ressource consuming player like VLC or ffplay, b) because sometimes you just want to listen to the audio and save up even more ressources by not decoding the video and c) because I can 😉
Read the rest of this entry

How to create your own PKI with openssl

Today certificates are widely used to verify, authenticate a client/user or server, to encrypt or sign emails or to sign other types of objects (e.g. source code). You are using a certificate at the moment, due to the secure http (https) connection.
In this post I will show you how to create your own Root Certificate Authority (CA).

1. Create a self signed root certificate
2. Create a sub ca certificate
3. Create a server certificate
4. Create a user certificate
5. Generate a certificate revocation list
6. Revoke a certificate
7. Export a certificate to PKCS#12 format
8. Bash script to manage your own CA

I recommend to configure your openssl.cnf (located at /etc/ssl/openssl.cnf). This is the most annoying part, but it simplifies the next steps. You can find an example of an openssl.cnf I’ll use at the end of this post. Be careful at the policy_match section, this can be a problem while signing a certificate signing request.
Read the rest of this entry

How to make daily backups with rsync and cronjobs

It’s always a good idea to make backups of important data (documents, images, music, etc.), e.g. the worst case for students is to lose data of written reports or theses.
I wanted to make backups of my documents and source codes every day. The backup should be stored on a usb flash drive. But I also wanted to keep deleted files for a couple of days, in case of accidentally deleted files. There are many ways to complete this task, but I decided to use rsync and cronjobs for this purpose. Both tools make the backup process very flexible. It’s very easy to manage the time of the backup process with cronjobs and rsync makes it easy to change the destination (e.g. another folder, device or remote host).
My idea was to add a cronjob to start a bash script which uses rsync to copy the directories and files to the flash drive. But the device names of flash drives changes, if you plug them in a different order. To avoid this problem, I added a udev rule to create a symbolic link every time the flash drive is plugged.
Read the rest of this entry

How to trim strings with bash

If you want to trim a string from or to a certain pattern, you don’t have to invoke cut, sed or awk. You can use the % operator to trim a string from the first or last occurrence of a pattern to the end or the # operator to trim from the beginning to the first or last occurrence of it.
Read the rest of this entry

How to transcode a directory of audio files to AAC with FFmpeg

I recently started to backup my CDs as lossless flac files on my home server. The problem is, my mobile phone cannot play back flac files, so I wrote a little bash script that transcodes a directory of audio files to high quality variable bitrate AAC files that I can play back on my mobile phone. Read the rest of this entry

How to get the top 10 recently used commands

The file .bash_history in a users home-directory saves the recently used commands. You can modify the maximum number of lines that will be stored in .bash_history. You only have to change the value of the global bash variable $HISTFILESIZE. For much more details have a look at the bash man-page.

We use sort, uniq and head to get the top 10 recently used command.
Read the rest of this entry

How to remove overscan on HDMI 1080p output using xrandr

I recently got a Asus 24T1E TV monitor with integrated DVB tuner and lots of connectors on the back, two of which are HDMI inputs. When I first connected my Laptop to it via HDMI and turned it to 1920×1080 (native resolution of the monitor) I encountered that the picture was kind of blury and the borders of the desktop were outside of the screen, i.e. 52 pixels on the left and right and 26 pixels on the top and bottom border. The info pannel on the monitor’s OSD reported “1080p” as resolution.

Read the rest of this entry

How to generate a random password using command-line tools

Here is a little command-line example to generate a random password.

To generate a random string you can use /dev/random or /dev/urandom. Both are kernel random number source devices. Read the man page to get detailed information. But if you use
cat /dev/urandom
you’ll get printable and non-printable bytes. Non-printable bytes are useless for a password. We have to use a filter to get only printable character. For this purpose we can use tr.
In this case SET is the charset we want, e.g. only lowercase chars, only digits, etc. So we have to delete all chars execpt SET. To delete these “bad chars” we can use the options cd. c is the complement of SET and d deletes the chars in SET.
The last thing we have to set is the password length. For this purpose we’ll use the head-command with option c. This option will print only the first N bytes.

A little example:
cat /dev/urandom | tr -cd [:graph:] | head -c 10

Read the rest of this entry

How to encrypt a string with ROT13 (one liner)

ROT13 is a variation of the caesar chiffre. It replaces each letter with the letter 13 positions after that letter. A nice side effect: you need only a function to encrypt a string. If you encrypt an encrypted string, you’ll get the plaintext.

echo 'This is a Test' | tr 'A-Za-z' 'N-ZA-Mn-za-m'
Output: Guvf vf n Grfg

echo 'Guvf vf n Grfg' | tr 'A-Za-z' 'N-ZA-Mn-za-m'
Output: This is a Test

How to play flash videos with the media player of your choice

Notice: This method will probably not work on all YouTube videos.

Remember the good old days when you could pause a flash video, let it buffer and play the video with the media player of your choice by opening the corresponding file from the /tmp directory?

Well that’s still possible. However the video-files are deleted from the /tmp directory. So you need to find the PID of the flash plugin-container to open the video from /proc/PID/fd/.
Read the rest of this entry