[Ubuntu] Use nano

  1. Show line when use nano

    nano -c file.txt
  2. Go to specify line

    nano -c +2 file.txt  // Go to line 2

 

 

Advertisements

Use grep command line

1.  Use grep to filter command line history

history | grep 'text'

 

2. Search text in all file of folder

grep -rnw '/path/to/folder/' -e 'text-to-search'
  • -r or -R is recursive,
  • -n is line number, and
  • -w stands for match the whole word.
  • -l (lower-case L) can be added to just give the file name of matching files.

 

3. Find files with name

find | grep "name_file"

 

4. Search texts with no contain char

grep(/^abc(?!e).*/)

Ex:

array = [“abcdx”, “abcex”, “abcfx”]

array.grep(/^abc(?!e).*/) => [“abcdx”, “abcfx”]

Use “alias” in Linux

In file “~/.bash_profile“, we can use alias to define keyword shorter to run app faster 🙂

Example: In server has been installed redis. To run redis-server we must run command “/opt/redis/latest/bin/redis-server”. We can use alias in “~/.bash_profile“:

alias redis-server="/opt/redis/latest/bin/redis-server"

And with redis-cli we must run command: “/opt/redis/latest/bin/redis-cli”. We can use alias:

alias redis-cli="/opt/redis/latest/bin/redis-cli"

And now, we can type redis-server or redis-cli in terminal instead of long command: “/opt/redis/latest/bin/redis-server” and “/opt/redis/latest/bin/redis-cli”.

 

When we added alias to “~/.bash_profile“, we can reload “~/.bash_profile” by the way:

. ~/.bash_profile

 

Change the Command-Line Prompt Colour in the Ubuntu/Linux Terminal

First, open file bashrc

gedit ~/.bashrc

Find line has text: if [ “$color_prompt” = yes ]; then

Copy context in if clause and paste to else clause

Save, close and reopen terminal. 🙂

Optional information:

Black 0;30 – Dark Gray 1;30 – Blue 0;34 – Light Blue 1;34 – Green 0;32 – Light Green1;32 – Cyan 0;36 – Light Cyan 1;36 – Red 0;31 – Light Red 1;31 – Purple 0;35 – Light Purple 1;35 – Brown 0;33 – Yellow 1;33 – Light Gray 0;37 – White 1;37

 

[Linux] Using the Vim editor

Commands that switch the editor to insert mode

  • a will append: it moves the cursor one position to the right before switching to insert mode
  • i will insert
  • o will insert a blank line under the current cursor position and move the cursor to that line.
  • Esc key switches back to command mode.

Pressing the Esc key switches back to command mode. If you’re not sure what mode you’re in because you use a really old version of vi that doesn’t display an “INSERT” message, type Esc and you’ll be sure to return to command mode. It is possible that the system gives a little alert when you are already in command mode when hitting Esc, by beeping or giving a visual bell (a flash on the screen). This is normal behavior.

Basic operations

