Linux Tips and Tricks

This post contains some tips and tricks that helps resolve problems that i've encountered when working with Linux, mostly Ubuntu.

Ubuntu: Install 32 bits libraries on 64 bits system

Some of my applications are 32 bits only which sometime depend on several 32 bits libraries. By default, ubuntu installed only the 64 bits version of these libraries. To installed the 32 bit ones, we need enable the i386 architecture using dpkg, these following commands should be executed as root:

    dpkg --add-architecture i386
    apt-get update

Then to install a 32 bits version of a library:

    apt-get install lib_name:i386
    # for example
    apt-get install libcairo2:i386

How to find all files containing specific text on Linux?

This is a copy of an answer found from StackOverflow.
Do the following:

    grep -rnw '/path/to/somewhere/' -e "pattern"
  • -r or -R is recursive,
  • -n is line number, and
  • -w stands match the whole word.
  • -l (lower-case L) can be added to just give the file name of matching files.
  • Along with these, --exclude, --include, --exclude-dir or --include-dir parameters could be used for efficient searching:

  • This will only search through the files which have .c or .h extensions:

      grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern"
  • This will exclude searching all the files ending with .o extension:
      grep --exclude=*.o -rnw '/path/to/somewhere/' -e "pattern"
  • Just like exclude files, it's possible to exclude/include directories through --exclude-dir and --include-dir parameter. For example, this will exclude the dirs dir1/, dir2/ and all of them matching *.dst/:
      grep --exclude-dir={dir1,dir2,*.dst} -rnw '/path/to/somewhere/' -e "pattern"

Fix GIT error : GH001: Large files detected. You may want to try Git Large File Storage

Move the file out of git and simply remove the file from the git cache with the following command:

git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch path/to/large/file.ext'

Limit outgoing network access on certain users using iptable

#! /bin/sh
iptables --append OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# create a new chain
iptables --new-chain chk_demo_user

# use new chain to process packets generated by apache
iptables -A OUTPUT -m owner --uid-owner demo -j chk_demo_user

# allow user use the port 5901
iptables -A chk_demo_user -p tcp --syn -d --dport 5901 -j RETURN

# reject everything else and stop hackers downloading code into our server
iptables -A chk_demo_user -j REJECT

Shrink Raspberry Pi image

Extracting the partition information from the image using fdisk:

$ fdisk -lu image.img
Disk image.img: 4096 MB, 4096000000 bytes, 8000000 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000a1bc7

      Device Boot      Start         End      Blocks   Id  System
image.img1              2048     5872026     5869978    b  W95 FAT32

We see, that the partition has a size of about 2,8Gb (5872026 * 512), the rest is unpartitioned.

So, everything after the end of the partition can be removed.
This will be done with the tool truncate. Don't forget to add 1 to the number of sectors, as block-numbers start at 0.

$ truncate --size=$[(5872026+1)*512] image.img

Related posts


The comment editor supports Markdown syntax. Your email is necessary to notify you of further updates on the discussion. It will be hidden from the public.
Powered by antd server, (c) 2017 - 2024 Dany LE.This site does not use cookie, but some third-party contents (e.g. Youtube, Twitter) may do.