Linux+must+know

=Linux must know=

toc

Table of contents

 * 1) ==Delete and backspace keys in terminal==
 * 2) ==Aliases and command abbreviations==
 * 3) ==Smart Bash history search==
 * 4) ==SSH without password ==
 * 5) ==Make your ls command printing in colors ==
 * 6) ==Change the default shell (e.g to bash)==
 * 7) ==Command line search==
 * 8) ==BASH data processing==
 * 9) ==Open access to a folder (file)==
 * 10) == Tabs and mouse in vim==
 * 11) == Set default PDF viewer to acroread==
 * 12) ==Python==
 * 13) ==How to set up PATH variable==
 * 14) ==Common commands in .bashrc==
 * 15) =unzip=

Delete and backspace keys in terminal
If instead of deleting a character the "delete" or "backspace" keys give you "~" or similar you should add the following commands into your ~/.inputrc file and restart BASH: code format="bash" "\e[3~": delete-char "\e[1~": beginning-of-line "\e[4~": end-of-line "\e[H":beginning-of-line "\e[F":end-of-line "\e[7~":beginning-of-line "\e[8~":end-of-line code see more in [|http://www.ibb.net/~anne/keyboard/keyboard.html#Bash].

Aliases and command abbreviations
These commands and functions are aimed to make life much easier: you can make a command abbreviation, gather commands ... You just need to type the commands in your shell or/and add this to your ~/.bashrc file:

1. Alias. Alias renames a command. Examples: code format="bash" alias ls='ls --color=auto' alias l='ls --color=auto' # typing l in command line gives the same result as ls --color=auto. alias cb='cd .. ; l' alias v='vim' alias sshgarching='ssh -i $HOME/.ssh/id_garch username@vip.rzg.mpg.de' code

2. Bash functions. Unfortunately one can not pass an argument to aliases. One can do it with functions Examples: code format="bash" function c { cd "$1" ; ls ;} # F.e. typing c ~ gives the same result as cd ~ ; ls ;. code Mind previously declared aliases work under functions.

Smart Bash history search
Everyone who is working under linux/unix machine would like to have a smart history search in the command line like it is realized in Matlab. For example, if you are just press the "up" button then the last typed command will be displayed, but if your first type for example letter "a" and then push "Up" then the last command starting with "a" will appear.

Sounds tempting?

If you are lucky and your default shell is //**Bash**// (see change the default shell) then you have to create the file "**//.inputrc//**" (or open) in your home directory and put there the following lines: code format="bash" "\e[A": history-search-backward "\e[B": history-search-forward code Then restart your bash and now it should work.
 * 1) "\e[5~": history-search-backward
 * 2) "\e[6~": history-search-forward

For those who uses **CShell** (sign ">" in the command line) - change this horrible thing to the **Bash** shell (sine $ ). :)

To indicate what is your shell, just look at the last symbol in your terminal: sign **">"** - **CShell**, sign **$** - **Bash**

SSH without password
Please, follow the instructions. I did it once and it required some attempts to make it properly. As soon as you succeed, please, add here the exact instructions.

[]

Here the script which makes the commands described in the above web-page automatically: usage: >>1) save the file on your working machine. >>2) make sure the PATH variable includes the path to the script: PATH variable >>3) Make the file executable: chmod +x ssh_wo_pwd.sh >>4) run the script: ssh_wo_pwd.sh login@server

---

The general case is described here: []

In our particular case, we have a common file system, thus these instructions seem to reduce to: code format="bash" ssh-keygen -t rsa cat ~/.ssh/id_rsa.pub >> ~.ssh/authorized_keys code

--- NB: If you system have been upgraded (to ubuntu10 e.g.), you may face a problems accessing you machine form the outside: code format="bash" ssh neso code

code @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @   WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY! Someone could be eavesdropping on you right now (man-in-the-middle attack)! It is also possible that the RSA host key has just been changed. The fingerprint for the RSA key sent by the remote host is a0:17:d6:74:86:6d:1a:00:7f:00:bf:19:da:7b:75:cf. Please contact your system administrator. Add correct host key in /usr/people/sergiiev/.ssh/known_hosts to get rid of this message. Offending key in /usr/people/sergiiev/.ssh/known_hosts:31 RSA host key for neso has changed and you have requested strict checking. Host key verification failed. code

