Wednesday, 28 November 2012

Understanding basic linux system - Part 1

This post is about some of the basic unix/linux commands, there is also a sequel to it.

chapter 1 => unix runlevel [0 to 6]
A run level is a state of init and the whole system that defines what system services are operating.
prayag@prayag_f1:~$ who -r
         run-level 2  2012-12-09 17:04


run-level 2 = Local Multiuser with Networking but without network service (like NFS)

similarly my amazon elastic cloud VM was on runlevel 3, which means Multi-user mode with networking.

So, once the kernel is loaded, it finds init in sbin and executes it.
ll /sbin/init
lrwxrwxrwx 1 root root 20 Sep 28 18:40 /sbin/init -> /lib/systemd/systemd*
Then init continues to read the /etc/inittab file (/etc/init.d in ubuntu), which describes how the system should be set up in each run-level and sets the default run-level

The /etc/inittab will use the file /etc/rc<RUN_LEVEL>.d/

ll /etc/rc2.d/
total 20
drwxr-xr-x   2 root root  4096 Nov 12 12:58 ./
drwxr-xr-x 132 root root 12288 Nov 12 12:58 ../
-rw-r--r--   1 root root   677 Feb  5  2016 README
lrwxrwxrwx   1 root root    16 Nov 11 14:12 S01apport -> ../init.d/apport*
lrwxrwxrwx   1 root root    17 Nov 11 14:12 S01rsyslog -> ../init.d/rsyslog*
lrwxrwxrwx   1 root root    15 Nov 11 14:12 S01uuidd -> ../init.d/uuidd*
lrwxrwxrwx   1 root root    15 Nov 11 14:12 S02acpid -> ../init.d/acpid*
lrwxrwxrwx   1 root root    17 Nov 11 14:12 S02anacron -> ../init.d/anacron*
lrwxrwxrwx   1 root root    24 Nov 12 12:58 S02cgroupfs-mount -> ../init.d/cgroupfs-mount*
lrwxrwxrwx   1 root root    14 Nov 11 14:12 S02cron -> ../init.d/cron*
lrwxrwxrwx   1 root root    14 Nov 11 14:12 S02dbus -> ../init.d/dbus*
lrwxrwxrwx   1 root root    20 Nov 11 14:12 S02irqbalance -> ../init.d/irqbalance*
lrwxrwxrwx   1 root root    20 Nov 11 14:12 S02kerneloops -> ../init.d/kerneloops*
lrwxrwxrwx   1 root root    15 Nov 11 14:12 S02rsync -> ../init.d/rsync*
lrwxrwxrwx   1 root root    27 Nov 11 14:12 S02speech-dispatcher -> ../init.d/speech-dispatcher*
lrwxrwxrwx   1 root root    18 Nov 11 14:12 S02thermald -> ../init.d/thermald*
lrwxrwxrwx   1 root root    18 Nov 11 14:12 S02whoopsie -> ../init.d/whoopsie*
lrwxrwxrwx   1 root root    22 Nov 11 14:12 S03avahi-daemon -> ../init.d/avahi-daemon*
lrwxrwxrwx   1 root root    19 Nov 11 14:12 S03bluetooth -> ../init.d/bluetooth*
lrwxrwxrwx   1 root root    16 Nov 12 12:58 S03docker -> ../init.d/docker*
lrwxrwxrwx   1 root root    17 Nov 11 14:12 S03lightdm -> ../init.d/lightdm*
lrwxrwxrwx   1 root root    14 Nov 11 14:12 S04cups -> ../init.d/cups*
lrwxrwxrwx   1 root root    22 Nov 11 14:12 S04cups-browsed -> ../init.d/cups-browsed*
lrwxrwxrwx   1 root root    15 Nov 11 14:12 S04saned -> ../init.d/saned*
lrwxrwxrwx   1 root root    21 Nov 11 14:12 S05grub-common -> ../init.d/grub-common*
lrwxrwxrwx   1 root root    18 Nov 11 14:12 S05ondemand -> ../init.d/ondemand*
lrwxrwxrwx   1 root root    18 Nov 11 14:12 S05plymouth -> ../init.d/plymouth*
lrwxrwxrwx   1 root root    18 Nov 11 14:12 S05rc.local -> ../init.d/rc.local*


