Cognitionis
The little I know

Linux Basic Kit


Basic configuration:

How to modify GRUB boot menu –> /boot/grub/menu.lst
How to modify update fonts to Universal –> /etc/apt/sources.lst (or GUI in system->admin)
How to install other types of shell –> sudo aptitude install tcsh — chsh -s /bin/tcsh

How to get super-user in old Ubuntus –> sudo passw

VARIABLES (actualitzar path.. por ejemplo para cambiar la versión de java q ejecuto)
BASH
Para lo del java por ejemplo:
PATH=/tools/java5/bin/:$PATH; export PATH
Nota: La primera ruta q pilla es la q usa por eso :$PATH va al final

PATH=$PATH:newdir…
echo $PATH
export PATH

ECHO Command

Use echo command to display text or value of variable.
echo [options] [string, variables...]
Displays text or variables value on screen.
Options
-n Do not output the trailing new line.
-e Enable interpretation of the following backslash escaped characters in the strings:
\a alert (bell)
\b backspace
\c suppress trailing new line
\n new line
\r carriage return
\t horizontal tab
\\ backslash
IMPORTANT: only use -e option in bash shell and only when the script do not contain #!/bin/sh declaration

QUOTES
There are three types of quotes
” Double Quotes “Double Quotes” - Anything enclose in double quotes removed meaning of that characters (except \ and $).
‘ Single quotes ‘Single quotes’ - Enclosed in single quotes remains unchanged.
` Back quote `Back quote` - To execute command
Example:
$ echo “Today is date” #Can’t print message with today’s date.
$ echo “Today is `date`”#It will print today’s date as, Today is Tue Jan ….,Can you see that the `date` statement uses back quote?

EXIT Status
(1) If return value is zero (0), command is successful.
(2) If return value is nonzero, command is not successful or some sort of error executing command/shell script.

Wild cards (Filename Shorthand or meta Characters)

WILD CARDS / Shorthand Meaning Examples
* Matches any string or group of characters.
? Matches any single character.
[...] Matches any one of the enclosed characters
[..-..] A pair of characters separated by a minus sign denotes a range.
Example:
$ ls /bin/[a-c]*

LIST (LS)

ls -la *

MAKE SYMBOLIC LINKS (LN)

ln -s “dir or file” “link name” (link is created in current folder if no other location specified)

FIND FILES
find [path] [expression]
Example:
find /home -iname ‘*.avi’ # finds any avi in our home directory
find ~/home/ -name ‘*.avi’ -a -size +700M -mtime -15 # finds any avi bigger than 700MB and modifyed in the last 15 days
~ for recursive searching (all subdirectories)
whereis filename
locate filename
dpkg packagename #finds files in packages

COMMAND LINE ARGUMENTS
$# holds number of arguments specified on command line. And $* or $@ refer to all arguments passed to script.

ARITMETIC EXPRESSIONS (SUM, SUBSTRACTION, MULTIPLICATION AND DIVISION)

Bash do not manage floating point operations. Here are two basic ways to operate:
let expression # example let 2+2; let a=4/3 (a=0)…
(( expression )) # example a=$((2+2))
To manage floating point operations you can use bc (shell calculator):
Example:
echo “scale=4; ($rel+25)/1000″ | bc -l #scale is the desired precission (number of decimals), rel is an integer number
But there is an inconvenience with bc, it does not take locale in account for printing the decimal separator (comma in spanish) and always displays a point. Then if you whant to make the previous operation using your locale you have to use for example gawk that takes locale into account.
Trick:
echo “$rel” | gawk ‘{frel=$1; frel=(frel+25)/1000; printf(”%s”,frel)}’ #were rel is an integer number

VIM

Linux command line text editor. Use esc key to switch between edit or command mode.

Useful commands:
i insert
:q! exit without saving
:wq exit saving
Y copy a line
P paste a line
/ for search (i.e.: /john searches for john in text)
s/search/replace/

TR
Substituye cadenas en un texto (text replace)
tr “\n” ” ” < fichero.txt // cambia los saltos de linea por espacios (muy util)
cat fichero.txt | tr ” ” “\n” // operacion inversa y llamandolo de otro modo posible

GREP
Muestra las lineas que contengan la expresión regular (ej: cat fichero | grep hola, muestra las lineas que contengan hola). Tiene las mimas expresiones regulares que sed.

trick (GREP CONTEXT): Tiene una funcion de lineas de contexto -C arriba/abajo -A abajo -B arriba para sacar n lineas de contexto que es muy buena.

SED
Useful: Print concrete line: sed ‘numLineq;d’ < file —- example: sed ‘600q;d’ < bible

numbering:

# number each line of a file (simple left alignment). Using a tab (see
# note on ‘\t’ at end of file) instead of space will preserve margins.
sed = filename | sed ‘N;s/\n/\t/’

# number each line of a file (number on left, right-aligned)
sed = filename | sed ‘N; s/^/ /; s/ *\(.\{6,\}\)\n/\1 /’

# number each line of file, but only print numbers if line is not blank
sed ‘/./=’ filename | sed ‘/./N; s/\n/ /’

Edita el texto, normalmente usado como filtro, remplazar algo, borrar algo,…
sed ‘accion/palabra a buscar/palabra con la que substituri/…’
s/a/b/ substituye a por b (una vez por linea, 1a ocurrencia)
s/a/b/g substituye a por b (global, todas las ocrurrencias)
/a/d borra todas las a
-r expresiones extendidas… (no se usa casi)

sed ’s/foo/bar/g’ filename # standard replace command
sed ‘/foo/ s/foo/bar/g’ filename # executes more quickly
sed ‘/foo/ s//bar/g’ filename # shorthand sed syntax

TRICK: If you try to replace newlines like “s/\n\n/\n/g” you will notice it do not work. Solution:

sed ‘:a;N;$!ba;s/\n\n\n*/\n\n/g’

POSIX Character Class Definitions (funciona con las basicas)

POSIX 1003.2 section 2.8.3.2 (6) defines a set of character classesthat denote certain common ranges. They tend to look very ugly but have the advantage that also take into account the ‘locale’, that is, any variant of the local language/coding system. Many utilities/languages provide short-hand ways of invoking these classes. Strictly the names used and hence their contents reference the LC_CTYPE POSIX definition (1003.2 section 2.5.2.1).
Value
Meaning
[:digit:] Only the digits 0 to 9
[:alnum:] Any alphanumeric character 0 to 9 OR A to Z or a to z.
[:alpha:] Any alpha character A to Z or a to z.
[:blank:] Space and TAB characters only.
[:xdigit:] Hexadecimal notation 0-9, A-F, a-f.
[:punct:] Punctuation symbols . , ” ‘ ? ! ; : # $ % & ( ) * + - / < > = @ [ ] \ ^ _ { } | ~
[:print:] Any printable character.
[:space:] Any whitespace characters (space, tab, NL, FF, VT, CR). Many system abbreviate as \s.
[:graph:] Exclude whitespace (SPACE, TAB). Many system abbreviate as \W.
[:upper:] Any alpha character A to Z.
[:lower:] Any alpha character a to z.
[:cntrl:] Control Characters NL CR LF TAB VT FF NUL SOH STX EXT EOT ENQ ACK SO SI DLE DC1 DC2 DC3 DC4 NAK SYN ETB CAN EM SUB ESC IS1 IS2 IS3 IS4 DEL.

These are always used inside square brackets in the form [[:alnum:]] or combined as [[:digit:]a-d]

BACK REFERENCE:
echo “esto es una prueba” | sed “s/.*\(prueba\).*/La palabra encontrada entre el parentesis es \1/ig”
echo “esto es <xml>una</xml> prueba” | sed “s/<[^>]*>\([^<]*\)<[^>]*>/<xml2>\1<\/xml2>/ig”
XML DE LOS COJONES:
echo “esta i<s> me jode i </s><s>me toca los huevos fritos i i</s> i” | sed “s/<\/s>/<\/s>\n/g” | sed “/<s>/ {s/i/<i>/g}”

AWK (better using GAWK)
Reads a file or imput stream line by line and does whatever we want. It divides each line in columns, the default column separator is the whitespace but we can use other by definig -F separator. Awk manages regular expressions and divides the input by $x. By

Ex: hello how are you $0 the whole line $1 hello $2 how…

#using awk script in a file
awk -f fichero.awk # or gawk -f fichero.awk

#regular use
awk ‘BEGIN{something before processing (normally variable declarations and initializations)}
/ereg/{what to do}
/ereg2/{what to do2}
!/rege3/{what to do3}
END{something to do after processing (nomally printing some results)}’

Example (sum values in first column):

{ s += $1 }
END { print s }

IMPORTANT: Execute shell comands from an awk script
/regexp/{print | “shellCommand”}
TRICK: How to use shell variables into a awk script using -v option
awk -v Varname=”$shellvar” ‘awk script …$0 Varname’ “input”

CUT
Su utilidad más común es mostrar una columna de un texto parcialmente estructurado.
cut -d delimiter -f field(columna) (por defecto el delimitador es el tabulador \t)

SORT
Ordena un fichero o un texto pasado por tuberia
-r orden inverso
-g orden numerico general (solucion a 1 123 2)(no es necesario de esta forma insertar ceros al principio del numero)

LEER UN ARCHIVO POR LINEAS PARA PASAR CADA UNA DE ELLAS COMO ARGUMENTO DE ENTRADA DE OTRO PROGRAMA

DOS METODOS

1 Util cuando no solo queremos hacer un cat sinó algo mas
for line in `cat file.txt`;do
actions
done

2 Más rápido si simplemente queremos leer las líneas tal cual
for line in $(< file.txt);do
actions
done

Bourne shell (sh) syntax samples
OPERACIONES BASICAS (ESTRUCTURA)

test:
[ number -lt|-le|-eq|-ne|-ge|-gt number ]
[ string = != string ]

if [ test ]
then
commands
elif [ test ]
commands
else
commands
fi

for var in item1 item2 item3
do
commands
done

while test
do
commands
done

case expression in
case1) commands ;;
case2|case3) commands ;;
*) default-commands ;;
esac

CONCATENAR
a pelo $var/hola/$pepito…

COMANDO TEST

‘test’ es un comando externo que devolverá un código de retorno 0 o 1 dependiendo del resultado de la expresión que siga a continuación.
Se usa muchísimo y tiene dos formatos. ‘test <expr>’ o ‘[ <expr> ]‘. ambas formas son equivalentes.
Las expresiones de este tipo que solo pueden valer TRUE o FALSE se denominan expresiones lógicas.

||
Comprobación sobre ficheros
||
|| -r <fichero> || TRUE si el fichero existe y es legible. ||
|| -w <fichero> || TRUE si el fichero existe y se puede escribir. ||
|| -x <fichero> || TRUE si el fichero existe y es ejecutable. ||
|| -f <fichero> || TRUE si el fichero existe y es de tipo regular (normal). ||
|| -d <fichero> || TRUE si existe y es un directorio. ||
|| -c <fichero> || TRUE si existe y es un dispositivo especial de caracteres. ||
|| -b <fichero> || TRUE si existe y es un dispositivo especial de bloques. ||
|| -p <fichero> || TRUE si existe y es un pipe (fifo). ||
|| -u <fichero> || TRUE si existe y tiene el bit set-user-ID. ||
|| -s <fichero> || TRUE si existe y tiene tamaño mayor que 0. ||
||
Comparación de cadenas
||
|| -z <cadena> || TRUE si es una cadena vacía. ||
|| -n <cadena> || TRUE si es una cadena no vacía. ||
|| <cadena1> = <cadena2> || TRUE si ambas cadenas son idénticas. OJO hay dejar espacios
a un lado y otro del signo igual para no confundir la orden
con una asignación. ||
|| <cadena1> != <cadena2> || TRUE si son distintas. ||
|| <cadena> || TRUE si no es cadena nula. (Una variable que no existe
devuelve cadena nula). ||
||
Comparación de números
||
|| <num1> -eq <num2> || TRUE si ambos números son iguales. ||
|| <num1> -ne <num2> || TRUE si ambos números son distintos. ||
|| <num1> -gt <num2> || TRUE si <num1> mayor que <num2>. ||
|| <num1> -ge <num2> || TRUE si <num1> mayor o igual que <num2>. ||
|| <num1> -lt <num2> || TRUE si <num1> menor que <num2>. ||
|| <num1> -le <num2> || TRUE si <num1> menor o igual que <num2>. ||
||
Operadores lógicos
||
|| ! <expresión_logica> || Operador lógico NOT retorna TRUE si <expresión_logica> es
FALSE y retorna FALSE si <expresión_logica> es TRUE. ||
|| <ExprLogi1> -a <ExprLogi2> || Operador lógico AND retorna TRUE si <ExprLogi1> y
<ExprLogi2> son ambas TRUE y FALSE en caso contrario. ||
|| <ExprLogi1> -o <ExprLogi2> || Operador lógico OR retorna TRUE si <ExprLogi1> o
<ExprLogi2> son alguna de ellas TRUE y FALSE en caso contrario. ||
||
Agrupación de expresiones
||
|| ( <expr> ) || Se pueden usar paréntesis para agrupar expresiones lógicas.
Es decir que valgan TRUE o FALSE ||

DEBUG

debug de un sh se hace con

“sh -x script.sh”

SSH SECURE SHELL CONECTARSE, COMANDOS Y TRANSFERIR FICHEROS

CONNECT

ssh username@remotehost.net

RUNNING COMMANDS Over SSH

Sometimes you don’t really want to run a shell like Bash on the host you are connecting to. Maybe you just want to run a command and exit. This is very simply accomplished by putting the command you wish to run at the end of your ssh connection command.

ssh username@remotehost.net ls -l /home/username/

FILE TRANFER

Here is a basic command that copies a file called report.doc from the local computer to a file by the same name on the remote computer.

scp report.doc username@remote.host.net:

FILE TYPE INFORMATION

file filename (file -i filename to obtain mimetype (codification/charencoding/charset))

DOWNLOAD FILES THROUGH HTTP WITH RESUME

WGET

wget -c url

HOT+KEYS

ctrl+c: canel/abort/terminate foreground process
ctrl+r: Reverse command search
ctrl+z: sleep foreground process
ctrl+s: block terminal
ctrl+q: unblock terminal
windows ctrl+alt+surp…–> ctrl+alt+backspace (I’m not sure)

PS, TOP

Info on computer and processes:
uname -a
ps
ps aux (all processes)
top (interactive) —> q to exit, k to kill a process giving PID

EXECUTE PROCESSES IN BACKGROUND

& (background)
nohup (do not kill the process when exit console)

Example: nohup ls &

SCREEN

Solution for mantain console sessions after closing ssh conections

1. Enter via ssh to a remote computer open a console and write “screen”
2. Execute whatever
3. Ctrl+a (command mode) “d” (detach)
4. Close connection/Exit console
5. Open a new session via shh into the remote computer and type “screen -R” (Recovery last screen)
6. And enjoy your last session as if you wouldn’t close it.

To list screens and see id numbers: “screen -list”
To retach a detached screen to be able to close it with exit: “screen -r idnum”

For more options “man screen”

Makefile, Configure and other building tools (useful information)

./configure [--with-options]

make [export or edit variables: CCFLAGS to change compiler options and LDFLAGS to change linker options]

sudo make install

BASH PROGRAMING (#!/bin/bash)

Some useful things… to be a bash programing basic kit:

To manage options you can use the simple bash builtin function getopts (only manages short options) or if you need a powerful solution then try getopt included in the default util-linux-ng package (getopt(1)) which has the same features as getopt(3) Linux C library (libc).