Flashing Android on Nexus

To flash a system image:

  1. Download the appropriate system image for your device from:
    Factory Images for Nexus Devices
  2. cd to the directory where the file is stored then unarchive it:
    $ tar -xvzf <filename>
  3. Connect your device to your computer over USB (make sure USB Debugging is selected under Settings > Developer Options)TIP: on my system the following commands must be run as root. To do that properly su - to root then execute the following:
    # export PATH=$PATH:/home/<user>/android-sdk/tools/:\
    /home/<user>/android-sdk/platform-tools/
  4. Start the device in fastboot mode with one of the following methods:
    • Using the adb tool: With the device powered on, execute:
      # adb reboot bootloader
    • Using a key combo: turn the device off, then turn it on and immediately hold down the relevant key combination for your device
  5. to display whether you’re correctly connected to your device run:
    # fastboot devices list
    badcafe00    fastboot
    
  6. If necessary, unlock the device’s bootloader by running:
    # fastboot oem unlock

    NOTE: The target device will show you a confirmation screen. This command erases all data!!!

  7. cd to the unzipped system image directory
  8. Execute the flash-all script. This script installs the necessary bootloader, baseband firmware(s), and operating system:
    # ./flash-all.sh 
    sending 'bootloader' (3911 KB)...
    OKAY [  0.170s]
    writing 'bootloader'...
    OKAY [  1.388s]
    finished. total time: 1.559s
    rebooting into bootloader...
    OKAY [  0.005s]
    finished. total time: 0.056s
    archive does not contain 'boot.sig'
    archive does not contain 'recovery.sig'
    archive does not contain 'system.sig'
    archive does not contain 'vendor.img'
    Creating filesystem with parameters:
        Size: 28856791040
        Block size: 4096
        Blocks per group: 32768
        Inodes per group: 8192
        Inode size: 256
        Journal blocks: 32768
        Label: 
        Blocks: 7045115
        Block groups: 215
        Reserved block group size: 1024
    Created filesystem with 11/1761280 inodes and 154578/7045115 blocks
    Creating filesystem with parameters:
        Size: 587202560
        Block size: 4096
        Blocks per group: 32768
        Inodes per group: 7168
        Inode size: 256
        Journal blocks: 2240
        Label: 
        Blocks: 143360
        Block groups: 5
        Reserved block group size: 39
    Created filesystem with 11/35840 inodes and 4616/143360 blocks
    --------------------------------------------
    Bootloader Version...: FLO-04.04
    Baseband Version.....: none
    Serial Number........: 06273f26
    --------------------------------------------
    checking product...
    OKAY [  0.003s]
    checking version-bootloader...
    OKAY [  0.004s]
    sending 'boot' (7182 KB)...
    OKAY [  0.294s]
    writing 'boot'...
    OKAY [  0.386s]
    sending 'recovery' (7768 KB)...
    OKAY [  0.317s]
    writing 'recovery'...
    OKAY [  0.296s]
    erasing 'system'...
    OKAY [  1.170s]
    sending 'system' (799293 KB)...
    OKAY [ 32.173s]
    writing 'system'...
    OKAY [ 35.771s]
    erasing 'userdata'...
    OKAY [ 24.776s]
    sending 'userdata' (139085 KB)...
    OKAY [  5.464s]
    writing 'userdata'...
    OKAY [  6.017s]
    erasing 'cache'...
    OKAY [  0.464s]
    sending 'cache' (10984 KB)...
    OKAY [  0.437s]
    writing 'cache'...
    OKAY [  0.488s]
    rebooting...
    
    finished. total time: 108.130s
  9. Once the script finishes, your device reboots. You should now lock the bootloader for security. Start the device in fastboot mode again, as described above.
  10. Execute one more time:
    # fastboot oem lock

    NOTE: Locking the bootloader does not wipe any data. In case you’ll want to flash it again it’ll force you to run fastboot oem unlock which will wipe the data instead.

Mac OS X: edit .plist files from CLI

