TIPS &TRICKS
Clearing the terminal screen
Enter ‘clear’ without quotes in the terminal and hit the Enter button. This causes the screen to be cleared, making it look like a new terminal.
—Abhinay B, ohyesabhi2393@gmail.com
Convert image formats from the command line in Ubuntu
‘convert’ is a command line tool that works very well in many Linux based OSs. The ‘convert’ program is a part of the ImageMagick suite of tools and is available for all major Linux based operating systems. If it is not on your computer, you can install it using your package manager.
It can convert between image formats as well as resize an image, blur, crop, dither, draw on, flip, join, and resample more from your command line.
The syntax is as follows:
convert [input options] input file [output options] output file.
For example, we can convert a PNG image to GIF by giving the following command:
convert image.png image.gif
To convert a JPG image to BMP, you can give the following command:
convert image.jpg image.bmp
The tool can also be used to resize an image, for which the syntax is shown below:
convert [nameofimage.jpg] resize [dimensions] [newnameofimage.jpg]
For example, to convert an image to a size of 800 x 600, the command would be as follows: convert image1.jpg -resize 800x600 newimage1.jpg —Anirudh K, anirudh.3194@gmail.com
Passwordless SSH to remote machine
It can be really annoying (mostly in the enterprise environment) when you have to enter a password each time while doing an SSH to a remote machine. So, our aim here is to do a passwordless SSH from one machine (let’s call it host A/User a) to another (host B/User b).
Now, on host A, if a pair of authentication keys are not generated for User a, then generate these with the following commands (do not enter a passphrase):
a@A:~> ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/a/.ssh/id_rsa): Created directory ‘/home/a/.ssh’.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/a/.ssh/id_rsa. Your public key has been saved in /home/a/.ssh/id_rsa.pub. The key fingerprint is:
3e:4f:05:79:3a:9f:96:7c:3b:ad:e9:58:37:bc:37:e4 a@A
This will generate a public key on /home/a/.ssh/id_rsa.pub. On host B as User b, create ~/.ssh directory (if not already present) as follows:
a@A:~> ssh b@B mkdir p .ssh b@B’s password:
Finally, append User a’s new public key to b@B:.ssh/ authorized_keys and enter User b’s password for one last time:
a@A:~> cat .ssh/id_rsa.pub | ssh b@B ‘cat >> .ssh/authorized_ keys’ b@B’s password:
From now on, you can log into host B as User b from host A as User a without a password:
a@A:~> ssh b@B —Ashay Shirwadkar, ashayshirwadkar12@gmail.com
Performance analysis of code
In order to check the performance of the code you have written, you can use a simple tool called ‘perf’. Just run the following command:
$sudo aptget install linuxtoolscommon linuxtoolsgeneric
The above command will install the ‘perf’ tool on Ubuntu or a similar operating system.
$perf list
The above command gives the list of all the information that can be got by running ‘perf’.
For example, to analyse the performance of a C program and if you want to know the number of cachemisses, the command is as follows:
$perf stat e cachemisses ./a.out
If you want to use more than one command at a time, give the following command:
$perf stat e cachemisses,cachereferences ./a.out —Gunasekar Duraisamy, dg.gunasekar@gmail.com
Create a QR code from the command line
QR code (abbreviated from Quick Response Code) is a type of matrix bar code (or two-dimensional bar code) first designed for the automotive industry. There are many online websites that help you create a QR code of your choice. Here is a method that helps generate QR codes for a string or URL using the Linux command line: $echo “Tips and Tricks” | curl F=\< qrenco.de
To generate the QR code for a domain, use the following code:
$echo “http://opensourceforu.com” | curl F=\< qrenco.de
Note: You need a working Internet connection on your computer.
—Remin Raphael, remin13@gmail.com
Replace all occurrences of a string with a new line
Often, we might need to replace all occurrences of a string with a new line in a file. We can use the ‘sed’ command for this:
$sed ‘s/\@@/\n/g’ file1.txt > file2.txt
The above command replaces the string ‘@@’ in ‘file1. txt’ with a new line character and writes the modified lines to ‘file2.txt’.
sed is a very powerful tool; you can read its manual for more details.
—Nagaraju Dhulipalla, nagarajunice@gmail.com
Git: Know about modified files in changeset
Running the plain old ‘git log’ spews out a whole lot of details about each commit. How about extracting just the name of the files (with their path relative to the root of the Git repository)? Here is a handy command for that:
git log m 1 nameonly pretty=”format:” HEAD
Changing the HEAD to a different SHA1 commit ID will fetch the names of the files only. This can come in handy while tooling the CI environment.