Change tab width in vim

By default, inside vim the tab width is 8 spaces, we can reduce it using below steps.

Create a .vimrc file in ~(home dir) and add below content.  Save it and now try.

filetype plugin indent on
" show existing tab with 4 spaces width
set tabstop=4
" when indenting with '>', use 4 spaces width
set shiftwidth=4
" On pressing tab, insert 4 spaces
set expandtab

send foreground job/running process to run in background

1.Moving Linux job from Foreground Into background :

  • start the program
  • Ctrl+Z (Will pause the program on the terminal)
  • jobs (find the job number from current shell)
  • bg  %jobnum

 

NOTE : Here we are only sending the process into background but exiting the programe still use the child shell of the current shell. So exiting the shell/terminal will kill the process.

 

2. Moving Linux jobs into background(nohup mode, freeing the shell)

  • Start the program
  • Ctrl+Z (Will pause the program on the terminal)
  • bg to run it in the background
  • disown -h ( shell disowns the process and will not get SIGHUP, so its kind of nohup mode)
  • exit (To exit from the shell)
  • Check in other terminal if the process is still running

 

NOTE : Here  running process is moved into background, and exiting the shell will not kill the process and will still run.

Docker Private Registry Setup

We can create our own secure private  docker repository where we can store our images and can be accessed from remote machine.

1. Goto /var/lib/docker in server and Create certificate using the domain name:

cd /var/lib/docker && mkdir certs
 mkdir -p certs && openssl req -newkey rsa:4096 -nodes -sha256 -keyout certs/sl09vmf0022.us.company.com.key -x509 -days 365 -out certs/sl09vmf0022.us.company.com.crt

2. Delete any old registry if exists:

docker rm  OR docker rmi registry:2

3. Recreate the registry using the newly created certificates by staying in the cert dir:

docker run -d -p 5000:5000 --restart=always --name bkdevregistry -v `pwd`/certs:/certs -e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/sl09vmf0022.us.company.com.crt -e REGISTRY_HTTP_TLS_KEY=/certs/sl09vmf0022.us.company.com.key registry:2

4. Goto docker cert dir and copy the crt file as ca.crt and restart docker service

cd /etc/docker/certs.d/sl09vmf0022.us.company.com\:5000/
 cp /var/lib/docker/sl09vmf0022.us.company.com.crt /etc/docker/certs.d/sl09vmf0022.us.company.com\:5000/ca.crt
 update-ca-trust enable
 service docker restart

5. Now push images to private repository:

docker pull ubuntu
 docker tag ubuntu sl09vmf0022.ua.company.com:5000/ubuntu1404
 docker push sl09vmf0022.ua.company.com:5000/ubuntu1404

6. Client side configuration:

Copy the ca.crt file from docker registry server to local docker cert dir and restart docker service

mkdir -p /etc/docker/certs.d/sl09vmf0022.us.company.com\:5000/
 scp sl09vmf0022.us.company.com:/var/lib/docker/certs/sl09vmf0022.us.company.com.crt /etc/docker/certs.d/sl09vmf0022.us.company.com:5000/ca.crt
 service docker restart

7. Pull image from remote registry :

docker pull sl09vmf0022.us.company.com:5000/oel6u6

8. Check images in remote registry available using the crt file or in insecure mode:

curl -X GET https://sn09vmf0022.us.company.com:5000/v2/_catalog --cacert /etc/docker/certs.d/sn09vmf0022.us.company.com\:5000/ca.crt

OR

curl -X GET https://sl09vmf0022.us.company.com:5000/v2/_catalog --insecure

My Git commandline Cheat-sheet

Creating a new repository

mkdir project

cd project

git init

git remote add origin git@github.com:yourlogin/your-repo.git

git add .

git commit -am “new repository”

git push -u origin master

Cloning existing repository

git clone https://github.com/username/your-repo.git

Creating branch

git checkout -b feature-1

# you are now in a branch, you can edit and create new files

git add .

git commit -am “new feature”

Merging branch to master

git checkout master

git merge feature-1

git push

Deleting branch

git branch -d feature-x

List all branches

git branch -a

Switch branch

git checkout feature-x

Switch to master branch

git checkout master

Listing Remote repositories

git remote -v

Replacing remote repository

# in case your remote repository changes, or you want to switch from HTTPS->SSH or SSH->HTTPS

git remote remove origin

git remote add origin git@github.com:yourlogin/your-repo.git

One liner tricks using perl/bash

Sometime small small hacks saves a lot of our time. The regexes used here can be changed as per need. In some cases try alternatively with single/double quote if dont work.

Count the number of times a specific character appears in each line

  • This counts the number of quotation marks in each line and prints it
    perl -ne ‘$cnt = tr/”//;print “$cnt\n“‘ inputFileName.txt

