Auto-mount disks on Startup


sudo fdisk -l

See which disk you want to auto-mount.

Suppose you attached a USB-drive and it’s shown as “/dev/sdb1” in above command.

Its file-system will also be shown beside that. Match your type from System name and notice the corresponding Linux type.

 

System name

English name

Linux type

 

W95 FAT32

Microsoft FAT32

vfat

 

W95 FAT32 (LBA)

Microsoft FAT32

vfat

 

W95 FAT16 (LBA)

Microsoft FAT16

vfat

 

W95 Ext’d (LBA)

Microsoft extended partition

Not used

 

NTFS volume set

Microsoft NTFS

ntfs

 

NTFS volume set

Microsoft NTFS with read-write access

ntfs-3g

 

Apple_HFS

Apple HFS

hfsplus

Suppose it’s W95 FAT32 (LBA) so your Linux type is vfat. If you are unsure about this type, you may consider mounting the device using: sudo mount /dev/sdb1 and then run mount command to see mounted file-systems and it’s file-system type.

After gathering all the necessary information, open /etc/fstab with root permissions with your text editor e.g. sudo vim /etc/fstab

Then add the new entry,

/dev/sdb1       /mnt/usb_drive       vfat       errors=remount-ro       0      0

to fill in line with the following.

<file system>     <mount point>   <type>       <options>         <dump>  <pass>

Then save the file and exit. You may check on the next reboot if it is mounted.

errors=remount-ro option is for the case when some error occurs, the device will get remounted as read-only. The <dump> option specifies if the file-system needs to be dumped (back-up) for safe keeping. 0 here means it doesn’t need to be dumped. And the <pass> means that the order which filesystem checks are to be performed at system reboot. The root filesystem should be labeled as 1, while others as 20 if we don’t want it to be checked. See man fstab command for more.

See “less” output at a time


There are a lot commands/tools that produce a lot of output that we can’t see at once (and without scrolling). Most of the commands on terminal can be made to show us whole output page by page using “less” command.

less command_here

less ls -R ~/

or you may pipe the output to less command,

ls -R ~/ | less

 

or similarly, to see the contents of a file:

cat file_name | less

There are many commands/tools that have their own prompt.

 

For IPython, you may do:

%page variable_name

 

Similarly for MySQL,

pager less -SFX

Or you may also try:

SELECT * FROM sometable \G

 

Above commands will show you the output from start that you can navigate through using the arrow up/down keys or space (page by page). Press “q” to exit.

Vim: Add some text at the start/end of each line


Suppose, you have a file with 1000 lines. You want to add some text at the beginning/end of each line. You won’t think of doing it manually (actually, I can’t stop you). Here, I am telling you how to save you a lot of time.

For example, we have the following lines in the file:

Name

Email

Phone Number

Address

And you want your output to be:

“Name”,

“Email”,

“Phone Number”,

“Address”,

and so on.

In Vim, you can simply type:

:%s/$/",/

It will place “, at the end of each line.

Similarly, for beginning:

:%s/^/"/

It will place ” at the beginning of each line.

Hence, we used $ for appending to end and ^ for prepending.

You can also do this for a few lines by visually selecting them with and then pressing : and type the command

s/^/"/

Skip the “%” because we do it while doing things globally across the file. So it will look like:

:'<,'>s/^/"/

Another method, if you want to type some text interactively on a couple of lines:

Ctrl+V

Select a few rows (downside, pressing j). After selecting the position in couple in all rows, press:

Shift + i

They type what you want. Then press the Esc key:

Esc

Now, wait 1 second. You’ll see same text to be replicated over all rows you selected.

Please share in comments, if you even easier method to do the similar.

Extract text out of a image/PDF


For this purpose, you may read my previous related post here.

I am going to introduce (again) to the tesseract OCR engine. But this time I am using 16.04 and the command to install it is:

sudo apt install tesseract-ocr

If you have some PDF and want it to convert to image to further process it. You may use various methods. One of them may be:

convert input.pdf output.png

But this will produce a relatively low-resolution image that may result in bad text out of OCR.

So, instead we use:

convert -density 300 -quality 100 input.pdf output.png

Changing the density and tell it to not to decrease the quality than 100%.

Note if the input.pdf is a multi-page PDF, it will create different output images named like: output-0.png, output-1.png and so on.

