You have a text fine containing several lines of text. You want to loop through each line so write a for loop in your bash script saying,
for lines in `cat somefile`;do
But wait a minute, your code is now processing word by word and not line by line. Whats happening? It is basically using space as the separator by default. So what do you have to do now. Use line break(\n) as your separator. How do you do that, IFS. IFS stands for Internal Field Separator.
So now if I add the line,
to my script before my for loop. Bash would now start looping through the lines rather than words.
A friend of mine asked for an easy way to create an image grid of a specific dimension with four images. Two in each row. The method shown here can be slightly varied to accommodate other image grid sizes or individual image sizes easily.
I had to create six image grids from four image files each. So totally i had 24 images split into different folders, each folder corresponding to images needed for a particular image grid. Since this was a repeated task, and it could be done using just the terminal with Image Magick. I love Image Magick. I created a script file with the following code.
convert a.png -resize 246x246\! a_e.png
convert b.png -resize 246x246\! b_e.png
convert c.png -resize 246x246\! c_e.png
convert d.png -resize 246x246\! d_e.png
montage a_e.png b_e.png c_e.png d_e.png -geometry 246x246+2+2 collage.png
That is it.
- The first line is just letting the computer know that you are using bash script.This script has to be executed from the same folder as these images, else it wouldn’t work.
- Lines two to five convert individual images named a, ,b, c and d to their edited images of uniform size, a_e, b_e, c_e and d_e respectively. We use the convert command for this. It is part of the Image Magick package. Did i tell you how much I love image magick?.
- The resize parameter resizes the images. 246×246 specifies that the image size should be 246 pixels wide and 246 pixels high.
- The exclamation mark(!) ignores aspect ratio. Since all the images i had were almost square i did not have to bother about the aspect ratio.
- It is required that all the images be the size when you create an image grid, else the results would not be favorable. But you will get results though.
- Now it is time to make our collage from these four images. We use the montage command for this. Also from the Image Magick package.
- In the montage command, first you specify the list of images you want to use. The geometry parameter is used to specify the size of each image and the number of rows and columns in the image grid. Then finally the name of the output file.
- 246×246 shows the width and height of the individual images, +2+2 shows the rows and columns of the image grid respectively.
With this script, I was done creating those image grids in less than five minutes. Took five minutes to google it though. 🙂 I did not know about montage before this post. You can read about resizing and montage using their links in this post. In case you have special needs.
The final result using duplicates of a sample image as my a, b, c, d files is this.
This was real fun. I had to create an gif from a set of images and i knew for sure that Image Magick can do it and yes it can. Its pretty simple.
Move all the images you want in the gif into a folder. cd to that folder. Now type in,
convert -delay 50 -loop 0 *.jpg myimages.gif
This would create a gif file called myimages.gif that is made up of all the images in the folder. The order of these images would be the the order in which ls lists them on the terminal. So change the names accordingly.
The delay parameter can be used to tweak how fast/smooth the animation should be. Loop set to 0 loops the image infinitely as is common with all gifs. *.jpg assumes that all your images are jpg. If they are in some other format use as necessary or you could just say *. Since the images are the only ones in the folder.
This could be really useful. As usual, I’m documenting this for myself as well.
To do this you need to understand something called a Visual Block in Vim. People who don’t want to understand can skip this paragraph. A visual block selection is a way of selection by which you consider the characters in the text file as part of a matrix. So when you make a visual selection, you could select one column or multiple columns. Selection by Columns!. That is something you cannot find in your usual lightweight text editor right?
Anyways, the process goes like this. Use Ctrl + v to begin visual block selection. Select the first column of the lines you want to comment. Then, Shift + i + <whatever_comment_character>. Then press Esc to exit the visual block selection mode. Give it a second to process. You should see the comment character on the lines that you selected.
To remove comments, follow a similar procedure. Use visual block selection to select the column of comment characters by pressing Ctrl + v and then using the arrow keys to select them. Then press x to cut them away. Done!
Comparing two files is easy on linux and this can be really useful. In my case, I frequently receive folders on my share folder that I make a copy of and work on. Sometimes I forget if I ever made a copy of it and worked on it. If the folder that looks to be a copy is really a copy of that folder or something else. Diff comes to the rescue.
diff -r path/to/dir1 path/to/dir2
This gives me a list that tells me if these files have been changed. Diff looks at these files as binary files so it is not looking at the semantics of the files. If it finds a change, any sort of change, it will report it to you. You can look into it and see if there is something wrong.
Binary files /home/data/text1.docx and data/text1.docx differ
This basically tells me that there is a difference in these two files. Depending on how many files you have, you would probably get many more.
You would also get something like these if there files only in one of the two folders
Only in /home/data: 01.wav
Really simple. But i keep forgetting this all the time. So wanted to write this down somewhere.
Find the drive that you want to erase.
You will require root permission for this. So do what is necessary for that.
su -c or
sudo or whatever. This lists all the drives that you have in your system with their addresses. Usually with just one hard disk, and another target drive, the target drive should be /dev/sdb and your hard disk should be /dev/sda.
Now type in,
mkfs.vfat -I /dev/sdx
This should begin formatting. This would require root permission as well. Replace sdx with what ever drive is your target.
You are done.
There is a way to manually write zeros as well. You do this by typing in
dd if=/dev/zero of=/dev/sdx
if is the input file system. of is the output file system. What you are basically doing is writing zeros on the output file system. Here again, replace sdx with your target drive. Once this is done, you can use the mkfs.vfat command used above to format the drive to a fat file system.