Linux must know


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:
"\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
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:
 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'

2. Bash functions. Unfortunately one can not pass an argument to aliases. One can do it with functions
Examples:
function c() { cd "$1" ; ls ;} # F.e. typing c ~ gives the same result as cd ~ ; ls ;.
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:
#"\e[5~": history-search-backward
#"\e[6~": history-search-forward
"\e[A": history-search-backward
"\e[B": history-search-forward
Then restart your bash and now it should work.

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.

http://www.linuxhorizon.ro/ssh-wo-passw.html

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:
http://linuxproblem.org/art_9.html

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

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

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    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.

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.
  • Open for editing the .bashrc file, for example type in your command line:
gedit ~/.bashrc
  • Put this line there:
alias ls='ls --color=auto'
  • Save, restart bash
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.


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

  1. Command line
find . | xargs grep 'string' -sl
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).

Finds all subdirectories of ANY_PATH folder:
find ANY_PATH -type d

Finds all files of ANY_PATH folder:
find ANY_PATH -type f

Finds all files with .log extensionof ANY_PATH folder:
find ANY_PATH -name \*.log

See also for find command:
http://www.linux.ie/newusers/beginners-linux-guide/find.php

Examples: goes through all subdirectories of InitialData_2 and prints out the subdirectory which does not contain a file with .log extension:
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

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":
for i in *.xyz ; do echo $i; fn=${i%.*}; eval tail -n +3 "$i" | gawk '{print $2 " " $3 " " $4}' > "$fn".coors ; done

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/":
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

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

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

Skipping the first and the last symbols from string variable:
i='_VAR_'
fn=${i:1:$((${#i}-1))};
echo $fn
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:
fn='1,2-dimethilbuthane'
fn=${fn//\,/\_}
echo $fn
RESULT: 1_2-dimethilbuthane

Generating a sequence of digits:
for i in `seq 1 10` ; do echo $i ; done


Open access to a folder (file)

The following command line command allows to open access to the files (folders) to other users:
chmod -R 755 <file>
chmod -R 755 <folder>


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:
export PATH=new_dir:$PATH
example: export PATH=~/scripts:$PATH


Python

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


Set default PDF viewer to acroread

In Ubuntu terminal type:
xdg-mime default acroread.desktop application/pd



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