My solution: remove in the ~/.ssh/known_hosts all mentioned lines (here - line 31). After that you host will become unknown, and you can repeat procedure above to make it known...

Make your ls command printing in colors
Your have to add to the Bash "initializations" file **.bashrc** in your **home directory** an alias. gedit ~/.bashrc code format="bash" alias ls='ls --color=auto' code Now it should work. For those who are working with horrible **CShell** : 1. Change to Bash :) 2. If you insist working with **CShell**, then these instructions should be valid but you have to modify not **.bashrc**, but **.cshrc** instead.
 * Open for editing the **.bashrc** file, for example type in your command line:
 * Put this line there:
 * Save, restart bash

Change the default shell
If you want to have bash as your terminal shell, you could configure the GNOME terminal correspondingly (Edit->Current Profile, Title and Command, check Run custom command ...).


 * Be careful!** it may lead to closing the existing terminals, and programs which were run in them (e.g. windows session)

Command line search
code format="bash" find. | xargs grep 'string' -sl code The //-s// is for summary and won't display warning messages such as grep: ./directory-name: Is a directory The //-l// is for list, so we get just the filename and not all instances of the match displayed in the results. Additional key: //mtimes// - for the time of changes in the file (= 1, -1).
 * 1) **Command line**

Finds all subdirectories of ANY_PATH folder: code format="bash" find ANY_PATH -type d code

Finds all files of ANY_PATH folder: code format="bash" find ANY_PATH -type f code

Finds all files with .log extensionof ANY_PATH folder: code format="bash" find ANY_PATH -name \*.log code

See also for //find// command: []

Examples: goes through all subdirectories of InitialData_2 and prints out the subdirectory which does not contain a file with .log extension: code format="bash" for i in `find InitialData_2/ -type d`; do eval a=`ls "$i" | grep ".log" | wc -l` ; if [ "$a" -ne "1" ] ; then echo $i ; fi ; done code

BASH data processing
The following command line command takes all files with xyz extension form the current directory, skips first 2 lines, skips the first column, and prints the rest (coordinates) into file with the same name but extension "coors": code format="bash" for i in *.xyz ; do echo $i; fn=${i%.*}; eval tail -n +3 "$i" | gawk '{print $2 " " $3 " " $4}' > "$fn".coors ; done code

This command takes all files that end with "_input_RISM" from all subdirectories starting from current ("."). Extracts the file name, prints the file name, copies the file to directory "/net/maxwell/people/frolov/distr/RISM_MOL/LastReleaseRismMol/Systems/": code format="bash" for i in `find. -name *_input_RISM`; do fn=${i##*/}; echo $fn; cp $i /net/maxwell/people/frolov/distr/RISM_MOL/LastReleaseRismMol/Systems/$fn; done code

Getting file_name from file with extension (splitting string according to . symbol): code format="bash" f='file_name.dat' fn=${f%.*} echo $fn code RESULT: file_name

code format="bash" n='/home/usr/bin' fn=${n##*/} echo $fn code RESULT: bin

Skipping the first and the last symbols from string variable: code format="bash" i='_VAR_' fn=${i:1:$((${#i}-1))}; echo $fn code RESULT: VAR Comment: ${#i} - number of symbols in //i//. $((${#i}-1)) - perform arithmetic operation - result number of symbols -1 minus , ${i:1:2} - get all symbols with indexes 1 to 2 from var i.

Regular expression: change all symbols "," to "_" in string variable: code format="bash" fn='1,2-dimethilbuthane' fn=${fn//\,/\_} echo $fn code RESULT: 1_2-dimethilbuthane

Generating a sequence of digits: code format="bash" for i in `seq 1 10` ; do echo $i ; done code

=Open access to a folder (file)= The following command line command allows to open access to the files (folders) to other users: code format="bash" chmod -R 755 chmod -R 755 code

Tabs and mouse in vim
To turn on mouse in vim:
 * set mouse=a

To open new tab:
 * tabnew

OR
 * tabnew filename

How to set up PATH variable
In bash: code export PATH=new_dir:$PATH code example: export PATH=~/scripts:$PATH

Python
import re str(" \"Name\" ")
 * brackets in a line

Set default PDF viewer to acroread
In Ubuntu terminal type: code format="bash" xdg-mime default acroread.desktop application/pd code

Command commands in .bashrc
Here is an example bashrc file from Frolov:

Unzip
Extract all files of the zip into the /tmp directory unzip data.zip -d /tmp