chapter 2 => linux system state/ linux services
A Linux service is an app that runs in the background waiting to be used, or carrying out essential tasks. eg. Apache and MySQL.
I will generally be unaware of services until I need them.

System state
-----------------

systemCTL is the central management tool for controlling the init system.
With systemCTL, I can start and stop, enable/disable systemd services.

* The systemctl list-units command only displays units that systemd has attempted to parse and load into memory.
* systemd will only read units that it thinks it needs, this will not necessarily include all of the available units on the system.
* To see every available unit-file within the systemd paths, including those that systemd has not attempted to load, you can use the list-unit-files command instead

systemctl list-unit-files
UNIT FILE                                  STATE
proc-sys-fs-binfmt_misc.automount          static
org.freedesktop.hostname1.busname          static
org.freedesktop.locale1.busname            static
org.freedesktop.login1.busname             static
org.freedesktop.network1.busname           static
org.freedesktop.resolve1.busname           static
org.freedesktop.systemd1.busname           static
org.freedesktop.timedate1.busname          static
dev-hugepages.mount                        static
dev-mqueue.mount                           static
proc-sys-fs-binfmt_misc.mount              static
sys-fs-fuse-connections.mount              static
sys-kernel-config.mount                    static
sys-kernel-debug.mount                     static
acpid.path                                 enabled
cups.path                                  enabled
systemd-ask-password-console.path          static
systemd-ask-password-plymouth.path         static
systemd-ask-password-wall.path             static
systemd-networkd-resolvconf-update.path    static
accounts-daemon.service                    enabled
acpid.service                              disabled
docker.service                             enabled
dbus.socket                                static  
docker.socket                              enabled

display the unit file that systemd has loaded into its system, systemctl cat docker.service and do list the dependencies of the service systemctl list-dependencies docker.service


chapter 3 => linux processes
Linux process is a "dynamic entity", constantly changing as the machine code instructions are executed by the processor. As well as the program's instructions and data, the process also includes the program counter and all of the CPU's registers as well as the process stacks containing temporary data such as routine parameters, return addresses and saved variables. The current executing program, or process, includes all of the current activity in the microprocessor.

Modern virtual memory operating systems generally have a flat memory model and not a segmented one.

List all linux processes,
$ ps -A
  PID TTY          TIME CMD
    1 ?        00:00:01 systemd
 4142 ?        00:00:02 dockerd
 4151 ?        00:00:00 docker-containerd


List all running linux processes,
ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.1 119836  6044 ?        Ss   12:54   0:01 /sbin/init splash
root      4142  0.0  0.9 419120 37848 ?        Ssl  12:58   0:02 /usr/bin/dockerd -H fd://
root      4151  0.0  0.2 199756 10568 ?        Ssl  12:58   0:00 docker-containerd -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --shim docker-containerd-shim --metrics-interval=0 --start-timeout 2m --state-dir /var/run/docker/libcontainerd/containerd --runtime docker-runc

To kill a process,
$ pkill vim

The memory is virtualized among the processes on the system. An individual process's view of memory is as if it alone has full access to the system's physical memory.

Read more about linux process, and process virtual address space

chapter 4 => 32 or 64 bit OS
In 32-bit, a processor with 32-bit memory addresses can directly access at most (2^32) bits = 4 GiB of byte-addressable memory.
Which also means, each register stores 32 bits = 4 bytes. 32 bits (1 word) of information are processed per clock cycle.