These are some popular vi commands:

  • n dd will delete n lines starting from the current cursor position.
  • n dw will delete n words at the right side of the cursor.
  • x will delete the character on which the cursor is positioned
  • :n moves to line n of the file.
  • :w will save (write) the file
  • :q will exit the editor.
  • :q! forces the exit when you want to quit a file containing unsaved changes.
  • :wq will save and exit
  • :w newfile will save the text to newfile.
  • :wq! overrides read-only permission (if you have the permission to override permissions, for instance when you are using the root account.
  • /astring will search the string in the file and position the cursor on the first match below its position.
  • / will perform the same search again, moving the cursor to the next match.
  • :1, $s/word/anotherword/g will replace word with anotherword throughout the file.
  • yy will copy a block of text.
  • n p will paste it n times.
  • :recover will recover a file after an unexpected interruption.

Refer: http://www.tldp.org/LDP/intro-linux/html/sect_06_02.html

[Docker] Error: Can’t not access internet from container in Ubuntu 14.04

When I use Docker, I create container from image (Ex: Ubuntu:14.04) and ssh to this container by command:

docker run -it <image_id> bash

When I ssh to container, I used command ping google.com 

but I get error:  ping: unknown host google.com

To fix it, I have 2 solutions, but first, you must get DNS of your machine by command

nm-tool 

Result in my machine:

DNS: 192.168.30.29
DNS: 192.168.30.66

=> I’ll use DNS: 192.168.30.29

1. Solution 1

– Go to file sudo nano /etc/docker/daemon.json and add your DNS to this file

{
    "dns": ["192.168.30.29", "8.8.8.8", "8.8.4.4"]
}

Save it and restart docker by command: sudo service docker restart

2. Solution 2

solution 1- Go to file sudo nano /etc/default/docker, uncomment and add your DNS to this row:

DOCKER_OPTS="--dns 192.168.30.29 --dns 8.8.8.8 --dns 8.8.4.4 --ip-masq=true"

Save it and restart docker by command: sudo service docker restart

If when restart, you get error:

docker stop/waiting
start: Job failed to start

or error:

stop: Unknown instance: 
start: Job failed to start

– You shoud go to file sudo nano /etc/default/docker, and comment row

DOCKER_OPTS="--dns 192.168.30.29 --dns 8.8.8.8 --dns 8.8.4.4 --ip-masq=true"

and try again with solution 1.

==> After you try adding DNS successfully, you can ssh to container and run

ping google.com once again. And this is my result after try fixing with solution 1

root@2beb109bc7b3:~# ping google.com
PING google.com (113.171.239.123) 56(84) bytes of data.
64 bytes from static.vnpt.vn (113.171.239.123): icmp_seq=1 ttl=56 time=1.06 ms
64 bytes from static.vnpt.vn (113.171.239.123): icmp_seq=2 ttl=56 time=1.51 ms
64 bytes from static.vnpt.vn (113.171.239.123): icmp_seq=3 ttl=56 time=2.32 ms
64 bytes from static.vnpt.vn (113.171.239.123): icmp_seq=4 ttl=56 time=1.19 ms
64 bytes from static.vnpt.vn (113.171.239.123): icmp_seq=5 ttl=56 time=1.55 ms
.....

That’s all. Thank you for reading. 🙂

[Docker] Error Cannot connect to the Docker daemon. Is the docker daemon running on this host?

In Ubuntu, when I install Docker successfully, I try running with command:

docker images #show all images in local

but it has error

Cannot connect to the Docker daemon. Is the docker daemon running on 
this host?

because it doesn’t have permission of root. I can run it by command

sudo docker images

Another way, we can config it to needn’t use sudo

  • Step 1: go to /var/run

    cd /var/run
  • Step 2: Add permission for file docker.sock
    sudo chmod -R 776 docker.sock
    

And now, we can use command docker images for shorter code

 

Access remote server with local phpMyAdmin in Ubuntu

1

Default, the login page of phpmyadmin have only username & password. If you want to add some remote server you can follow by some steps:

  1. Find file “/etc/phpmyadmin/config.inc.php”
  2. Find row “/* Advance to next server for rest of config */” and add some configs below “$i++;”

/* Advance to next server for rest of config */
$i++;

/* some additional configs  */
$cfg[‘Servers’][$i][‘host’] = ‘52.69.150.47’;
$cfg[‘Servers’][$i][‘user’] = ‘mery_box’;
$cfg[‘Servers’][$i][‘password’] = ‘TewKszmceNQA’;
$cfg[‘Servers’][$i][‘auth_type’] = ‘config’;

And the result:

1

Can’t show group chat Skype in Ubuntu 14.04

I installed Skype in Ubuntu 14.04,  but when I logged in to Skype, I only show and chat with my friends and can’t show any groups, I fixed it by the way:

Open chat box with any my friend and type:

/dumpmsnp   #show details about your connection

And I showed log:

System: MSNP: Connection Data:
 * Status: LoggedOut

Ref:It’s LoggedOut   => It is problem

To solve it, type:

/msnp24

Then log out and restart Skype. After that, type again:

/dumpmsnp 

And I see:

System: MSNP: Connection Data (MSNP24):
 * Status: LoggedIn

And I also see all my groups in Skype, and the issue was solved.

Ref:  https://community.skype.com/t5/Mac-archive/New-chat-groups-not-showing-up-in-client/td-p/3206434