Add string to beginning of each line

  • Adds string(slc) to each line,
    perl -pe “s/(.*)/slcn\$1/” in.txt > out.txt

 Add string to end of each line

  • Append a string(us.o.com) to each line
     perl -pe “s/(.*)/\$1.us.o.com/” in.txt > out.txt

Print only alternate values in a list

  • Sometime froma list we have to print only alternate values, We can use the special $| of perl which stores onlt 1 or 0, Below will print the alternate from a list (a..z ) starting with a.
    perl -E  ‘say grep –$|, a..z’

Print only some columns of a file

  • Columns separated by a space
    cut fileWithLotsOfColumns.txt -d” “ -f 1,2,3,4 > fileWithOnlyFirst4Cols.txt

Print all columns except the first

  • cut -d” “ -f 1 –complement filename > filename.

 Replace a pattern with another one inside the file with backup

  • Replace all occurrences of pattern1 (e.g. [0-9]) with pattern2
    perl -p -i.bak -w -e ‘s/pattern1/pattern2/g’ inputFile

 Print only non-uppercase letters

  • Go through file and only print words that do not have any uppercase letters.
    perl –ne ‘print unless m/[A-Z]/’ allWords.txt > allWordsOnlyLowercase.txt

 Print one word per line

  • Go through file, split line at each space and print words one per line.
    perl –ne ‘print join(“\n”, split(/ /,$_));print(“\n”)’ someText.txt > wordsPerLine.txt

 Kill all screen sessions (no remorse)

  • Since there’s no screen command that would kill all screen sessions regardless of what they’re doing, here’s a perl one-liner that really kills ALL screen sessions without remorse.
    screen -ls | perl –ne ‘/(\d+)\./;print $1’ | xargs -l kill –9
  • The killall command may also do the job…

Return all unique words in a text document (divided by spaces), sorted by their counts (how often they appear)

  • assuming no punctuation marks:
    perl -ne ‘print join(“\n“, split(/\s+/,$_));print(“\n“)’ documents.txt > wordsOnePerLine.txt
    cat wordsOnePerLine.txt | sort | uniq -c  | sort -n > wordCountsSorted.txt

 Delete all special characters

  • delete every character that is not a letter, white space or line end (replace with nothing)
    perl -pne ‘s/[^a-zA-Z\s]*//g’ text_withSpecial.txt > text_lettersOnly.txt

 Lower case everything

  • perl -pne ‘tr/[A-Z]/[a-z]/’ textWithUpperCase.txt > textwithoutuppercase.txt;

Combine lower-casing with word counting and sorting

  • perl -pne ‘tr/[A-Z]/[a-z]/’ sentences.txt | perl –ne ‘print join(“\n”, split(/ /,$_));print(“\n”)’ | sort | uniq -c | sort -n

 Print only one column

  • Print only the second column of the data when using tabular as a separator
    perl –ne ‘@F = split(“\t”, $_); print “$F[1]”;’ columnFileWithTabs.txt > justSecondColumn.txt

 Print only text between tags

  • perl –ne ‘if (m/\<a\>(.*?)\<\/a\>/g){print “$1\n”}’ textFile
  • Extracting multiple multiline patterns between a start and an end tag
    • Here, we want to extract everything between <parse> and </parse>.
    • #!/usr/bin/perl -w
      local $/;open(DAT, “yourFile.xml”) || die(“Could not open file!”);
      my $content = <DAT>;while ($content =~ m/<parse>(.*?)<\/parse>/sg){
      print “$1\n
      };

Sort lines by their length

  • perl -e ‘print sort {length $a <=> length $b} <>’ textFile

Print second column, unless it contains a number

  • perl -lane ‘print $F[1] unless $F[1] =~ m/[0-9]/’ wordCounts.txt

Trim/ Collapse white spaces and replace new lines by something else

  • echo “The cat sat    on    the  mat
    asd  sad  das   “
    | perl  –ne ‘s/\n/ /; print $_; print(“;”)’ | perl –ne ‘s/\s+/ /g; print $_’

Get the average of one column from certain lines

  • grep “another criterion” thisDataFile.txt |  perl –ne ‘@F = split(“,”, $_); print “$F[29]\n”;’ | awk ‘{sum+=$1} END { print “Average = “,sum/NR}’

How to sort a file by a column

  • Columns are separated by a space, we sort numerically (-n) and we sort by the 10’th column (-k10)
  • sort -t’ ‘ -n -k10 eSet1_both.txt

 Replace specific space but also copy a group of matches

  • matches a group of numbers in the beginning of a line
  • perl -p -i.bak -w -e ‘s/^([0-9]+) “/$1\t”/g’ someFile.txt