In 64-bit computing, processors that have datapath widths, integer size, and memory-address widths of 64 bits = 8 bytes.
Which also means, each register stores 64 bits = 8 bytes.  64 bits (1 word) of information are processed per clock cycle.


prayag@prayag_f1:~$ file /sbin/init
/sbin/init: ELF 32-bit LSB shared object, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=0x5f722891935c0f35fd8f318d4606b334a9b1d423, stripped

or

[ec2-user@ip-172-21-19-140 ~]$ file /lib/systemd/systemd
/lib/systemd/systemd: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=51a025073a1ab8c1a4dfa808d5af7240179400a1, stripped

ELF - https://en.wikipedia.org/wiki/Executable_and_Linkable_Format
LSB is Linux Standard Base including fs - https://en.wikipedia.org/wiki/Linux_Standard_Base

Watch this for more info, 32 Bit vs 64 Bit - How much data the OS can handle

OS

[ec2-user@ip-172-18-21-240 ~]$ cat /proc/version
Linux version 3.10.0-514.el7.x86_64 (mockbuild@x86-039.build.eng.bos.redhat.com) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC) ) #1 SMP Wed Oct 19 11:24:13 EDT 2016


chapter 5 => linux access control/user groups
user groups are defined in /etc/group

$ cut -d: -f1 /etc/group
root
daemon
bin
sys
adm
lp
uucp
man
proxy
sudo
dip
sasl
plugdev
staff
games
users
nogroup
input
crontab
syslog
netdev
messagebus
ssh
pulse
pulse-access
rtkit
saned
prayagupd
sambashare
vboxsf
docker


To know which group the current user is assigned to,

groups
prayagupd adm cdrom sudo dip plugdev lpadmin sambashare vboxsf
or 
groups $USER
prayagupd : prayagupd adm cdrom sudo dip plugdev lpadmin sambashare vboxsf

To know the groups, current user assigned to with the group id/gid, which you can map in /etc/group

id $USER
uid=1000(prayagupd) gid=1000(prayagupd) groups=1000(prayagupd),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),113(lpadmin),128(sambashare),999(vboxsf)

Similarly, all users are defined in /etc/passwd.
cut -d: -f1 /etc/passwd

Read this for user management.

chapter 6 => package repos and PPA (package archives)

On rh,
[ec2-user@ip-172-0-0-128 ~]$ yum repolist
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
repo id                                                               repo name                                                                  status
!amzn2-core/2/x86_64                                                  Amazon Linux 2 core repository                                             16,294
amzn2extra-docker/2/x86_64                                            Amazon Extras repo for docker                                                  14
docker-ce-stable/x86_64                                               Docker CE Stable - x86_64                                                      36
repolist: 16,344

On ubuntu,