A number of methods are available that involve “tools” either requiring installatins or some form of previous knowledge about the .plist file contents.
As everything we need is already embedded within the OS (tested with 10.8.5), I’ll just go ahead and describe my manual method:

  1. convert the .plist file (or a copy!) from binary1 (default/original format) to the format of your preference (xml1 or json)
  2. plutil -convert xml1 <filename>
  3. edit the file
  4. vi <filename> :)
  5. convert the file back to binary1 format
  6. plutil -convert binary1 <filename>

Show progress during dd copy

Ok, this is actually a copy and paste post thus credit is due where credit is deserved: Linux Commando.

The quick-and-dirty trick: open a new terminal and enter:

$ kill -INFO <PID of dd process>

NOTE: do not use USR1 on Mac OS X!

The “output” will be printed on the console where dd is actually running:

$ dd if=/path/to/file of=/dev/sdb1 bs=1M
0+14 records in
0+14 records out
204 bytes (204 B) copied, 24.92 seconds, 0.0 kB/s

[131028 update]: in fact $ sudo pkill -INFO -n -x dd seems to be a much quicker, and easy to remember, one-liner.

Source: RPi Easy SD Card Setup

List all files in a directory, order by date

Nothing original, the Internet is full of good recommendations, but I had to come up with my own answer:

  1. cd to the upper-level directory
  2. run:  find . -type f -print0 | xargs -0 ls -rtl | awk '{print $6 " " $7 " " $8 " " $9}'

Explanation:

  • find recursively looks for regular files starting from the current directory, output is a long uninterrupted list
  • xargs is used to pass the above output to ls which will display in reverse order, by time, in long format
  • awk will select only the relevant fields

Compare hex files in Mac OS X (strings, hexdump)

diff or sdiff built-in utilities make it easy to compare text files:
example file txt1.txt

line #1
line #2
line #3

example file txt2.txt

line #1
line #2
line #4 <<<===this is where difference lies from the above file

Both diff and sdiff will give us a precise indication of which differences exist and where they are:

$ diff txt1.txt txt2.txt 
3c3
< line #3 <===this is the contents of the first argument ("<", left file)
---
> line #4 <===this is the contents of the second argument (">", right file)

$ sdiff txt1.txt txt2.txt
line #1                         line #1
line #2                         line #2
line                       |    line #4 <===files are shown side-by-side
                                            and differences highlighted

Now what would occur with two different binary files is:

$ diff bin1.bin bin2.bin 
Binary files bin1.bin and bin2.bin differ

Evidently that’s not enough for us…
Well, there’s built-in tools that would be useful in highlighting some discrepancies, namely strings and hexdump. Let’s take a look at both of them with some examples.

1. strings command is able to find any “printable” objects in a file:

$ strings bin1.bin 
_DYNAMIC
_GLOBAL_OFFSET_TABLE_
__gmon_start__
_init
...

You might notice this can be a long output, hmmm… What about the following?

$ strings bin1.bin | grep -i version
version 5.0.0.5, build 0

Whereas:

$ strings bin2.bin | grep -i version
version 4.1.0.7, build 0

Of course “version” is just an example but quite relevant IMHO. Up to you to identify a string that would be meaningful for a comparison.

2. even more detailed, hexdump command, as its name implies, can show you the entire contents of the file along with the hex-to-ASCII translation, for instance:

$ hexdump -C bin1.bin 
00000000  7f 45 4c 46 01 01 01 00  00 00 00 00 00 00 00 00  |.ELF............|
00000010  03 00 03 00 01 00 00 00  e0 1d 00 00 34 00 00 00  |............4...|
00000020  b8 88 00 00 00 00 00 00  34 00 20 00 04 00 28 00  |........4. ...(.|
00000030  21 00 1e 00 01 00 00 00  00 00 00 00 00 00 00 00  |!...............|
...

In this case the suggestion is to redirect the output to a text file that can be examined with diff or sdiff, or your favourite tools, later on.

Start an FTP (or TFTP or SFTP) server on Mac OS X (10.7+)

FTP

Since upgrading to Lion (10.7) the option to enable the FTP server has disappeared for, I assume, security reasons.

Nonetheless, an FTP server is still available to the user. Let’s see how it can be started from a CLI one-liner:

$ sudo -s launchctl load -w /System/Library/LaunchDaemons/ftp.plist

You can immediately confirm that by trying:

$ ftp localhost

NOTE: files will be served from the user’s home directory.

To stop the FTP server just issue:

$ sudo -s launchctl unload -w /System/Library/LaunchDaemons/ftp.plist

TFTP

To start/stop a TFTP server replace ftp.plist with tftp.plist.
The source directory in this case will be /private/tftpboot.
NOTE: the files must be readable.

SFTP

As fas as SFTP (S=secure) goes instead, this can be enabled through System Preferences’ Sharing application. It’s not explicitly mentioned but if Remote Login (SSH) is on then the computer’s IP address will also respond to SFTP/SCP requests.

For the tech-savvy, yes, this is better referenced to as FTP over SSH.

Source: http://osxdaily.com/2011/09/29/start-an-ftp-or-sftp-server-in-mac-os-x-lion/

Instant Python HTTP server

The quick and dirty way of starting an instant web server is by means of Python‘s simple HTTP request handler.

The following command will start listening to HTTP requests on port <port> (if <port> is blank a default value of 8000 is assumed) and will be serving files from the current directory:

$ python -m SimpleHTTPServer <port>

On the host’s console the access log will be displayed (example):

Serving HTTP on 0.0.0.0 port 4444 ...
<source address> - - [16/May/2013 12:12:17] "GET / HTTP/1.1" 200 -
<source address> - - [16/May/2013 12:12:17] "GET /favicon.ico HTTP/1.1" 404 -
<source address> - - [16/May/2013 12:12:19] "GET /example_file.txt HTTP/1.1" 200 -
...

To stop the server, according to the above-mentioned quick and dirty approach, issuing CTRL+C is fine (a traceback will be printed by the Python interpreter, that’s not something to worry about).

Source: http://osxdaily.com/2010/05/07/create-an-instant-web-server-via-terminal-command-line/

tree-like ls

tree” is a very nice command that’s not always available on any systems. As the name implies it lists the contents of directories in a tree-like format.

An example is:

$ tree
.
|-- Boards
|-- Members
|-- Messages
|-- Settings.pl
|-- Sources
|-- Variables
|-- YaBB.cgi
|-- english.lng
|-- template.html
`-- template2.html

Well, a similar result can be achieved in Bash with a nifty little one-liner:

ls -R | grep ":$" | sed -e 's/:$//' \
   -e 's/[^-][^\/]*\//--/g' -e 's/^/   /' -e 's/-/|/'

NOTE: long line, I had to break/fold it, sorry about that. Copy&paste is fine as long as the backslash (“\”) at the end of the first line is preserved.

A short explanation of the cryptic syntax above is as follows:

  • initially a recursive listing of the current directory is done: ls -R
  • pipe
  • any output other that the directory names, identified by “:” at the very end of each line (hence “:$“), is filtered out: grep ":$"
  • pipe
  • finally there’s a little of “sedmagic finding and replacing any hierarchy level (“/“) with one or more dashes (“-“):
    sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/'

*NIX chat

Well, no, not chat in the common/modern sense but did you know you can display which users are connected to the same system you’re logged on and send them messages?

First off, displaying users connected to the same system is, for sysadmins, a basic and quite an important monitoring command.

The syntax is extremely simple: “w“!

Example:

$ w
10:02:12 up 2 min,  2 users,  load average: 0.33, 0.18, 0.07
USER      TTY      FROM             LOGIN@   IDLE   JCPU   PCPU  WHAT
<userid1> pts/0    <src IP addr>    10:02    0.00s  4.83s  0.08s bash
<userid2> pts/1    <src IP addr>    10:02    0.00s  4.83s  0.08s w

Now, sending a message to <userid1> from <userid2> is as easy as:

echo "I'm about to disconnect you ;)" | write <userid1> pts/0

On <userid1>‘s console the following will be printed:

Message from <userid2>@<server> on pts/1 at 10:04 ...
I'm about to disconnect you ;)
EOF

Please notice that the TTY being used, pts/0 in the example, may differ from one OS to another.
For instance, on Mac OS X, it should be ttys00x.

The same is possible on Cisco routers by means of “send” command.