So finally, use tesseract as:

tesseract output.png text_file -l eng

It will create a text_file.txt in the same directory. You may play with various options of convert or tesseract based on your needs.

adb not detecting the phone!


What is it?

adb is abbreviated for “Android Debug Bridge”. It comes with the Android Development SDK but it can be installed separately too. It allows users to send commands to the Android phone via PC. It will be helpful in cases if you are tinkering with your phone and reached some nearly bricked condition.

In my case: I have a Zuk Z1 and I have installed TWRP recovery and Cyanogen 13 installed as of now. There might be some case or something went wrong that I can’t boot into my phone and even recovery isn’t working. Then booting the phone into bootloader might help. But I didn’t find any way to boot into bootloader without going into recovery. And suppose recovery isn’t working too. Then adb can help you here. You may use adb to boot into bootloader and then flash recovery and the OS later on.

Installation

On debian based systems e.g. Ubuntu, you may install it like:

sudo apt-get install android-tools-adb android-tools-fastboot

If it didn’t work, then see here. It will get you adb and fastboot.

My Problem:

The problem was that after installing adb, I connected the phone and fired the command:

adb devices

but it wasn’t listing my device.

So after searching a bit, I got a solution.

First you need to know the vendor id of your device. Search the Internet for that.

Other thing you can try is connect your phone to Linux PC and from terminal type:

lsusb -v

and look for your device and then look for “idVendor” field. For example mine is: 0x2b4c

Another thing you may try is to use following command from terminal:

fastboot devices

after booting the phone into the bootloader.

So coming back to my problem, my device wasn’t getting listed on running adb devices.

The solution is that you need to create/edit a file ~/.android/adb_usb.ini and write it as:

# ANDROID 3RD PARTY USB VENDOR ID LIST — DO NOT EDIT.
# USE ‘android update adb’ TO GENERATE.
# 1 USB VENDOR ID PER LINE.
# ie 0x2207
0x2b4c

And place the vendor id at the end, like I did above. Save file and run adb devices again. It will list the device but it will be unauthorized. Now your phone should pop up a permission box to allow the connection. Allow it (tick always).

Then do the following to restart the adb server (with sudo).

adb kill-server

and afterwards

adb start-server

Try reconnecting the phone after disconnecting if it didn’t work.

Import string as Dictionary in Python


I had a Python dictionary (that looks like json) stored in a file, generated by some code. Now I wanted it in Python to act as the dictionary. But when we read that file, it gets stored as string (in a variable). But I wanted to access its keys and values. But if it’s a string we couldn’t do this directly.

So I searched for a method to convert string to dict. Here is the go:

In [1]: import ast

In [2]: string = “{‘name’: ‘xyz’, ‘age’: 3}”

In [3]: string
Out[3]: “{‘name’: ‘xyz’, ‘age’: 3}”

In [4]: ast.literal_eval(string)
Out[4]: {‘age’: 3, ‘name’: ‘xyz’}

In [5]: the_dict = ast.literal_eval(string)

In [6]: the_dict.keys()
Out[6]: [‘age’, ‘name’]

 

Import FreeCAD in Python


If you’ve compiled FreeCAD, but don’t know how to import it as a package in Python then read on.

First of all, you need to locate where the FreeCAD.so file is.

locate FreeCAD.so

Mine is in /home/mandeep/Desktop/build/lib.

To be able to do:

import FreeCAD

You’ll have to go to that directory and run python and if you now try to import, it will work. But most probably, you won’t agree to use it as it might feel impractical.

Another method is to append this path to sys.path. For this, you need to edit a file site.py.

For example,

sys.path.append(“/home/mandeep/Desktop/build/lib”)

The site.py would probably be where your python lib directory is. It may probably look like: /usr/lib/python2.7. But I am using the Python within the virtualenv so the python lib is within the virtualenv e.g. venv/lib/python2.7. Here venv is my virtualenv.

Read more about this at https://docs.python.org/2/library/site.html

The idea to edit the site.py file is that it gets initialized with Python. So you can now import your packages from anywhere.

Sourcehttp://www.freecadweb.org/wiki/index.php?title=Embedding_FreeCAD

Referencehttp://stackoverflow.com/a/15109881/3784226