prayag@prayag_f1:~$ grep ^ /etc/apt/sources.list /etc/apt/sources.list.d/*

/etc/apt/sources.list:# deb cdrom:[Ubuntu 12.04 LTS _Precise Pangolin_ - Release i386 (20120423)]/ precise main restricted
/etc/apt/sources.list:
/etc/apt/sources.list:# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
/etc/apt/sources.list:# newer versions of the distribution.
/etc/apt/sources.list:deb http://np.archive.ubuntu.com/ubuntu/ precise main restricted
/etc/apt/sources.list:deb-src http://np.archive.ubuntu.com/ubuntu/ precise main restricted
[...]

/etc/apt/sources.list.d/dropbox.list:deb http://linux.dropbox.com/ubuntu precise main
/etc/apt/sources.list.d/dropbox.list.save:deb http://linux.dropbox.com/ubuntu precise main
/etc/apt/sources.list.d/google-chrome.list:### THIS FILE IS AUTOMATICALLY CONFIGURED ###
/etc/apt/sources.list.d/google-chrome.list:# You may comment out this entry, but any other modifications may be lost.
/etc/apt/sources.list.d/google-chrome.list:deb http://dl.google.com/linux/chrome/deb/ stable main
/etc/apt/sources.list.d/google-chrome.list.save:### THIS FILE IS AUTOMATICALLY CONFIGURED ###
/etc/apt/sources.list.d/google-chrome.list.save:# You may comment out this entry, but any other modifications may be lost.
/etc/apt/sources.list.d/google-chrome.list.save:deb http://dl.google.com/linux/chrome/deb/ stable main
/etc/apt/sources.list.d/groovy-dev-grails-precise.list:deb http://ppa.launchpad.net/groovy-dev/grails/ubuntu precise main
/etc/apt/sources.list.d/groovy-dev-grails-precise.list:deb-src http://ppa.launchpad.net/groovy-dev/grails/ubuntu precise main
/etc/apt/sources.list.d/groovy-dev-grails-precise.list.save:deb http://ppa.launchpad.net/groovy-dev/grails/ubuntu precise main
/etc/apt/sources.list.d/groovy-dev-grails-precise.list.save:deb-src http://ppa.launchpad.net/groovy-dev/grails/ubuntu precise main
/etc/apt/sources.list.d/precise-partner.list:deb http://archive.canonical.com/ubuntu precise partner #Added by software-center
/etc/apt/sources.list.d/precise-partner.list.save:deb http://archive.canonical.com/ubuntu precise partner #Added by software-center
/etc/apt/sources.list.d/ubuntu-mozilla-daily-ppa-precise.list:deb http://ppa.launchpad.net/ubuntu-mozilla-daily/ppa/ubuntu precise main
/etc/apt/sources.list.d/ubuntu-mozilla-daily-ppa-precise.list:deb-src http://ppa.launchpad.net/ubuntu-mozilla-daily/ppa/ubuntu precise main
/etc/apt/sources.list.d/ubuntu-on-rails-ppa-precise.list:deb http://ppa.launchpad.net/ubuntu-on-rails/ppa/ubuntu precise main

chapter 7 => task scheduling

When I check the /etc/cron.daily in RHEL 7 I get a following list of bash files.


[ec2-user@ip-172-21-19-140 ~]$ ll /etc/cron.daily/
total 12
-rwx------. 1 root root 219 Jul 14  2016 logrotate
-rwxr-xr-x. 1 root root 618 Mar 17  2014 man-db.cron
-rwx------. 1 root root 256 Sep 13  2016 rhsmd

And on ubuntu distro, 


prayag@prayag_f1:~$ ls -l /etc/cron.daily/
total 64
-rwxr-xr-x 1 root root   311 May 25  2012 0anacron
-rwxr-xr-x 1 root root   219 Oct  1 15:44 apport
-rwxr-xr-x 1 root root 15466 Oct 16 16:44 apt
-rwxr-xr-x 1 root root   355 Jun 19 03:42 bsdmainutils
-rwxr-xr-x 1 root root   384 May 25  2012 cracklib-runtime
-rwxr-xr-x 1 root root   256 Oct  2 01:15 dpkg
-rwxr-xr-x 1 root root   372 Oct  3 04:17 logrotate
-rwxr-xr-x 1 root root  1365 Sep 19 04:29 man-db
-rwxr-xr-x 1 root root   606 Aug 15 09:11 mlocate
-rwxr-xr-x 1 root root   249 Sep  7 00:59 passwd
-rwxr-xr-x 1 root root  2417 Jul  2  2011 popularity-contest
-rwxr-xr-x 1 root root  2947 Jun 15 00:41 standard
-rwxr-xr-x 1 root root   214 Oct 10 04:26 update-notifier-common

One of them, /etc/cron.daily/apt does variety of things based on apt configuration settings, which are maintained in /etc/apt/apt.conf and the various include files under /etc/apt/apt.conf.d/

The /etc/apt is something like this :

#!/bin/sh
[...]
check_stamp()
{
    stamp="$1"
    interval="$2"

    if [ $interval -eq 0 ]; then
    debug_echo "check_stamp: interval=0"
    # treat as no time has passed
        return 1
    fi

    if [ ! -f $stamp ]; then
    debug_echo "check_stamp: missing time stamp file: $stamp."
    # treat as enough time has passed
        return 0
    fi

    # compare midnight today to midnight the day the stamp was updated
    stamp_file="$stamp"
    stamp=$(date --date=$(date -r $stamp_file --iso-8601) +%s 2>/dev/null)
    if [ "$?" != "0" ]; then
        # Due to some timezones returning 'invalid date' for midnight on
        # certain dates (eg America/Sao_Paulo), if date returns with error
        # remove the stamp file and return 0. See coreutils bug:
        # http://lists.gnu.org/archive/html/bug-coreutils/2007-09/msg00176.html
        rm -f "$stamp_file"
        return 0
    fi

    now=$(date --date=$(date --iso-8601) +%s 2>/dev/null)
    if [ "$?" != "0" ]; then
        # As above, due to some timezones returning 'invalid date' for midnight
        # on certain dates (eg America/Sao_Paulo), if date returns with error
        # return 0.
        return 0
    fi

    delta=$(($now-$stamp))

    # intervall is in days, convert to sec.
    interval=$(($interval*60*60*24))
    debug_echo "check_stamp: interval=$interval, now=$now, stamp=$stamp, delta=$delta (sec)"

    # remove timestamps a day (or more) in the future and force re-check
    if [ $stamp -gt $(($now+86400)) ]; then
         echo "WARNING: file $stamp_file has a timestamp in the future: $stamp"
         rm -f "$stamp_file"
         return 0
    fi

    if [ $delta -ge $interval ]; then
        return 0
    fi

    return 1
}

update_stamp()
{
    stamp="$1"
    touch $stamp
}
[...]


chapter 8 => network proxy
specify system-wide environment variables http_proxy and https_proxy  as below,

prayag@prayag_f1:~$ cat /etc/environment

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
http_proxy="http://10.xx.xxx.xxx:8080/"
https_proxy="https://10.xx.xxx.xxx:8080/"
ftp_proxy="ftp://10.xx.xxx.xxx:8080/"
socks_proxy="socks://10.xx.xxx.xxx:8080/"

Read about Networking and proxy server.

SOCKet Secure (SOCKS) is an Internet protocol that routes network packets between a client and server through a proxy-server.

prayag@prayag_f1:~$ env | grep -i proxy
NO_PROXY=localhost,127.0.0.0/8
http_proxy=http://10.xx.xxx.xxx:8080/
ftp_proxy=ftp://10.xx.xxx.xxx:8080/
ALL_PROXY=socks://10.xx.xxx.xxx:8080/
all_proxy=socks://10.xx.xxx.xxx:8080/
socks_proxy=socks://10.xx.xxx.xxx:8080/
UBUNTU_MENUPROXY=libappmenu.so
https_proxy=https://10.xx.xxx.xxx:8080/
no_proxy=localhost,127.0.0.0/8

I also can add env variables to /etc/profile and source it to see immediate effect.

chapter 9 => sudo version


prayag@prayag_f1:~$ sudo sudo -V
[...]
Sudoers I/O plugin version 1.8.5p2

chapter 10 => Install packages (ubuntu)
prayag@prayag:~/Downloads$ sudo dpkg -i TrackerSetup.deb

Or with gDEBi,

prayag@prayag:~/Downloads$ sudo apt-get install gdebi
prayag@prayag:~/Downloads$ sudo gdebi TrackerSetup.deb 

check about the installed package
prayag@prayag:~/Downloads$ dpkg -s gedit

http://askubuntu.com/a/17829/37643

chapter 11 => play popular non-free media formats (ubuntu)
sudo apt-get install ubuntu-restricted-extras
sudo apt-get install libavcodec-extra-53 libavcodec53 ffmpeg



References

How to install REALVNC gui in ubuntu 12.04?

How can I specify a display?

Installing & Running VNC on Redhat/RPM Linux

Starting firefox on a remote host (over ssh) opens a new window locally: what is happening?

visit url with command line

http://waxborg.servepics.com/opensuse/remote-administration/running-firefox-remotely-over-ssh

How to shut down the computer after a task has been completed?, http://askubuntu.com/a/15536

How can I install software or packages without Internet (offline)?

35 Tricky and Complex Unix Interview Questions and Commands (Part 1)

Linux Hacks gist, https://gist.github.com/prayagupd/4730009

Friday, 23 November 2012

git cheat sheet I

[1] git color configuration
git config --global color.ui auto


OR add the following [color] content to ~/.gitconfig
vi ~/.gitconfig  


[color]
  diff = auto
  status = auto
  branch = auto
  interactive = auto
  ui = true
  pager = true
[diff]
     tool = vimdiff


[2] git incoming
#using git fetch
$ git fetch && git log ..origin/master --stat

##using git diff
$ git diff master origin/master

[3] display paths that have diffs between the index file and the current HEAD commit

git status

In emacs, same thing is achieved with M-! git status. The following screenshot is for emacs.




[4] git diff (with vimdiff)
create a file named git_diff_wrapper, put it somewhere in your $PATH
$ vi /usr/local/bin/git_diff_wrapper
#!/bin/sh
vimdiff "$2" "$5"

add following code to ~/.gitconfig

[diff]
  external = git_diff_wrapper
[pager]
  diff =

add following code to bash conf file
$ vi ~/.bashrc
function git_diff() {
  git diff --no-ext-diff -w "$@" | vim -R -
}

Now, its baked, fire following command
$ git diff 
git diff HEAD^ HEAD src/js  (with previous commit)
git diff HEAD~2 HEAD src/js (with 2 previous commits from HEAD)
$ git diff c3b8159 HEAD src/js (with sha-id)

If vim-fugitive is used, fire following command in opened file
:Gdiff -- %  # shows commits
:copen

To exit :qa

[5] git outgoing

git log origin/master.. --stat

OR

$ git fetch && git log origin/master.. --stat

OR (git whatchanged)


$ git whatchanged origin/master.. --stat


[6] git reset/rollback a commit

$ git reset --soft 'HEAD^' 
How to undo the last Git commit?
# --soft would keep the changed files and keep them in the index as they were just before the changed commit

$ git rebase -i            
#git equivalent for hg rollback



[7] git merge from remote repo to local HEAD
Feature-NepalTweets is the branch in the following example; 
$ git stash 
#changes stashed can later be viewed with git stash show and recovered via git stash pop.
$ git pull origin Feature-NepalTweets
git stash pop
or 
git stash apply && git stash drop
$ git diff --name-only --diff-filter=U 
OR
$ git status --short | grep "^UU "
#Fix conflicts
$ git status --short | grep "^UU "
#add conflicted files using git add
#comit files
git commit -i grails-app/controllers/whotweetsnepal/NepTweetController.groovy -m "FIX:json" --author prayag.upd@gmail.com;
[NepalTweets 0d5bf9d] FIX:json

#Commit only modified files
git commit -a -m "FIX : ADD Staff on bootstrap assigning it a User, MAKE createdBy of Person nullable, ADD school to a user in controller, ADD getting schoolAccount via  logged in user in makeRegistration";

git push origin Feature-NepalTweets 
To https://github.com/iPrayag/WhoTweetsNepal.git
   ebe387f..0d5bf9d  NepalTweets -> NepalTweets


NOTE : The other method could be removing the locally changed files, if the changes are not important, and then firing git pull.

undo a git merge
Say you are in branch analytics/tracking-requests, merged changes from master.
But for some reasons you want to undo a merge before commit and push,
git reset --hard origin/tracking-requests
or 
git reset --merge ORIG_HEAD

[8] add git submodule
I am pointing repo facebook-android-sdk-3.0.1 at https://prayagupd@bitbucket.org/prayagupd/facebook-android-sdk-3.0.1.git to my app BirthdayForestGap.

prayag@prayag: BirthdayForestGap$ git submodule add https://prayagupd@bitbucket.org/prayagupd/facebook-android-sdk-3.0.1.git facebook-android-sdk-3.0.1.git

Cloning into 'facebook-android-sdk-3.0.1.git'...

Password for 'https://prayagupd@bitbucket.org':
remote: Counting objects: 165, done.
remote: Compressing objects: 100% (116/116), done.
remote: Total 165 (delta 44), reused 162 (delta 44)
Receiving objects: 100% (165/165), 565.66 KiB | 8 KiB/s, done.
Resolving deltas: 100% (44/44), done.



prayag@prayag: BirthdayForestGap$ ls -la
total 68
drwxrwxr-x 10 prayag prayag 4096 Mar 29 00:40 .
drwxrwxr-x  7 prayag prayag 4096 Mar 28 01:10 ..
-rw-rw-r--  1 prayag prayag 2273 Mar 28 01:00 AndroidManifest.xml
drwxrwxr-x  3 prayag prayag 4096 Jul 17  2012 assets
drwxrwxr-x  4 prayag prayag 4096 Mar 28 19:05 bin
-rw-rw-r--  1 prayag prayag  416 Jul 17  2012 .classpath
drwxr-xr-x  5 prayag prayag 4096 Mar 29 00:40 facebook-android-sdk-3.0.1.git
drwxrwxr-x  3 prayag prayag 4096 Mar 28 19:03 gen
drwxrwxr-x  9 prayag prayag 4096 Mar 29 00:40 .git
-rw-r--r--  1 root   root    226 Mar 27 13:53 .gitignore
-rw-rw-r--  1 prayag prayag  164 Mar 29 00:40 .gitmodules
drwxrwxr-x  2 prayag prayag 4096 Mar 28 00:56 libs
-rw-rw-r--  1 prayag prayag  781 Jul 17  2012 proguard-project.txt
-rw-rw-r--  1 prayag prayag  820 Mar 26 15:08 .project
-rw-rw-r--  1 prayag prayag  629 Mar 28 00:54 project.properties
drwxrwxr-x 11 prayag prayag 4096 Mar 24 22:26 res
drwxrwxr-x  4 prayag prayag 4096 Mar 28 01:02 src


[9] Recover file permissions

$ git diff -p -R \
    | grep -E "^(diff|(old|new) mode)"  \
    | git apply
OR 

add as an git alias (let's call it permission-reset)
$ git config --global --add alias.permission-reset '!git diff -p -R | grep -E "^(diff|(old|new) mode)" | git apply'


If you don't see any changes when modifying permission, you probably have a configuration in git which ignore file mode.


[core]
    filemode = false

If the above doesn't work, fire the following command manually
prayag@prayag:~/hacker_/doctorhere-servlet$ git config core.filemode false

Also check git-preserve-permissions.


[10] git log --graph
9.1 show file history with patch
$ git log --follow --all -p grails-app/controllers/eccount/StudentController.groovy


OR git short log with --patch
$ git log -p grails-app/controllers/eccount/StudentController.groovy


[11] git difftool
add [diff] section to ~/.gitconfig.

[color]
  diff = auto
  status = auto
  branch = auto
  interactive = auto
  ui = true
  pager = true

[diff]
     tool = vimdiff

OR 

$ git config --global diff.tool vimdiff

Diff a single file with following command
$ git difftool grails-app/conf/Config.groovy

For shortcuts, follow vimdiff commands.



References
Git 1 2 3 by Jordan Schatz,

My git gist, https://gist.github.com/iPrayag/4730500

Try git, http://try.github.io/levels/1/challenges/1

http://git-scm.com/documentation

Git asks me to commit or stash changes on checkout master, even though all changes were commited?,  http://stackoverflow.com/a/13204493/432903

Change commit author at one specific commit, http://stackoverflow.com/a/3042512/432903

25 Tips for Intermediate Git Users, http://andyjeffries.co.uk/articles/25-tips-for-intermediate-git-users

https://github.com/jerith666/git-graph

Git error on commit after merge - fatal: cannot do a partial commit during a merge, http://stackoverflow.com/a/8062976/432903

is it possible to `git status` only modified files?, http://stackoverflow.com/a/13479518/432903

Git: how to diff the same file between two different commits on the same branch?

http://stackoverflow.com/a/5741436/432903

Thursday, 22 November 2012

VImroved 101

Vim is an advanced text editor that provides the power of the de-facto Unix editor 'Vi' with a more complete feature set.

The console version of vim comes preinstalled with ubuntu, a cut-down version called "vim-tiny".

STEP 1 :  install the package vim-gnome, for a GUI based vim

prayag@prayag:~$ sudo apt-get install vim-gnome ## on linux ubuntu
[...]
Processing triggers for libc-bin ...
ldconfig deferred processing now taking place


STEP 2 : install vim, with advanced functions

prayag@prayag:~$ sudo apt-get install vim ## on ubuntu
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libqtwebkit4 skype-bin
[...]
Unpacking vim (from .../vim_2%3a7.3.429-2ubuntu2.1_i386.deb) ...
Setting up vim (2:7.3.429-2ubuntu2.1) ...

STEP 3 : edit file with line numbers
$ vi grails-app/domain/eccount/Staff.groovy
add [ESC] :set nu at the end to enable line numbers or add set number to ~/.vimrc to enable line numbers every time.

u                   -  undo last change
Ctrl + r -  redo last change
Shift + w      -  Jump forward word
b                   -  Jump backward word


:w     -  write to file OR
:w !sudo tee % > /dev/null


STEP 4 : goto exact line number
add [ESC] :29 at the end.



STEP 5 : Open file in a new tab from a working tab
:tabe <filename>




Switch to previous tab
:tabp


Switch to nth tab
:tabn TAB_INDEX

STEP 6 : Scroll up/down
Scroll up : SHIFT + Page Up
Scroll Down : SHIFT + Page Down


STEP 7 : text selecting and copying

v - to enter visual mode
x  - to cut
y  - to yank
[ + p - before cursor
p  - to paste
] + p - after cursor

(Reference  : copy lines using visual mode in vim)

copy to clipboard, (first enable +clipboard, make sure it is enabled using vim --version)
In MacOS I had to enable following settings in vimrc
    set clipboard=unnamed


$ vim --version | grep clip
-clipboard       +job             +path_extra      -toolbar
+ex_extra        -mouse_gpm       -sun_workshop    -xterm_clipboard
:%y+ ##+ is a register


STEP 8 : running shell command from inside VI editor

:!ls

STEP 9 : Indentation

Go to the start of the text,
 - press [v] for visual mode.
 - use [up]/[down] arrow to highlight text.
 - press [=] to indent all the lines highlighted.


STEP 10 : folding
Syntax folding

##add to ~/.vimrc

set foldmethod=syntax
set foldlevelstart=1

let javaScript_fold=1         " JavaScript
let perl_fold=1               " Perl
let php_folding=1             " PHP
let r_syntax_folding=1        " R
let ruby_fold=1               " Ruby
let sh_fold_enabled=1         " sh
let vimsyn_folding='af'       " Vim script
let xml_syntax_folding=1      " XML

STEP 10 replace chars/words
:s/node2/yarn-node  # only one occurrence
:%s/node2/yarn-node/g  # all occurrences, % = all followed by pattern



use \ to escape special characters.

## replace \" with " in json data

:%s/\\"/"/g





References