Recetas Código: Shell Unix
De Daniel Pecos
Comandos útiles
Eliminar caracteres ^M de windows:
Cuidado! Si alguna línea no tiene ^M, perderá el último caracter
sed s/.$// < fileIN > fileOUT
Una alternativa utilizando vi:
:%s/^V^M//g
donde ^V y ^M son CONTROL+V y CONTROL+M (una vez tecleado aparecerá como ^M)
Eliminar el final del contenido de una variable:
$(variable%texto_final}
Hacer un trim (doble) sobre una cadena:
echo " cadena de texto " | sed -e 's/^ *//' -e 's/ *$//'
Uso de arrays:
Para declarar y informar datos en un array:
machines[0]="cassiopea" machines[1]="orion" machines[2]="perseo"
Para conocer la longitud de un array:
${#machines[@]}
Para iterar sobre los elementos de un array:
for machine in ${machines[@]}
ó
for (( i = 0 ; i < ${#machines[@]} ; i++ ))
Sincronizar los ficheros modificados en un directorio sobre otro:
diff -r --brief directorio_origen directorio_destino | grep differ | awk '{cmd = "cp " $2 " " $4; system(cmd)}'
Recuperar un fichero por HTTP a través de un proxy:
curl -x proxy_host:proxy_port URL
Tunneling con netcat:
$ mkfifo fifo $ cat fifo | nc -lp 8080 | nc remote 80 > fifo
(este no he probado si funciona)
netcat -L remote:80 -p 8080
One liners
Display Username and UID sorted by UID Using cut, sort and tr
$ cut -d ':' -f 1,3 /etc/passwd | sort -t ':' -k2n - | tr ':' '\t'
root 0 daemon 1 bin 2 sys 3 sync 4 games 5 man 6 lp 7 mail 8 news 9 uucp 10 proxy 13
Find List of Unique Words in a file Using tr, sed, uniq
$ tr -c a-zA-Z '\n' < Readme1.txt | sed '/^$/d' | sort | uniq -i -c
1 any 1 as 1 at 1 below 1 command 2 Copy 1 data 1 directory 2 file 1 Installtion 1 location 1 output 1 root 1 Run 2 steps 1 tar 1 temporary 4 the 1 to 2 unzip 1 user 1 using
Join Two Files (Where one file is not sorted) Using sort and join
$ cat m1.txt Jincy 500 Amit 300 Saurab 100 Jobi 400 Kumar 200 $ cat m2.txt Amit Monitoring Jincy Marketing Jobi Accounts Kumar Sales Saurab Maintenence $ sort m1.txt | join - m2.txt Amit 300 Monitoring Jincy 500 Marketing Jobi 400 Accounts Kumar 200 Sales Saurab 100 Maintenence
Find out which process is using up your memory using ps, awk, sort
$ ps aux | awk '{if ($5 != 0 ) print $2,$5,$6,$11}' | sort -k2n
PID VSZ RSS COMMAND 3823 3788 484 /sbin/mingetty 3827 3788 484 /sbin/mingetty 3830 3788 484 /sbin/mingetty 3833 3788 488 /sbin/mingetty 3834 3788 484 /sbin/mingetty 3873 3788 484 /sbin/mingetty 2173 3796 568 /usr/sbin/acpid 1835 3800 428 klogd 1832 5904 596 syslogd 2054 5932 540 /usr/sbin/sdpd 2281 6448 360 gpm
Find out Top 10 Largest File or Directory Using du, sort and head
# du -sk /var/log/* | sort -r -n | head -10
1796 /var/log/audit 1200 /var/log/sa 612 /var/log/anaconda.log 512 /var/log/wtmp 456 /var/log/messages.4 92 /var/log/messages.2 76 /var/log/scrollkeeper.log 72 /var/log/secure 56 /var/log/cups 48 /var/log/messages.1
Find out Top 10 Most Used Commands.
$ cat ~/.bash_history | tr "\|\;" "\n" | sed -e "s/^ //g" | cut -d " " -f 1 | sort | uniq -c | sort -n | tail -n 15
11 ssh 12 shutdown 15 cp 15 vncserver 22 cat 23 find 23 pwd 24 mv 25 ovc 47 grep 58 ps 67 vi 74 ll 117 ls 118 cd

